diff --git a/composer.json b/composer.json index 337f0ee..0fcbd7a 100644 --- a/composer.json +++ b/composer.json @@ -13,16 +13,16 @@ } ], "require": { - "php": "^7.4", + "php": "^7.4 || ^8.0", "ext-json" : "*", - "nikic/php-parser": "^4.0.0", - "symfony/console": "^5.0" + "nikic/php-parser": "^4.0", + "symfony/console": "^5.0 || ^6.0" }, "require-dev": { "phpunit/phpunit": "^9.5", - "symfony/finder": "^5.4", + "symfony/finder": "^5.4 || ^6.0", "php-stubs/wordpress-globals": "0.2.0", - "php-stubs/wordpress-stubs": "^5.9" + "php-stubs/wordpress-stubs": "^6.0" }, "autoload": { "psr-4": { diff --git a/src/NodeVisitor/Categorize.php b/src/NodeVisitor/Categorize.php index f8b84b5..18ef0df 100644 --- a/src/NodeVisitor/Categorize.php +++ b/src/NodeVisitor/Categorize.php @@ -55,26 +55,30 @@ public function leaveNode(Node $node) { if ($node instanceof Node\Stmt\Class_) { $this->addClassNames($node); - return; + return null; } if ($node instanceof Node\Stmt\Interface_) { $this->addInterfaceNames($node); - return; + return null; } if ($node instanceof Node\Stmt\Function_) { $this->addFunctionNames($node); - return; + return null; } if ($node instanceof Node\Stmt\Trait_) { $this->addTraitNames($node); - return; + return null; } if ($node instanceof Node\Stmt\Const_) { $this->addConstantNames($node); - return; + return null; } - if ($node instanceof Node\Stmt\Expression) { + if ($node instanceof Node\Stmt\Expression + && $node->expr instanceof Node\Expr\FuncCall + && $node->expr->name instanceof Node\Name + && $node->expr->name->toString() === 'define' + ) { $this->addDefineConstantNames($node); } @@ -178,7 +182,21 @@ private function addDefineConstantNames(Node $node) :void } try { - $this->constants[] = (string) $node->expr->args[0]->value->value; + $valueNode = $node->expr->args[0]->value; + + // Handle different types of scalar nodes + if ($valueNode instanceof Node\Scalar\String_) { + $constantName = $valueNode->value; + } elseif ($valueNode instanceof Node\Scalar\Encapsed) { + // For interpolated strings, we can't reliably extract a constant name + // Skip these cases to avoid warnings + return; + } else { + // For other node types, try to convert to string if possible + $constantName = (string) $valueNode; + } + + $this->constants[] = $constantName; } catch (Throwable $e) { throw new RuntimeException( "define() declaration has no constant name.\n{$e->getMessage()}", diff --git a/src/NodeVisitor/Filter.php b/src/NodeVisitor/Filter.php index d5c3b96..147f4b0 100644 --- a/src/NodeVisitor/Filter.php +++ b/src/NodeVisitor/Filter.php @@ -28,11 +28,8 @@ public function enterNode(Node $node) } } - public function leaveNode(Node $node) :?int + public function leaveNode(Node $node) { - if ( ! $this->isOfInterest($node) && $node instanceof Node\Stmt) { - return NodeTraverser::REMOVE_NODE; - } return null; }