Skip to content

[v0.2.2] DefinedSymbolCollector did not return traits and namespaces #136

@llaville

Description

@llaville

Describe the bug

The DefinedSymbolCollector did not return user traits and namespaces

Additional information

With these data source files

data/interfaces.php

<?php
// @link https://www.php.net/manual/en/language.oop5.interfaces.php

interface A
{
    public function foo();
}

interface B extends A
{
    public function baz(Baz $baz);
}

// This will work
class C implements B
{
    public function foo()
    {
    }

    public function baz(Baz $baz)
    {
    }
}

// This will not work and result in a fatal error
class D implements B
{
    public function foo()
    {
    }

    public function baz(Foo $foo)
    {
    }
}

data/namespaces.php

<?php
// @link https://www.php.net/manual/en/language.namespaces.importing.php

namespace foo;

use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

// importing a function
use function My\Full\functionName;

// aliasing a function
use function My\Full\functionName as func;

// importing a constant
use const My\Full\CONSTANT;

data/traits.php

<?php
// @link https://www.php.net/manual/en/language.oop5.traits.php

trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}

And this script test :

<?php

use ComposerUnused\SymbolParser\Parser\PHP\ConsumedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\DefineStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\Finder;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/DefineStrategy.php';

$dataSource = __DIR__ . '/data';

$strategies = [
//    new DefineStrategy(),
];

$finder = new Finder();
$finder
    ->files()
    ->in($dataSource)
    ->name('/\\.(php|inc|phtml)$/');

if (empty($strategies)) {
    $symbolCollector = new DefinedSymbolCollector();
} else {
    $symbolCollector = new ConsumedSymbolCollector($strategies);
}

$symbolParser = new SymbolNameParser(
    (new ParserFactory())->createForNewestSupportedVersion(),
    new NameResolver(),
    $symbolCollector
);

foreach ($finder as $fileInfo) {
    $taskId = $fileInfo->getRelativePathname();
    $symbols = iterator_to_array(
        $symbolParser->parseSymbolNames($fileInfo->getContents())
    );
    printf('Task %s found %d symbol(s)'. PHP_EOL, $taskId, count($symbols));
    var_export($symbols);
    echo PHP_EOL;
}

Expected results :

Task interfaces.php found 4 symbol(s)
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
)
Task traits.php found 3 symbol(s)
array (
  0 => 'ezcReflectionReturnInfo',
  1 => 'ezcReflectionMethod',
  2 => 'ezcReflectionFunction',
)
Task namespaces.php found 1 symbol(s)
array (
  0 => 'foo',
)

Current results :

Task interfaces.php found 4 symbol(s)
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
)
Task traits.php found 2 symbol(s)
array (
  0 => 'ezcReflectionMethod',
  1 => 'ezcReflectionFunction',
)
Task namespaces.php found 0 symbol(s)
array (
)

NOTE : The DefineStrategy is the solution to this issue. But I can't open a Feature Request (see #135 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions