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

reflectionClass attribute missing on a ClassMethod in an anonymous class #543

Closed
sanmai opened this issue Oct 12, 2018 · 4 comments
Closed

Comments

@sanmai
Copy link

sanmai commented Oct 12, 2018

Consider this code:

function create_date(string $format, string $time): DateTime
{
    return (new class() extends DateTime {
        public static function createFromFormat(string $format, string $time)
        {
            return parent::createFromFormat($format, $time);
        }
    })::createFromFormat($format, $time);
}

createFromFormat follows DateTime's interface in that anonymous class, yet that can't be seen from a reflectionClass attribute as it is missing (NULL).

Complete reproduction case:

<?php
include 'vendor/autoload.php';

$code = <<<'CODE'
<?php

function create_date(string $format, string $time): DateTime
{
    return (new class() extends DateTime {
        public static function createFromFormat(string $format, string $time)
        {
            return parent::createFromFormat($format, $time);
        }
    })::createFromFormat($format, $time);
}

CODE;

$traverser = new PhpParser\NodeTraverser();

$traverser->addVisitor(new class() extends PhpParser\NodeVisitorAbstract {
    public function leaveNode(PhpParser\Node $node): void
    {
        if ($node instanceof PhpParser\Node\Stmt\ClassMethod && $node->isPublic()) {
            echo (new PhpParser\PrettyPrinter\Standard())->prettyPrintFile([$node]);
            $reflection = $node->getAttribute('reflectionClass');

            if ($reflection === null) {
                echo "\n\nNo reflection for a class method!\n";
                exit(1);
            }
        }
    }
});

$traverser->traverse((new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::PREFER_PHP7)->parse($code));

Seems to be relevant to other classes, not just for DateTime.

First seen at infection/infection#501

sanmai added a commit to sanmai/infection that referenced this issue Oct 12, 2018
…reflection

It seems that PHP-Parser can't provide reflection for anonymous classes yet.

Fixes infection#501 for time being until nikic/PHP-Parser#543 gets fixed.
@nikic
Copy link
Owner

nikic commented Oct 12, 2018

The reflectionClass attribute is not being added by php-parser. My guess would be that https://github.com/Roave/BetterReflection adds this.

@sanmai
Copy link
Author

sanmai commented Oct 12, 2018

Oh, thanks! I'll be looking towards there then.

@sanmai sanmai closed this as completed Oct 12, 2018
@maks-rafalko
Copy link
Contributor

@sanmai
Copy link
Author

sanmai commented Oct 12, 2018

Oh, better and better! This is all my fault! 🤦‍♂️

maks-rafalko pushed a commit to infection/infection that referenced this issue Oct 13, 2018
…reflection (#502)

* Update ProtectedVisibility/PublicVisibility to guard against missing reflection

It seems that PHP-Parser can't provide reflection for anonymous classes yet.

Fixes #501 for time being until nikic/PHP-Parser#543 gets fixed.

* PublicVisibility: test for presence of reflection in one place

* Update type hint to include null
BackEndTea pushed a commit to BackEndTea/infection that referenced this issue Oct 22, 2018
…reflection (infection#502)

* Update ProtectedVisibility/PublicVisibility to guard against missing reflection

It seems that PHP-Parser can't provide reflection for anonymous classes yet.

Fixes infection#501 for time being until nikic/PHP-Parser#543 gets fixed.

* PublicVisibility: test for presence of reflection in one place

* Update type hint to include null
raphaelstolt pushed a commit to raphaelstolt/infection that referenced this issue Nov 2, 2018
…reflection (infection#502)

* Update ProtectedVisibility/PublicVisibility to guard against missing reflection

It seems that PHP-Parser can't provide reflection for anonymous classes yet.

Fixes infection#501 for time being until nikic/PHP-Parser#543 gets fixed.

* PublicVisibility: test for presence of reflection in one place

* Update type hint to include null
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

3 participants