Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
32 changes: 25 additions & 7 deletions src/NodeVisitor/Categorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()}",
Expand Down
5 changes: 1 addition & 4 deletions src/NodeVisitor/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down