Skip to content

Case insensitive method names #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e6cb7e2
Dont treat case changes in method names as breaking
emmetog Feb 8, 2018
4f565b6
Change indentation to tabs
emmetog Feb 12, 2018
a382291
Add some tests for Signature comparator
emmetog Feb 12, 2018
63f11e9
Detect method name case changes
emmetog Feb 12, 2018
d2418c3
Make renaming method case a PATCH change
emmetog Feb 12, 2018
6424295
Remove commented var_dump
emmetog Feb 12, 2018
5b091f4
Detect interface case name changes as PATCH
emmetog Feb 13, 2018
d0191d8
Revert Signature::analyze
emmetog Feb 14, 2018
b9de798
Detect case only name change for Traits
emmetog Feb 14, 2018
558aaae
Detect case only name changes for Classes
emmetog Feb 14, 2018
f8e54b3
Add rule for trait renamed case only
emmetog Feb 14, 2018
e1d49be
Split method and trait rename into visibility
emmetog Feb 19, 2018
9ca9220
Add tests for all visibilities of case change
emmetog Feb 19, 2018
50b291d
Merge branch 'master' into case-insensitive-method-names
emmetog Feb 19, 2018
e527747
Interface operation extend from Delta
emmetog Feb 19, 2018
596646c
Rename $mappingsBeforeKeyed to $filesBeforeKeyed
emmetog Feb 19, 2018
0c24711
Remove unneccessary compare of lowercase names
emmetog Feb 19, 2018
d49730c
Remove empty line
emmetog Feb 19, 2018
a15c6b7
Make ClassMethodRenamedCaseOnly extend Delta
emmetog Feb 19, 2018
fbdb940
Make TraitRenamedCaseOnly extend Delta
emmetog Feb 19, 2018
d672b2d
Spaces to tabs
emmetog Feb 19, 2018
ce3e042
Rename test methods
emmetog Feb 19, 2018
667e0be
Detect function renaming too
emmetog Feb 19, 2018
e50e738
Rename all operations to CaseChanged
emmetog Feb 19, 2018
931d81f
Use case changed instead of rename in rule names
emmetog Feb 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Detect case only name change for Traits
  • Loading branch information
emmetog committed Feb 14, 2018
commit b9de798637ce956ccffe15a70d481765ef67cf2c
1 change: 0 additions & 1 deletion src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
$mappingsAfterKeyed[strtolower($interfaceAfter->name)] = $registryAfter->mapping['interface'][$key];
}


$interfaceNamesBefore = array_keys($interfacesBeforeKeyed);
$interfaceNamesAfter = array_keys($interfacesAfterKeyed);
$added = array_diff($interfaceNamesAfter, $interfaceNamesBefore);
Expand Down
55 changes: 42 additions & 13 deletions src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPSemVerChecker\Operation\TraitAdded;
use PHPSemVerChecker\Operation\TraitRemoved;
use PHPSemVerChecker\Operation\TraitRenamedCaseOnly;
use PHPSemVerChecker\Registry\Registry;
use PHPSemVerChecker\Report\Report;

