Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 17 additions & 3 deletions src/Signature/ContractIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function toIdentityKey(): string {
'kind:' . $this->kind,
'types:[' . implode(',', array_map(function (IdentityKey $type): string {
return $type->toIdentityKey();
}, $this->types)) . ']',
}, $this->getNormalisedTypes())) . ']',
]);
}

Expand All @@ -41,12 +41,26 @@ public function equals(IdentityKey $other): bool {
return false;
}

foreach ($this->types as $index => $type) {
if (!$type->equals($other->types[$index])) {
$left = $this->getNormalisedTypes();
$right = $other->getNormalisedTypes();
foreach ($left as $index => $type) {
if (!$type->equals($right[$index])) {
return false;
}
}

return true;
}

/**
* @return IdentityKey[]
*/
private function getNormalisedTypes(): array {
$types = $this->types;
usort($types, function (IdentityKey $left, IdentityKey $right): int {
return strcmp($left->toIdentityKey(), $right->toIdentityKey());
});

return $types;
}
}
25 changes: 22 additions & 3 deletions src/Signature/TraitUseIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function toIdentityKey(): string {
'kind:' . $this->kind,
'traits:[' . implode(',', array_map(function (IdentityKey $trait): string {
return $trait->toIdentityKey();
}, $this->traits)) . ']',
}, $this->getNormalisedTraits())) . ']',
'method:' . ($this->method ?? ''),
'newName:' . ($this->newName ?? ''),
'newModifier:' . ($this->newModifier ?? ''),
Expand All @@ -67,12 +67,31 @@ public function equals(IdentityKey $other): bool {
return false;
}

foreach ($this->traits as $index => $trait) {
if (!$trait->equals($other->traits[$index])) {
$left = $this->getNormalisedTraits();
$right = $other->getNormalisedTraits();
foreach ($left as $index => $trait) {
if (!$trait->equals($right[$index])) {
return false;
}
}

return true;
}

/**
* @return IdentityKey[]
*/
private function getNormalisedTraits(): array {
if ($this->kind !== 'precedence' || count($this->traits) <= 2) {
return $this->traits;
}

$selected = $this->traits[0];
$insteadOfTraits = array_slice($this->traits, 1);
usort($insteadOfTraits, function (IdentityKey $left, IdentityKey $right): int {
return strcmp($left->toIdentityKey(), $right->toIdentityKey());
});

return array_merge([$selected], $insteadOfTraits);
}
}
86 changes: 86 additions & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,89 @@ function testNamespaceConstantOrderingDoesNotBumpVersion(): void {
assertSameValue('Reordering namespace constants without changing values should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}


function testImplementedContractOrderingDoesNotBumpVersion(): void {
$root = createRepository('implements-ordering', [
'src/Worker.php' => <<<'PHP'
<?php
namespace Demo;
use Vendor\Contract\FirstContract;
use Vendor\Contract\SecondContract;
class Worker implements FirstContract, SecondContract {}
PHP,
]);

writeFile($root . '/src/Worker.php', <<<'PHP'
<?php
namespace Demo;
use Vendor\Contract\FirstContract;
use Vendor\Contract\SecondContract;
class Worker implements SecondContract, FirstContract {}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Reordering implemented contracts without changing the set should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testExtendedInterfaceOrderingDoesNotBumpVersion(): void {
$root = createRepository('interface-extends-ordering', [
'src/Contract.php' => <<<'PHP'
<?php
namespace Demo;
use Vendor\Contract\FirstBase;
use Vendor\Contract\SecondBase;
interface Contract extends FirstBase, SecondBase {}
PHP,
]);

writeFile($root . '/src/Contract.php', <<<'PHP'
<?php
namespace Demo;
use Vendor\Contract\FirstBase;
use Vendor\Contract\SecondBase;
interface Contract extends SecondBase, FirstBase {}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Reordering extended interfaces without changing the set should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testTraitPrecedenceOrderingDoesNotBumpVersion(): void {
$root = createRepository('trait-precedence-ordering', [
'src/Worker.php' => <<<'PHP'
<?php
namespace Demo;
trait SharedTrait { public function boot() {} }
trait FirstFallback { public function boot() {} }
trait SecondFallback { public function boot() {} }
class Worker {
use SharedTrait, FirstFallback, SecondFallback {
SharedTrait::boot insteadof FirstFallback, SecondFallback;
}
}
PHP,
]);

writeFile($root . '/src/Worker.php', <<<'PHP'
<?php
namespace Demo;
trait SharedTrait { public function boot() {} }
trait FirstFallback { public function boot() {} }
trait SecondFallback { public function boot() {} }
class Worker {
use SharedTrait, FirstFallback, SecondFallback {
SharedTrait::boot insteadof SecondFallback, FirstFallback;
}
}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Reordering insteadof fallback traits without changing the set should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testIncludePathsRestrictTheSurface(): void {
$root = createRepository('include-paths', [
'src/Foo.php' => "<?php\nnamespace Demo;\nclass Foo { public function stableMethod() {} }\n",
Expand Down Expand Up @@ -2209,6 +2292,9 @@ function testCliParsingAndDefaults(): void {
testGroupedAndUngroupedConstImportsRemainEquivalent();
testTraitAliasFormattingEquivalenceIsPatch();
testNamespaceConstantOrderingDoesNotBumpVersion();
testImplementedContractOrderingDoesNotBumpVersion();
testExtendedInterfaceOrderingDoesNotBumpVersion();
testTraitPrecedenceOrderingDoesNotBumpVersion();
testGitIgnoreInlineCommentsAreIgnored();
testRootAnchoredGitIgnorePatternsAreHonoured();
testIncludePathsRestrictTheSurface();
Expand Down
Loading