From 5d1bddc1679a6130af0ca0b02b2ca1aaac2f1b8a Mon Sep 17 00:00:00 2001 From: James Buncle Date: Wed, 1 Apr 2026 11:35:20 +0100 Subject: [PATCH] Preserve magic constants in signatures --- src/Objects/AbstractFunction.php | 3 ++ src/Objects/ClassConstObject.php | 3 ++ src/Objects/NamespaceConstObject.php | 3 ++ tests/run.php | 48 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/src/Objects/AbstractFunction.php b/src/Objects/AbstractFunction.php index c64b499..4f7f2df 100644 --- a/src/Objects/AbstractFunction.php +++ b/src/Objects/AbstractFunction.php @@ -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); } diff --git a/src/Objects/ClassConstObject.php b/src/Objects/ClassConstObject.php index 5e02e83..1008fa0 100644 --- a/src/Objects/ClassConstObject.php +++ b/src/Objects/ClassConstObject.php @@ -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); diff --git a/src/Objects/NamespaceConstObject.php b/src/Objects/NamespaceConstObject.php index 1ed995c..5d99d1d 100644 --- a/src/Objects/NamespaceConstObject.php +++ b/src/Objects/NamespaceConstObject.php @@ -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); } diff --git a/tests/run.php b/tests/run.php index 95099f5..f1244e2 100644 --- a/tests/run.php +++ b/tests/run.php @@ -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' +__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' +diff('HEAD', 'WC')->getIncrement()); +} + function testSignatureSearchFormatsRicherDefaultExpressions(): void { $root = createRepository('default-expression-shapes', [ 'src/Defaults.php' => <<<'PHP' @@ -1902,6 +1948,8 @@ function testCliParsingAndDefaults(): void { testTraitUseSignatureModelsRenderCurrentStrings(); testNamespaceConstantSignatureModelsRenderCurrentStrings(); testSignatureSearchResolvesImportedConstantsInDefaultsAndValues(); +testSignatureSearchFormatsMagicConstants(); +testMagicConstantChangesAffectDiffs(); testSignatureSearchResolvesGroupedConstImportsInDefaults(); testImportedConstantChangesAffectDiffs(); testConstantIdentityAndSignatureEqualityUsesVisibility();