Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/Helper/NonNullabilityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function ensureShallowNonNullability(MutatingScope $scope, Scope $origina
$exprToSpecify,
$exprTypeWithoutNull,
TypeCombinator::removeNull($nativeType),
TrinaryLogic::createYes(),
$certainty,
);

return new EnsuredNonNullabilityResult(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,7 @@ private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Clo

if ($expr instanceof ArrayDimFetch) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
} elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch || $expr instanceof Expr\NullsafeMethodCall) {
$scope = $this->lookForExpressionCallback($scope, $expr->var, $callback);
} elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) {
$scope = $this->lookForExpressionCallback($scope, $expr->class, $callback);
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,33 @@ public function testNullsafeIsset(): void
$this->analyse([__DIR__ . '/data/variable-nullsafe-isset.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug7291(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-7291.php'], [
[
'Variable $a might not be defined.',
23,
],
[
'Variable $b might not be defined.',
32,
],
[
'Variable $c might not be defined.',
41,
],
[
'Variable $d might not be defined.',
50,
],
]);
}

public function testBug1306(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand Down
96 changes: 96 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-7291.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php // lint >= 8.0

namespace Bug7291;

class Foo
{

public ?Foo $foo = null;

public function bar(): self
{
return $this;
}

}

function bareProp(): void
{
if (rand(0, 1)) {
$a = rand(0, 1) ? new Foo() : null;
}

echo $a?->foo; // warn
}

function bareMethod(): void
{
if (rand(0, 1)) {
$b = rand(0, 1) ? new Foo() : null;
}

echo $b?->bar(); // warn
}

function bareChain(): void
{
if (rand(0, 1)) {
$c = rand(0, 1) ? new Foo() : null;
}

echo $c?->bar()?->foo; // warn
}

function notNullableStillWarns(): void
{
if (rand(0, 1)) {
$d = new Foo();
}

echo $d?->foo; // warn
}

function propCoalesce(): void
{
if (rand(0, 1)) {
$e = rand(0, 1) ? new Foo() : null;
}

echo $e?->foo ?? 0; // no warn, ?? handles it
}

function methodCoalesce(): void
{
if (rand(0, 1)) {
$f = rand(0, 1) ? new Foo() : null;
}

echo $f?->bar() ?? 0; // no warn, ?? handles it
}

function propIsset(): void
{
if (rand(0, 1)) {
$g = rand(0, 1) ? new Foo() : null;
}

var_dump(isset($g?->foo)); // no warn, isset handles it
}

function propEmpty(): void
{
if (rand(0, 1)) {
$h = rand(0, 1) ? new Foo() : null;
}

var_dump(empty($h?->foo)); // no warn, empty handles it
}

function methodEmpty(): void
{
if (rand(0, 1)) {
$i = rand(0, 1) ? new Foo() : null;
}

var_dump(empty($i?->bar())); // no warn, empty handles it
}
Loading