zendframework / zend-code

BSD 3-Clause "New" or "Revised" License
1.68k stars 78 forks source link

ClassScanner produces invalid class name that only contains namespace #124

Closed bashofmann closed 7 years ago

bashofmann commented 7 years ago

We just discovered some weird case where the ClassScanner produces an invalid class name that only contains the namespace of the class instead of the full qualified class name.

Version used: 3.2.0

Minimal reproduction code:

<?php
namespace foo\bar {

    class SomeClass {

        public function someMethod() {
            $foo = 'foo';
            $label = true ? "test ${foo} test" : "test ${foo}";

            return [
                'label' => $label,
                'class' => self::class
            ];
        }
    }
}

namespace {
    require_once __DIR__ . '/vendor/autoload.php';

    $fileScanner = new \Zend\Code\Scanner\FileScanner(__FILE__);

    foreach ($fileScanner->getClasses() as $class) {
        echo $class->getName() . PHP_EOL;
    }

    print_r($fileScanner->getClassNames());
}

Expected output:

foo\bar\SomeClass
Array
(
    [0] => foo\bar\SomeClass
)

Actual output:

foo\bar\
Array
(
    [0] => foo\bar\SomeClass
)

The FileScanner actually gets the correct class name.

It works correctly if you either remove the reference to "self::class" or one of the "${foo}" usages.

Ocramius commented 7 years ago

Fairly sure that the class scanner can't deal with any PHP 5.4+ feature. The entire namespace is going to be removed soon though

On 25 Jul 2017 12:06 PM, "Bastian Hofmann" notifications@github.com wrote:

We just discovered some weird case where the ClassScanner produces an invalid class name that only contains the namespace of the class instead of the full qualified class name.

Version used: 3.2.0

Minimal reproduction code:

<?php namespace foo\bar {

class SomeClass {

    public function someMethod() {
        $foo = 'foo';
        $label = true ? "test ${foo} test" : "test ${foo}";

        return [
            'label' => $label,
            'class' => self::class
        ];
    }
}

}

namespace { require_once DIR . '/vendor/autoload.php';

$fileScanner = new \Zend\Code\Scanner\FileScanner(__FILE__);

foreach ($fileScanner->getClasses() as $class) {
    echo $class->getName() . PHP_EOL;
}

print_r($fileScanner->getClassNames());

}

Expected output:

foo\bar\SomeClass Array ( [0] => foo\bar\SomeClass )

Actual output:

foo\bar\ Array ( [0] => foo\bar\SomeClass )

The FileScanner actually gets the correct class name.

It works correctly if you either remove the reference to "self::class" or one of the "${foo}" usages.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/124, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakP_oxjNrM-z3-6fFSYHi-TZZ-SH0ks5sRb4tgaJpZM4OiVzY .

bashofmann commented 7 years ago

Is there going to be a replacement, or do you have a recommendation for something similar? I'd rather not use reflection for our use case.

djmattyg007 commented 7 years ago

@bashofmann I recommend this: https://github.com/nikic/PHP-Parser it's developed and maintained by one of the core developers of PHP itself.

Ocramius commented 7 years ago

See Roave/BetterReflection (https://github.com/Roave/BetterReflection), which doesn't cause loading of the files you are reflecting.

On 25 Jul 2017 1:46 PM, "Matthew" notifications@github.com wrote:

@bashofmann https://github.com/bashofmann I recommend this: https://github.com/nikic/PHP-Parser it's developed and maintained by one of the core developers of PHP itself.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/124#issuecomment-317712863, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakMmhuzXbsKPyW2DwBDMzmzWig_rSks5sRdWbgaJpZM4OiVzY .

djmattyg007 commented 7 years ago

The latter is built on top of the former :)

bashofmann commented 7 years ago

Ok thanks for the pointers. Since the feature is going away, I guess we can close this issue then.