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 committed Mar 17, 2024
1 parent 7eb6d2e commit 145cdb2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 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,7 +679,7 @@ 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) {
Expand Down

0 comments on commit 145cdb2

Please sign in to comment.