Skip to content

Commit b9de798

Browse files
committed
Detect case only name change for Traits
1 parent d0191d8 commit b9de798

File tree

4 files changed

+120
-14
lines changed

4 files changed

+120
-14
lines changed

src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
3434
$mappingsAfterKeyed[strtolower($interfaceAfter->name)] = $registryAfter->mapping['interface'][$key];
3535
}
3636

37-
3837
$interfaceNamesBefore = array_keys($interfacesBeforeKeyed);
3938
$interfaceNamesAfter = array_keys($interfacesAfterKeyed);
4039
$added = array_diff($interfaceNamesAfter, $interfaceNamesBefore);

src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPSemVerChecker\Operation\TraitAdded;
66
use PHPSemVerChecker\Operation\TraitRemoved;
7+
use PHPSemVerChecker\Operation\TraitRenamedCaseOnly;
78
use PHPSemVerChecker\Registry\Registry;
89
use PHPSemVerChecker\Report\Report;
910

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

17-
$keysBefore = array_keys($registryBefore->data['trait']);
18-
$keysAfter = array_keys($registryAfter->data['trait']);
19-
$added = array_diff($keysAfter, $keysBefore);
20-
$removed = array_diff($keysBefore, $keysAfter);
21-
$toVerify = array_intersect($keysBefore, $keysAfter);
18+
$traitsBefore = $registryBefore->data['trait'];
19+
$traitsAfter = $registryAfter->data['trait'];
20+
21+
$traitsBeforeKeyed = [];
22+
$mappingsBeforeKeyed = [];
23+
foreach($traitsBefore as $key => $traitBefore)
24+
{
25+
$traitsBeforeKeyed[strtolower($traitBefore->name)] = $traitBefore;
26+
$mappingsBeforeKeyed[strtolower($traitBefore->name)] = $registryBefore->mapping['trait'][$key];
27+
}
28+
29+
$traitsAfterKeyed = [];
30+
$mappingsAfterKeyed = [];
31+
foreach($traitsAfter as $key => $traitAfter)
32+
{
33+
$traitsAfterKeyed[strtolower($traitAfter->name)] = $traitAfter;
34+
$mappingsAfterKeyed[strtolower($traitAfter->name)] = $registryAfter->mapping['trait'][$key];
35+
}
36+
37+
$traitNamesBefore = array_keys($traitsBeforeKeyed);
38+
$traitNamesAfter = array_keys($traitsAfterKeyed);
39+
$added = array_diff($traitNamesAfter, $traitNamesBefore);
40+
$removed = array_diff($traitNamesBefore, $traitNamesAfter);
41+
$toVerify = array_intersect($traitNamesBefore, $traitNamesAfter);
2242

2343
foreach ($removed as $key) {
24-
$fileBefore = $registryBefore->mapping['trait'][$key];
25-
$traitBefore = $registryBefore->data['trait'][$key];
44+
$fileBefore = $mappingsBeforeKeyed[$key];
45+
$traitBefore = $traitsBeforeKeyed[$key];
2646

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

3151
foreach ($toVerify as $key) {
32-
$fileBefore = $registryBefore->mapping['trait'][$key];
52+
$fileBefore = $mappingsBeforeKeyed[$key];
3353
/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
34-
$traitBefore = $registryBefore->data['trait'][$key];
35-
$fileAfter = $registryAfter->mapping['trait'][$key];
54+
$traitBefore = $traitsBeforeKeyed[$key];
55+
$fileAfter = $mappingsAfterKeyed[$key];
3656
/** @var \PhpParser\Node\Stmt\Class_ $traitBefore */
37-
$traitAfter = $registryAfter->data['trait'][$key];
57+
$traitAfter = $traitsAfterKeyed[$key];
3858

3959
// Leave non-strict comparison here
4060
if ($traitBefore != $traitAfter) {
61+
62+
// Check for name case change.
63+
if(
64+
$traitBefore->name !== $traitAfter->name
65+
&& strtolower($traitBefore->name) === strtolower($traitAfter->name)
66+
) {
67+
$report->add($this->context, new TraitRenamedCaseOnly($fileAfter, $traitAfter));
68+
}
69+
4170
$analyzers = [
4271
new ClassMethodAnalyzer('trait', $fileBefore, $fileAfter),
4372
new PropertyAnalyzer('trait', $fileBefore, $fileAfter),
@@ -51,8 +80,8 @@ public function analyze(Registry $registryBefore, Registry $registryAfter)
5180
}
5281

5382
foreach ($added as $key) {
54-
$fileAfter = $registryAfter->mapping['trait'][$key];
55-
$traitAfter = $registryAfter->data['trait'][$key];
83+
$fileAfter = $mappingsAfterKeyed[$key];
84+
$traitAfter = $traitsAfter[$key];
5685

5786
$data = new TraitAdded($fileAfter, $traitAfter);
5887
$report->addTrait($data);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Operation;
4+
5+
use PhpParser\Node\Stmt\Trait_;
6+
use PHPSemVerChecker\Node\Statement\Trait_ as PTrait;
7+
8+
class TraitRenamedCaseOnly extends Operation {
9+
/**
10+
* @var string
11+
*/
12+
protected $code = 'V152';
13+
/**
14+
* @var string
15+
*/
16+
protected $reason = 'Trait was renamed (case only).';
17+
/**
18+
* @var string
19+
*/
20+
protected $fileAfter;
21+
/**
22+
* @var \PhpParser\Node\Stmt\Trait_
23+
*/
24+
protected $traitAfter;
25+
26+
/**
27+
* @param string $fileAfter
28+
* @param \PhpParser\Node\Stmt\Trait_ $traitAfter
29+
*/
30+
public function __construct($fileAfter, Trait_ $traitAfter)
31+
{
32+
$this->fileAfter = $fileAfter;
33+
$this->traitAfter = $traitAfter;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getLocation()
40+
{
41+
return $this->fileAfter;
42+
}
43+
44+
/**
45+
* @return int
46+
*/
47+
public function getLine()
48+
{
49+
return $this->traitAfter->getLine();
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getTarget()
56+
{
57+
return PTrait::getFullyQualifiedName($this->traitAfter);
58+
}
59+
}

tests/PHPSemVerChecker/Analyzer/TraitAnalyzerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,23 @@ public function testTraitAdded()
6262
$this->assertSame('Trait was added.', $report[$context][$expectedLevel][0]->getReason());
6363
$this->assertSame('tmp', $report[$context][$expectedLevel][0]->getTarget());
6464
}
65+
66+
public function testTraitRenamedCaseOnly()
67+
{
68+
$before = new Registry();
69+
$after = new Registry();
70+
71+
$before->addTrait(new Trait_('testTRAIT'));
72+
$after->addTrait(new Trait_('testtrait'));
73+
74+
$analyzer = new TraitAnalyzer();
75+
$report = $analyzer->analyze($before, $after);
76+
77+
$context = 'trait';
78+
$expectedLevel = Level::PATCH;
79+
Assert::assertDifference($report, $context, $expectedLevel);
80+
$this->assertSame('V152', $report[$context][$expectedLevel][0]->getCode());
81+
$this->assertSame('Trait was renamed (case only).', $report[$context][$expectedLevel][0]->getReason());
82+
$this->assertSame('testtrait', $report[$context][$expectedLevel][0]->getTarget());
83+
}
6584
}

0 commit comments

Comments
 (0)