Replies: 6 comments
-
Source code of my CAUTION works only if <?php
declare(strict_types=1);
namespace ComposerUnused\SymbolParser\Parser\PHP;
use PhpParser\Node;
use SplObjectStorage;
use function get_class;
/**
* @author Laurent Laville
*/
final class NodeSymbolCollector extends ConsumedSymbolCollector
{
private SplObjectStorage $nodeStack;
public function __construct(array $properties)
{
parent::__construct($properties);
$this->nodeStack = new SplObjectStorage();
}
public function getHandledNodes(): array
{
$handled = [];
foreach ($this->nodeStack as $node) {
[$symbols, $strategy] = $this->nodeStack->getInfo();
if (empty($symbols)) {
continue;
}
$handled[] = [
'symbols' => $symbols,
'strategy' => $strategy,
'nodeType' => $node->getType(),
'nodeAttributes' => $node->getAttributes(),
];
}
return $handled;
}
public function getNotHandledNodes(): array
{
$handled = [];
foreach ($this->nodeStack as $node) {
[$symbols, $strategy] = $this->nodeStack->getInfo();
if (!empty($symbols)) {
continue;
}
$handled[] = [
'strategy' => $strategy,
'nodeType' => $node->getType(),
'nodeAttributes' => $node->getAttributes(),
];
}
return $handled;
}
public function enterNode(Node $node)
{
$symbols = [];
$this->followIncludes($node);
foreach ($this->strategies as $strategy) {
if (!$strategy->canHandle($node)) {
continue;
}
$symbols[] = $collectedSymbols = $strategy->extractSymbolNames($node);
$this->nodeStack->attach($node, [$collectedSymbols, get_class($strategy)]);
}
if (count($symbols) > 0) {
$this->symbols = array_merge($this->symbols, ...$symbols);
}
return null;
}
} |
Beta Was this translation helpful? Give feedback.
-
Love the idea. I didn't spend much time to give some deeper thoughts into it, but will get to it. What came to mind with the changes you did was some kind of using the symbol-parser to identify used or unused code if your own application. (composer-unused/unused-code, so to say) Meaning to find dead code for symbols your code base defines but never uses. |
Beta Was this translation helpful? Give feedback.
-
This is not really my goal. Here is the patch (adding a new method to return full AST after analyse)
|
Beta Was this translation helpful? Give feedback.
-
Forget about my previous comment on new method |
Beta Was this translation helpful? Give feedback.
-
FYI: I've put all my contributions combined into the |
Beta Was this translation helpful? Give feedback.
-
I'll close this discussion because I've found a better solution that may match all needs (from default to specialized). |
Beta Was this translation helpful? Give feedback.
-
Hello @icanhazstring
My future project based on
composer-unused/symbol-parser
need a specialized collector that is able to :This is the goal of this request :
Are you agree to include it by default in your package ?
Awaiting your feedbacks
Pre-requis :
Use Case 1
Retrieve only symbols list : uses the standard
ConsumedSymbolCollector
On repo source code, with only one file scanned (
AbstractCollector
: see$dataSource
in example script below), will print something likeUse Case 2
Retrieve symbols list and what nodes defined/used them: replace the standard
ConsumedSymbolCollector
by newNodeSymbolCollector
On repo source code, will print something like
POC example script
Beta Was this translation helpful? Give feedback.
All reactions