zendframework / zend-code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ClassScanner produces invalid class name that only contains namespace

bashofmann opened this issue · comments

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.

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.

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

The latter is built on top of the former :)

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