Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -1911,6 +1912,13 @@ public function restoreThis(self $restoreThisScope): self

$nativeExpressionTypes[$exprString] = $expressionTypeHolder;
}
} elseif (isset($restoreThisScope->expressionTypes['$this'])) {
$expressionTypes['$this'] = $restoreThisScope->expressionTypes['$this'];
if (isset($restoreThisScope->nativeExpressionTypes['$this'])) {
$nativeExpressionTypes['$this'] = $restoreThisScope->nativeExpressionTypes['$this'];
} else {
unset($nativeExpressionTypes['$this']);
}
} else {
unset($expressionTypes['$this']);
unset($nativeExpressionTypes['$this']);
Expand Down Expand Up @@ -2107,6 +2115,10 @@ public function enterAnonymousFunctionWithoutReflection(
$expressionTypes[$exprString] = $typeHolder;
}
}
} elseif (!$closure->static && !$this->isInClosureBind()) {
$node = new Variable('this');
$expressionTypes['$this'] = ExpressionTypeHolder::createYes($node, new ObjectWithoutClassType());
$nativeTypes['$this'] = ExpressionTypeHolder::createYes($node, new ObjectWithoutClassType());
}

$filteredConditionalExpressions = [];
Expand Down Expand Up @@ -2251,6 +2263,8 @@ public function enterArrowFunctionWithoutReflection(Expr\ArrowFunction $arrowFun

if ($arrowFunction->static) {
$arrowFunctionScope = $arrowFunctionScope->invalidateExpression(new Variable('this'));
} elseif (!$this->hasVariableType('this')->yes() && !$this->isInClosureBind()) {
$arrowFunctionScope = $arrowFunctionScope->assignVariable('this', new ObjectWithoutClassType(), new ObjectWithoutClassType(), TrinaryLogic::createYes());
}

return $this->scopeFactory->create(
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-1348.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Bug1348Types;

use function PHPStan\Testing\assertType;

$closure = function () {
assertType('object', $this);
};

$arrow = fn() => assertType('object', $this);

class Foo
{
public function doFoo(): void
{
$closure = function () {
assertType('$this(Bug1348Types\Foo)', $this);
};

$arrow = fn() => assertType('$this(Bug1348Types\Foo)', $this);
}
}

$bound = \Closure::bind(
function () {
assertType('stdClass', $this);
},
new \stdClass(),
\stdClass::class
);
18 changes: 9 additions & 9 deletions tests/PHPStan/Analyser/nsrt/param-closure-this.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,36 @@ public function interplayWithProcessImmediatelyCalledCallable2(): void
}

function (Foo $f): void {
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(function () {
assertType(Some::class, $this);
});
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(static function () {
assertType('*ERROR*', $this);
});
assertType('*ERROR*', $this);
assertType('object', $this);
};

function (Foo $f): void {
$a = 1;
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(function () use (&$a) {
assertType(Some::class, $this);
});
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(static function () use (&$a) {
assertType('*ERROR*', $this);
});
assertType('*ERROR*', $this);
assertType('object', $this);
};

function (Foo $f): void {
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(fn () => assertType(Some::class, $this));
assertType('*ERROR*', $this);
assertType('object', $this);
$f->paramClosureClass(static fn () => assertType('*ERROR*', $this));
assertType('*ERROR*', $this);
assertType('object', $this);
};

class Bar extends Foo
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Debug/DebugScopeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public function testRuleInPhpStanNamespace(): void
'$a (Yes): int',
'$b (Yes): int',
'$debug (Yes): bool',
'$this (Yes): object',
'native $a (Yes): int',
'native $b (Yes): int',
'native $debug (Yes): bool',
'native $this (Yes): object',
]),
10,
],
Expand All @@ -40,10 +42,12 @@ public function testRuleInPhpStanNamespace(): void
'$a (Yes): int',
'$b (Yes): int',
'$debug (Yes): bool',
'$this (Yes): object',
'$c (Maybe): 1',
'native $a (Yes): int',
'native $b (Yes): int',
'native $debug (Yes): bool',
'native $this (Yes): object',
'native $c (Maybe): 1',
'condition about $c #1: if $debug=false then $c is *ERROR* (No)',
'condition about $c #2: if $debug=true then $c is 1 (Yes)',
Expand All @@ -58,7 +62,9 @@ public function testPr4663(): void
$this->analyse([__DIR__ . '/data/pr-4663.php'], [
[
implode("\n", [
'$this (Yes): object',
"\$result (Yes): 'no matches!'",
'native $this (Yes): object',
"native \$result (Yes): 'no matches!'",
]),
11,
Expand Down
22 changes: 18 additions & 4 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,6 @@ public function testFormerThisVariableRule(): void
'Undefined variable: $this',
20,
],
[
'Undefined variable: $this',
26,
],
[
'Undefined variable: $this',
38,
Expand Down Expand Up @@ -1562,4 +1558,22 @@ public function testBug10729(): void
]);
}

public function testBug1348(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-1348.php'], [
[
'Undefined variable: $this',
25,
],
[
'Undefined variable: $this',
28,
],
]);
}

}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-1348.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug1348;

$closure = function () {
$this->foo = 'bar';
};

$object = new \stdClass();

\Closure::bind($closure, $object, $object)();
\Closure::bind(
function () {
$this->foo = 'bar';
},
$object,
$object
)();

// arrow function case
$arrow = fn() => $this;

// static closures should still report $this as undefined
static function () {
$this->foo = 'bar';
};

static fn() => $this;
Loading