microsoft / microsoft/tolerant-php-parser

Incorrectly identified usage of $this in static method

Open
#273 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
894
Forks
85
Avg merge
2d 13h
Merged PRs (30d)
3

Description

Example:

<?php

class Foo {
    private $bar = 'baz';
}

class Test  {
    public static function run () {
        $getter  = function () { return $this->bar; };
        $closure = Closure::bind($getter, new Foo(), Foo::class);
        return $closure();
    }
}

echo Test::run() . "\r\n";

Reports error $this can not be used in static methods.:

{
    "resource": "/closure.php",
    "owner": "_generated_diagnostic_collection_name_#2",
    "severity": 8,
    "message": "$this can not be used in static methods.",
    "source": "php",
    "startLineNumber": 9,
    "startColumn": 41,
    "endLineNumber": 9,
    "endColumn": 46
}

Code itself is valid:

$ php -l closure.php 
No syntax errors detected in closure.php
$ php closure.php 
baz

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the closure.php example, focusing on the closure using $this inside the static method and Closure::bind. Compare the parser's reported error with php -l and php closure.php; done means the valid example no longer produces the false $this can not be used in static methods. diagnostic while still outputting baz.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.