Skip to content

Commit

Permalink
Merge pull request #1408 from staabm/fix-test
Browse files Browse the repository at this point in the history
Fix php version dependent constant deprecation
  • Loading branch information
Ocramius authored Mar 18, 2024
2 parents f9ff5ed + 47f5063 commit cf4e555
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 @@ -396,6 +398,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 @@ -612,16 +623,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 @@ -647,7 +662,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 @@ -664,14 +679,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 @@ -468,37 +468,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');
$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 cf4e555

Please sign in to comment.