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
3 changes: 3 additions & 0 deletions src/Objects/AbstractFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ private function renderDefaultExpression($expression): string {

return '[' . implode(', ', $items) . ']';
}
if ($expression instanceof \PhpParser\Node\Scalar\MagicConst) {
return $expression->getName();
}
if ($expression instanceof \PhpParser\Node\Expr\ConstFetch) {
return $this->parentObject->getAbsoluteConstant($expression->name);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Objects/ClassConstObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ private function renderValue($value): string {

return '[' . implode(', ', $items) . ']';
}
if ($value instanceof \PhpParser\Node\Scalar\MagicConst) {
return $value->getName();
}
if ($value instanceof \PhpParser\Node\Expr\ConstFetch) {
if ($this->typeLookup !== null) {
return $this->typeLookup->getAbsoluteConstant($value->name);
Expand Down
3 changes: 3 additions & 0 deletions src/Objects/NamespaceConstObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ private function renderValue($value): string {

return '[' . implode(', ', $items) . ']';
}
if ($value instanceof \PhpParser\Node\Scalar\MagicConst) {
return $value->getName();
}
if ($value instanceof \PhpParser\Node\Expr\ConstFetch) {
return $this->typeLookup->getAbsoluteConstant($value->name);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,52 @@ protected function cloneParent(parent $source): parent {}
assertSameValue('Adding PHP 7.2 built-in or contextual types should affect the diff result.', 'MAJOR', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testSignatureSearchFormatsMagicConstants(): void {
$root = createRepository('magic-constant-rendering', [
'src/Values.php' => <<<'PHP'
<?php
namespace Demo;
function build($dir = __DIR__, $file = __FILE__): void {}
class Info {
public const SELF_CLASS = __CLASS__;
}
const CURRENT_NAMESPACE = __NAMESPACE__;
PHP,
]);

$signatures = getSignaturesForFiles($root, ['src/Values.php']);
assertSameList('Magic constants should be preserved explicitly in signatures.', [
'\Demo\CURRENT_NAMESPACE = __NAMESPACE__',
'\Demo\Info->__construct()',
'\Demo\Info::SELF_CLASS = __CLASS__',
'\Demo\build():void',
'\Demo\build(mixed = __DIR__):void',
'\Demo\build(mixed = __DIR__, mixed = __FILE__):void',
'\Demo\build(mixed, mixed):void',
'\Demo\build(mixed):void',
], $signatures);
}

function testMagicConstantChangesAffectDiffs(): void {
$root = createRepository('magic-constant-diff', [
'src/Values.php' => <<<'PHP'
<?php
namespace Demo;
function build($value = __DIR__): void {}
PHP,
]);

writeFile($root . '/src/Values.php', <<<'PHP'
<?php
namespace Demo;
function build($value = __FILE__): void {}
PHP
);

$diff = new SemVerDiff($root, [], []);
assertSameValue('Changing a magic constant in a signature should affect the diff result.', 'MAJOR', $diff->diff('HEAD', 'WC')->getIncrement());
}

function testSignatureSearchFormatsRicherDefaultExpressions(): void {
$root = createRepository('default-expression-shapes', [
'src/Defaults.php' => <<<'PHP'
Expand Down Expand Up @@ -1902,6 +1948,8 @@ function testCliParsingAndDefaults(): void {
testTraitUseSignatureModelsRenderCurrentStrings();
testNamespaceConstantSignatureModelsRenderCurrentStrings();
testSignatureSearchResolvesImportedConstantsInDefaultsAndValues();
testSignatureSearchFormatsMagicConstants();
testMagicConstantChangesAffectDiffs();
testSignatureSearchResolvesGroupedConstImportsInDefaults();
testImportedConstantChangesAffectDiffs();
testConstantIdentityAndSignatureEqualityUsesVisibility();
Expand Down
Loading