Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
llaville opened this issue Feb 22, 2024 · 4 comments
Closed

Comments

@llaville
Copy link
Contributor

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 )

@llaville
Copy link
Contributor Author

Discussion about new DefineStrategy was just opened at #137

@icanhazstring
Copy link
Member

@llaville is this working already?
Lost a track a bit :)

@llaville
Copy link
Contributor Author

llaville commented Mar 8, 2024

@icanhazstring Of course the problem is still alive !

Reason is explained because, when I've wrote the first version of DefineStrategy (introduced by feature at #137), I've just pick code from DefinedSymbolCollector of v0.2.2

Now, compare with original source code, that check only for classes and interfaces, and my own implementation into DefineStrategy that check all family -- thats include class, interface, trait and enum.

llaville added a commit to llaville/symbol-parser that referenced this issue Mar 9, 2024
@llaville llaville mentioned this issue Mar 9, 2024
5 tasks
icanhazstring pushed a commit that referenced this issue Mar 9, 2024
@llaville
Copy link
Contributor Author

Fix included in release 0.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants