microsoft / microsoft/tolerant-php-parser
Incorrectly identified usage of $this in static method
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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