Skip to content

Commit

Permalink
Fix php version dependent constant deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Mar 23, 2024
1 parent 94ca133 commit 39a2081
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
25 changes: 20 additions & 5 deletions src/SourceLocator/SourceStubber/PhpStormStubsSourceStubber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
use PhpParser\PrettyPrinter\Standard;
use RecursiveIterator;
use Roave\BetterReflection\Reflection\Annotation\AnnotationHelper;
use Roave\BetterReflection\Reflection\Exception\InvalidConstantNode;
use Roave\BetterReflection\SourceLocator\FileChecker;
use Roave\BetterReflection\SourceLocator\SourceStubber\Exception\CouldNotFindPhpStormStubs;
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubs\CachingVisitor;
use Roave\BetterReflection\Util\ConstantNodeChecker;
use SimpleXMLElement;
use SplFixedArray;
use Traversable;
Expand Down Expand Up @@ -446,6 +448,15 @@ private function parseFile(string $filePath): void

private function createStub(Node\Stmt\ClassLike|Node\Stmt\Function_|Node\Stmt\Const_|Node\Expr\FuncCall $node, Node\Stmt\Namespace_|null $namespaceNode): string
{
if ($node instanceof Node\Expr\FuncCall) {
try {
ConstantNodeChecker::assertValidDefineFunctionCall($node);
$this->addDeprecatedDocComment($node);
} catch (InvalidConstantNode) {
// just keep going
}
}

if (! ($node instanceof Node\Expr\FuncCall)) {
$this->addDeprecatedDocComment($node);

Expand Down Expand Up @@ -662,16 +673,20 @@ private function getStmtType(Node\Stmt\Function_|Node\Stmt\ClassMethod|Node\Stmt
: null;
}

private function addDeprecatedDocComment(Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Stmt\Const_ $node): void
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
{
if ($node instanceof Node\Stmt\Const_) {
if ($node instanceof Node\Expr\FuncCall) {
if (! $this->isDeprecatedByPhpDocInPhpVersion($node)) {
$this->removeAnnotationFromDocComment($node, 'deprecated');
}

return;
}

if ($node instanceof Node\Stmt\Const_) {
return;
}

if (! $this->isDeprecatedInPhpVersion($node)) {
$this->removeAnnotationFromDocComment($node, 'deprecated');

Expand All @@ -697,7 +712,7 @@ private function addAnnotationToDocComment(
}

private function removeAnnotationFromDocComment(
Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Stmt\Const_ $node,
Node\Stmt\ClassLike|Node\Stmt\ClassConst|Node\Stmt\Property|Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\FuncCall|Node\Stmt\Const_ $node,
string $annotationName,
): void {
$docComment = $node->getDocComment();
Expand All @@ -714,14 +729,14 @@ private function isCoreExtension(string $extension): bool
return in_array($extension, self::CORE_EXTENSIONS, true);
}

private function isDeprecatedByPhpDocInPhpVersion(Node\Stmt\Const_ $node): bool
private function isDeprecatedByPhpDocInPhpVersion(Node\Expr\FuncCall $node): bool
{
$docComment = $node->getDocComment();
if ($docComment === null) {
return false;
}

if (preg_match('#@deprecated\s+(\d+)\.(\d+)(?:\.(\d+)?)$#m', $docComment->getText(), $matches) === 1) {
if (preg_match('#@deprecated\s+(\d+)\.(\d+)(?:\.(\d+))?$#m', $docComment->getText(), $matches) === 1) {
$major = $matches[1];
$minor = $matches[2];
$patch = $matches[3] ?? 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,37 +481,26 @@ public function testStubForConstantThatExists(): void

public function testStubForConstantThatIsDeprecated(): void
{
// use a faked stub to make this test independent of the actual PHP version
$exampleStub = <<<'EOT'
<?php
/**
* ID of "string" filter.
* @link https://php.net/manual/en/filter.constants.php
* @deprecated 8.1
*/
\define('FILTER_SANITIZE_STRING', 513);
EOT;
$stubData = new StubData($exampleStub, 'filter', null);
$stubData = $this->sourceStubber->generateConstantStub('MT_RAND_PHP');

self::assertStringMatchesFormat(
"%Adefine('FILTER_SANITIZE_STRING',%w%d);",
self::assertStringContainsString(
'define("MT_RAND_PHP", 1);',
$stubData->getStub(),
);

if (PHP_VERSION_ID >= 80100) {
if (PHP_VERSION_ID >= 80300) {
self::assertStringContainsString(
'@deprecated 8.1',
'@deprecated 8.3',
$stubData->getStub(),
);
} else {
self::assertStringNotContainsString(
'@deprecated 8.1',
'@deprecated 8.3',
$stubData->getStub(),
);
}

self::assertSame('filter', $stubData->getExtensionName());
self::assertSame('standard', $stubData->getExtensionName());
}

public function testNoStubForConstantThatDoesNotExist(): void
Expand Down

0 comments on commit 39a2081

Please sign in to comment.