Expand All @@ -14,30 +15,58 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
{
$report = new Report();

$keysBefore = array_keys($registryBefore->data['trait']);
$keysAfter = array_keys($registryAfter->data['trait']);
$added = array_diff($keysAfter, $keysBefore);
$removed = array_diff($keysBefore, $keysAfter);
$toVerify = array_intersect($keysBefore, $keysAfter);
$traitsBefore = $registryBefore->data['trait'];
$traitsAfter = $registryAfter->data['trait'];

$traitsBeforeKeyed = [];
$mappingsBeforeKeyed = [];
foreach($traitsBefore as $key => $traitBefore)
{
$traitsBeforeKeyed[strtolower($traitBefore->name)] = $traitBefore;
$mappingsBeforeKeyed[strtolower($traitBefore->name)] = $registryBefore->mapping['trait'][$key];
}

$traitsAfterKeyed = [];
$mappingsAfterKeyed = [];
foreach($traitsAfter as $key => $traitAfter)
{
$traitsAfterKeyed[strtolower($traitAfter->name)] = $traitAfter;
$mappingsAfterKeyed[strtolower($traitAfter->name)] = $registryAfter->mapping['trait'][$key];
}

$traitNamesBefore = array_keys($traitsBeforeKeyed);
$traitNamesAfter = array_keys($traitsAfterKeyed);
$added = array_diff($traitNamesAfter, $traitNamesBefore);
$removed = array_diff($traitNamesBefore, $traitNamesAfter);
$toVerify = array_intersect($traitNamesBefore, $traitNamesAfter);

foreach ($removed as $key) {
$fileBefore = $registryBefore->mapping['trait'][$key];
$traitBefore = $registryBefore->data['trait'][$key];
$fileBefore = $mappingsBeforeKeyed[$key];
$traitBefore = $traitsBeforeKeyed[$key];

$data = new TraitRemoved($fileBefore, $traitBefore);
$report->addTrait($data);
}

foreach ($toVerify as $key) {
$fileBefore = $registryBefore->mapping['trait'][$key];
$fileBefore = $mappingsBeforeKeyed[$key];
/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
$traitBefore = $registryBefore->data['trait'][$key];
$fileAfter = $registryAfter->mapping['trait'][$key];
$traitBefore = $traitsBeforeKeyed[$key];
$fileAfter = $mappingsAfterKeyed[$key];
/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
$traitAfter = $registryAfter->data['trait'][$key];
$traitAfter = $traitsAfterKeyed[$key];

// Leave non-strict comparison here
if ($traitBefore != $traitAfter) {

// Check for name case change.
if(
$traitBefore->name !== $traitAfter->name
&& strtolower($traitBefore->name) === strtolower($traitAfter->name)
) {
$report->add($this->context, new TraitRenamedCaseOnly($fileAfter, $traitAfter));
}

$analyzers = [
new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
Expand All @@ -51,8 +80,8 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
}

foreach ($added as $key) {
$fileAfter = $registryAfter->mapping['trait'][$key];
$traitAfter = $registryAfter->data['trait'][$key];
$fileAfter = $mappingsAfterKeyed[$key];
$traitAfter = $traitsAfter[$key];

$data = new TraitAdded($fileAfter, $traitAfter);
$report->addTrait($data);
Expand Down
59 changes: 59 additions & 0 deletions src/PHPSemVerChecker/Operation/TraitRenamedCaseOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace PHPSemVerChecker\Operation;

use PhpParser\Node\Stmt\Trait_;
use PHPSemVerChecker\Node\Statement\Trait_ as PTrait;

class TraitRenamedCaseOnly extends Operation {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a delta operation.

/**
* @var string
*/
protected $code = 'V152';
/**
* @var string
*/
protected $reason = 'Trait was renamed (case only).';
/**
* @var string
*/
protected $fileAfter;
/**
* @var \PhpParser\Node\Stmt\Trait_
*/
protected $traitAfter;

/**
* @param string $fileAfter
* @param \PhpParser\Node\Stmt\Trait_ $traitAfter
*/
public function __construct($fileAfter, Trait_ $traitAfter)
{
$this->fileAfter = $fileAfter;
$this->traitAfter = $traitAfter;
}

/**
* @return string
*/
public function getLocation()
{
return $this->fileAfter;
}

/**
* @return int
*/
public function getLine()
{
return $this->traitAfter->getLine();
}

/**
* @return string
*/
public function getTarget()
{
return PTrait::getFullyQualifiedName($this->traitAfter);
}
}
19 changes: 19 additions & 0 deletions tests/PHPSemVerChecker/Analyzer/TraitAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,23 @@ public function testTraitAdded()
$this->assertSame('Trait was added.', $report[$context][$expectedLevel][0]->getReason());
$this->assertSame('tmp', $report[$context][$expectedLevel][0]->getTarget());
}

public function testTraitRenamedCaseOnly()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name should be testTraitNameCaseChanged.

{
$before = new Registry();
$after = new Registry();

$before->addTrait(new Trait_('testTRAIT'));
$after->addTrait(new Trait_('testtrait'));

$analyzer = new TraitAnalyzer();
$report = $analyzer->analyze($before, $after);

$context = 'trait';
$expectedLevel = Level::PATCH;
Assert::assertDifference($report, $context, $expectedLevel);
$this->assertSame('V152', $report[$context][$expectedLevel][0]->getCode());
$this->assertSame('Trait was renamed (case only).', $report[$context][$expectedLevel][0]->getReason());
$this->assertSame('testtrait', $report[$context][$expectedLevel][0]->getTarget());
}
}