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
18 changes: 17 additions & 1 deletion src/Objects/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private function getTraitUseSignatureModels(): array {

if ($adaptation instanceof \PhpParser\Node\Stmt\TraitUseAdaptation\Alias) {
$signatures[] = TraitUseSignature::forAlias(
$adaptation->trait !== null ? $this->createTypeReferenceFromName($adaptation->trait) : null,
$this->getTraitAliasTarget($adaptation, $traits),
(string) $adaptation->method,
$adaptation->newName !== null ? (string) $adaptation->newName : null,
$this->getTraitAliasModifier($adaptation->newModifier)
Expand All @@ -200,6 +200,22 @@ private function getTraitUseSignatureModels(): array {
return $signatures;
}


/**
* @param TypeReference[] $traits
*/
private function getTraitAliasTarget(\PhpParser\Node\Stmt\TraitUseAdaptation\Alias $adaptation, array $traits): ?TypeReference {
if ($adaptation->trait !== null) {
return $this->createTypeReferenceFromName($adaptation->trait);
}

if (count($traits) === 1) {
return $traits[0];
}

return null;
}

private function getTraitAliasModifier(?int $modifier): ?string {
if ($modifier === null) {
return null;
Expand Down
103 changes: 103 additions & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,105 @@ function testGitIgnoreInlineCommentsAreIgnored(): void {
assertSameValue('Ignored files should not affect the increment.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testGroupedAndUngroupedTypeImportsRemainEquivalent(): void {
$root = createRepository('grouped-import-equivalence', [
'src/Types.php' => <<<'PHP'
<?php
namespace Demo;
use Vendor\Package\Thing as Alias;
use Vendor\Package\Other;
function build(?Alias $item, Other $other = null): ?Alias {}
PHP,
]);

writeFile($root . '/src/Types.php', <<<'PHP'
<?php
namespace Demo;
use Vendor\Package\{Thing as Alias, Other};
function build(?Alias $item, Other $other = null): ?Alias {}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Grouped and ungrouped type imports with the same resolved targets should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testGroupedAndUngroupedConstImportsRemainEquivalent(): void {
$root = createRepository('grouped-const-import-equivalence', [
'src/Config.php' => <<<'PHP'
<?php
namespace Demo;
use const Vendor\Config\DEFAULT_MODE;
function build($mode = DEFAULT_MODE): void {}
PHP,
]);

writeFile($root . '/src/Config.php', <<<'PHP'
<?php
namespace Demo;
use Vendor\Config\{const DEFAULT_MODE};
function build($mode = DEFAULT_MODE): void {}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Grouped and ungrouped const imports with the same resolved targets should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testTraitAliasFormattingEquivalenceIsPatch(): void {
$root = createRepository('trait-alias-equivalence', [
'src/Traits.php' => <<<'PHP'
<?php
namespace Demo;
trait SharedTrait {
public function boot() {}
}
class Worker {
use SharedTrait {
SharedTrait::boot as private;
}
}
PHP,
]);

writeFile($root . '/src/Traits.php', <<<'PHP'
<?php
namespace Demo;
trait SharedTrait {
public function boot() {}
}
class Worker {
use SharedTrait {
boot as private;
}
}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Qualified and unqualified trait aliases with the same semantic meaning should remain PATCH.', 'PATCH', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testNamespaceConstantOrderingDoesNotBumpVersion(): void {
$root = createRepository('namespace-constant-ordering', [
'src/Constants.php' => <<<'PHP'
<?php
namespace Demo;
const STATUS = 'ok', MODE = 'safe';
PHP,
]);

writeFile($root . '/src/Constants.php', <<<'PHP'
<?php
namespace Demo;
const MODE = 'safe', STATUS = 'ok';
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Reordering namespace constants without changing values 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 @@ -2041,6 +2140,10 @@ function testCliParsingAndDefaults(): void {
testDiffReportRendererUsesReportAccessors();
testSignatureIdentityKeepsCurrentDiffBehaviour();
testExcludePathsAreHonoured();
testGroupedAndUngroupedTypeImportsRemainEquivalent();
testGroupedAndUngroupedConstImportsRemainEquivalent();
testTraitAliasFormattingEquivalenceIsPatch();
testNamespaceConstantOrderingDoesNotBumpVersion();
testGitIgnoreInlineCommentsAreIgnored();
testRootAnchoredGitIgnorePatternsAreHonoured();
testIncludePathsRestrictTheSurface();
Expand Down
Loading