Skip to content

Commit cf4e555

Browse files
authored
Merge pull request #1408 from staabm/fix-test
Fix php version dependent constant deprecation
2 parents f9ff5ed + 47f5063 commit cf4e555

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
use PhpParser\PrettyPrinter\Standard;
2525
use RecursiveIterator;
2626
use Roave\BetterReflection\Reflection\Annotation\AnnotationHelper;
27+
use Roave\BetterReflection\Reflection\Exception\InvalidConstantNode;
2728
use Roave\BetterReflection\SourceLocator\FileChecker;
2829
use Roave\BetterReflection\SourceLocator\SourceStubber\Exception\CouldNotFindPhpStormStubs;
2930
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubs\CachingVisitor;
31+
use Roave\BetterReflection\Util\ConstantNodeChecker;
3032
use SimpleXMLElement;
3133
use SplFixedArray;
3234
use Traversable;
@@ -396,6 +398,15 @@ private function parseFile(string $filePath): void
396398

397399
private function createStub(Node\Stmt\ClassLike|Node\Stmt\Function_|Node\Stmt\Const_|Node\Expr\FuncCall $node, Node\Stmt\Namespace_|null $namespaceNode): string
398400
{
401+
if ($node instanceof Node\Expr\FuncCall) {
402+
try {
403+
ConstantNodeChecker::assertValidDefineFunctionCall($node);
404+
$this->addDeprecatedDocComment($node);
405+
} catch (InvalidConstantNode) {
406+
// just keep going
407+
}
408+
}
409+
399410
if (! ($node instanceof Node\Expr\FuncCall)) {
400411
$this->addDeprecatedDocComment($node);
401412

@@ -612,16 +623,20 @@ private function getStmtType(Node\Stmt\Function_|Node\Stmt\ClassMethod|Node\Stmt
612623
: null;
613624
}
614625

615-
private function addDeprecatedDocComment(Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Stmt\Const_ $node): void
626+
private function addDeprecatedDocComment(Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\FuncCall|Node\Stmt\Const_ $node): void
616627
{
617-
if ($node instanceof Node\Stmt\Const_) {
628+
if ($node instanceof Node\Expr\FuncCall) {
618629
if (! $this->isDeprecatedByPhpDocInPhpVersion($node)) {
619630
$this->removeAnnotationFromDocComment($node, 'deprecated');
620631
}
621632

622633
return;
623634
}
624635

636+
if ($node instanceof Node\Stmt\Const_) {
637+
return;
638+
}
639+
625640
if (! $this->isDeprecatedInPhpVersion($node)) {
626641
$this->removeAnnotationFromDocComment($node, 'deprecated');
627642

@@ -647,7 +662,7 @@ private function addAnnotationToDocComment(
647662
}
648663

649664
private function removeAnnotationFromDocComment(
650-
Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Stmt\Const_ $node,
665+
Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\FuncCall|Node\Stmt\Const_ $node,
651666
string $annotationName,
652667
): void {
653668
$docComment = $node->getDocComment();
@@ -664,14 +679,14 @@ private function isCoreExtension(string $extension): bool
664679
return in_array($extension, self::CORE_EXTENSIONS, true);
665680
}
666681

667-
private function isDeprecatedByPhpDocInPhpVersion(Node\Stmt\Const_ $node): bool
682+
private function isDeprecatedByPhpDocInPhpVersion(Node\Expr\FuncCall $node): bool
668683
{
669684
$docComment = $node->getDocComment();
670685
if ($docComment === null) {
671686
return false;
672687
}
673688

674-
if (preg_match('#@deprecated\s+(\d+)\.(\d+)(?:\.(\d+)?)$#m', $docComment->getText(), $matches) === 1) {
689+
if (preg_match('#@deprecated\s+(\d+)\.(\d+)(?:\.(\d+))?$#m', $docComment->getText(), $matches) === 1) {
675690
$major = $matches[1];
676691
$minor = $matches[2];
677692
$patch = $matches[3] ?? 0;

test/unit/SourceLocator/SourceStubber/PhpStormStubsSourceStubberTest.php

+7-18
Original file line numberDiff line numberDiff line change
@@ -468,37 +468,26 @@ public function testStubForConstantThatExists(): void
468468

469469
public function testStubForConstantThatIsDeprecated(): void
470470
{
471-
// use a faked stub to make this test independent of the actual PHP version
472-
$exampleStub = <<<'EOT'
473-
<?php
474-
475-
/**
476-
* ID of "string" filter.
477-
* @link https://php.net/manual/en/filter.constants.php
478-
* @deprecated 8.1
479-
*/
480-
\define('FILTER_SANITIZE_STRING', 513);
481-
EOT;
482-
$stubData = new StubData($exampleStub, 'filter');
471+
$stubData = $this->sourceStubber->generateConstantStub('MT_RAND_PHP');
483472

484-
self::assertStringMatchesFormat(
485-
"%Adefine('FILTER_SANITIZE_STRING',%w%d);",
473+
self::assertStringContainsString(
474+
'define("MT_RAND_PHP", 1);',
486475
$stubData->getStub(),
487476
);
488477

489-
if (PHP_VERSION_ID >= 80100) {
478+
if (PHP_VERSION_ID >= 80300) {
490479
self::assertStringContainsString(
491-
'@deprecated 8.1',
480+
'@deprecated 8.3',
492481
$stubData->getStub(),
493482
);
494483
} else {
495484
self::assertStringNotContainsString(
496-
'@deprecated 8.1',
485+
'@deprecated 8.3',
497486
$stubData->getStub(),
498487
);
499488
}
500489

501-
self::assertSame('filter', $stubData->getExtensionName());
490+
self::assertSame('standard', $stubData->getExtensionName());
502491
}
503492

504493
public function testNoStubForConstantThatDoesNotExist(): void

0 commit comments

Comments
 (0)