`closureUsesThis` should not be reported when `@param-closure-this` is used to change the `$this`
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reproducing the provided Macroable and SpecialScope example and inspect the closureUsesThis rule. The fix is done when @param-closure-this changing the closure's $this type prevents this diagnostic for the captured outer $this, while the existing warning remains for matching types.
Written by the indexing model from the issue text.
Description
Hello!
When using macros, the $this parameter inside the provided closure is overridden using @param-closure-this:
trait Macroable
{
/**
* Register a custom macro.
*
* @param-closure-this $this $macro
*/
public static function macro(string $name, Closure $macro): void;
}
class Builder
{
use Macroable;
}
However, in some cases, the original class $this needs to be referenced inside the closure:
class SpecialScope
{
protected function addOnlyConfidential(Builder $query): void
{
$scope = $this;
$query->macro('onlyConfidential', function (Builder $query) use ($scope) {
$query->withoutGlobalScope($scope);
return $query->where($scope->column, $scope->invert ? false : true);
});
}
}
But this triggers the closureUsesThis rule:
Anonymous function uses $this assigned to variable $scope. Use $this directly in the function body.
🪪 closure.useThis
The rule probably shouldn't be triggered if the $this type outside the closure does not match the type inside the closure
- Dominant language
- PHP
- Stars
- 709
- Forks
- 62
- Avg merge
- 5d 19h
- Merged PRs (30d)
- 3
Contributor guide
No contributing guide indexed for this repository
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.
More from phpstan/phpstan-strict-rules
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
phpstan/phpstan-strict-rules#317 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
phpstan/phpstan-strict-rules#316 · 3 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
phpstan/phpstan-strict-rules#298 · 2 comments · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
phpstan/phpstan-strict-rules#289 · 1 comment ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 35/100
phpstan/phpstan-strict-rules#283 · 6 comments ·
All issues in phpstan/phpstan-strict-rules
Similar issues
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nextcloud/fulltextsearch#1011 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
phpstan/phpstan-doctrine#794 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Automattic/static-site-importer#1767 ·