Skip to content

Catch use of $$this #4710

@ancarda

Description

@ancarda

Feature request

A colleague of mine caught a bug that used $$this->methodCall(). I know the phpstan/phpstan-strict-rules plugin can ban the use of $$, however I think PHPStan, out of the box, should prohibit $$this. Given $this always refers to an instance of a class, and not a string, it can't possibly be used as the lookup for something on the symbol table.

I played around with this in 3v4l.org, $$this just produces NULL, which PHPStan (0.12.81) does not pickup on to stop the method call. Consider the following code (available at https://3v4l.org/mhr52):

<?php

declare(strict_types=1);

final class Greeter
{
    public function greet(): void
    {
        echo $$this->greetText();
    }

    public function greetText(): string
    {
        return 'Hello World';
    }
}

$g = new Greeter();
$g->greet();

You will get the following PHP error when trying to run:

Fatal error: Uncaught Error: Object of class Greeter could not be converted to string in /in/mhr52:9

However, PHPStan (with --level=max) will not report any issues.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions