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
12 changes: 7 additions & 5 deletions src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,24 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $argsResult->getScope();

if ($methodReflection !== null) {
$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
if ($parametersAcceptor !== null) {
$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
}

if ($methodReflection->getName() === '__construct' || $methodReflection->hasSideEffects()->yes()) {
$nodeScopeResolver->callNodeCallback($nodeCallback, new InvalidateExprNode($normalizedExpr->var), $scope, $storage);
$scope = $scope->invalidateExpression($normalizedExpr->var, true, $methodReflection->getDeclaringClass());
} elseif ($this->rememberPossiblyImpureFunctionValues && $methodReflection->hasSideEffects()->maybe() && !$methodReflection->getDeclaringClass()->isBuiltin()) {
} elseif ($this->rememberPossiblyImpureFunctionValues && $methodReflection->hasSideEffects()->maybe() && !$methodReflection->getDeclaringClass()->isBuiltin() && $parametersAcceptor !== null) {
$scope = $scope->assignExpression(
new PossiblyImpureCallExpr($normalizedExpr, $normalizedExpr->var, sprintf('%s::%s()', $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName())),
$parametersAcceptor->getReturnType(),
new MixedType(),
);
}
if (!$methodReflection->isStatic()) {
if ($parametersAcceptor !== null && !$methodReflection->isStatic()) {
$selfOutType = $methodReflection->getSelfOutType();
if ($selfOutType !== null) {
$scope = $scope->assignExpression(
Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $argsResult->getScope();
$scopeFunction = $scope->getFunction();

if ($methodReflection !== null) {
if ($methodReflection !== null && $parametersAcceptor !== null) {
$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
Expand All @@ -221,6 +221,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
} elseif (
$methodReflection !== null
&& $this->rememberPossiblyImpureFunctionValues
&& $parametersAcceptor !== null
&& $scope->isInClass()
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
&& $methodReflection->hasSideEffects()->maybe()
Expand Down
70 changes: 14 additions & 56 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3205,7 +3205,7 @@
} else {
$scope = $scope->removeTypeFromExpression($expr, $type);
}
$specifiedExpressions[$typeSpecification['exprString']] = ExpressionTypeHolder::createYes($expr, $scope->getScopeType($expr));
$specifiedExpressions[$typeSpecification['exprString']] = ExpressionTypeHolder::createYes($expr, $scope->getType($expr));
}

$conditions = [];
Expand Down Expand Up @@ -3337,7 +3337,7 @@
return $this->inFirstLevelStatement;
}

public function mergeWith(?self $otherScope, bool $preserveVacuousConditionals = false): self
public function mergeWith(?self $otherScope): self
{
if ($otherScope === null || $this === $otherScope) {
return $this;
Expand All @@ -3347,18 +3347,6 @@

$mergedExpressionTypes = $this->mergeVariableHolders($ourExpressionTypes, $theirExpressionTypes);
$conditionalExpressions = $this->intersectConditionalExpressions($otherScope->conditionalExpressions);
if ($preserveVacuousConditionals) {
$conditionalExpressions = $this->preserveVacuousConditionalExpressions(
$conditionalExpressions,
$this->conditionalExpressions,
$theirExpressionTypes,
);
$conditionalExpressions = $this->preserveVacuousConditionalExpressions(
$conditionalExpressions,
$otherScope->conditionalExpressions,
$ourExpressionTypes,
);
}
$conditionalExpressions = $this->createConditionalExpressions(
$conditionalExpressions,
$ourExpressionTypes,
Expand Down Expand Up @@ -3464,48 +3452,6 @@
return $newConditionalExpressions;
}

/**
* @param array<string, ConditionalExpressionHolder[]> $currentConditionalExpressions
* @param array<string, ConditionalExpressionHolder[]> $sourceConditionalExpressions
* @param array<string, ExpressionTypeHolder> $otherExpressionTypes
* @return array<string, ConditionalExpressionHolder[]>
*/
private function preserveVacuousConditionalExpressions(
array $currentConditionalExpressions,
array $sourceConditionalExpressions,
array $otherExpressionTypes,
): array
{
foreach ($sourceConditionalExpressions as $exprString => $holders) {
foreach ($holders as $key => $holder) {
if (isset($currentConditionalExpressions[$exprString][$key])) {
continue;
}

$typeHolder = $holder->getTypeHolder();
if ($typeHolder->getCertainty()->no() && !$typeHolder->getExpr() instanceof Variable) {
continue;
}

foreach ($holder->getConditionExpressionTypeHolders() as $guardExprString => $guardTypeHolder) {
if (!array_key_exists($guardExprString, $otherExpressionTypes)) {
continue;
}

$otherType = $otherExpressionTypes[$guardExprString]->getType();
$guardType = $guardTypeHolder->getType();

if ($otherType->isSuperTypeOf($guardType)->no()) {
$currentConditionalExpressions[$exprString][$key] = $holder;
break;
}
}
}
}

return $currentConditionalExpressions;
}

/**
* @param array<string, ConditionalExpressionHolder[]> $newConditionalExpressions
* @param array<string, ConditionalExpressionHolder[]> $existingConditionalExpressions
Expand Down Expand Up @@ -3603,6 +3549,12 @@
}

foreach ($variableTypeGuards as $guardExprString => $guardHolder) {
if (
array_key_exists($guardExprString, $theirExpressionTypes)
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()

Check warning on line 3554 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ foreach ($variableTypeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }

Check warning on line 3554 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ foreach ($variableTypeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }
) {
continue;
}
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], $holder);
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
}
Expand All @@ -3614,6 +3566,12 @@
}

foreach ($typeGuards as $guardExprString => $guardHolder) {
if (
array_key_exists($guardExprString, $theirExpressionTypes)
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()

Check warning on line 3571 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ foreach ($typeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->yes() ) { continue; }

Check warning on line 3571 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ foreach ($typeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }

Check warning on line 3571 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ foreach ($typeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && $guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->yes() ) { continue; }

Check warning on line 3571 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ foreach ($typeGuards as $guardExprString => $guardHolder) { if ( array_key_exists($guardExprString, $theirExpressionTypes) - && !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no() + && !$theirExpressionTypes[$guardExprString]->getType()->isSuperTypeOf($guardHolder->getType())->no() ) { continue; }
) {
continue;
}
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], new ExpressionTypeHolder($mergedExprTypeHolder->getExpr(), new ErrorType(), TrinaryLogic::createNo()));
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ public function processStmtNode(
$throwPoints = array_merge($throwPoints, $branchScopeStatementResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $branchScopeStatementResult->getImpurePoints());
$branchScope = $branchScopeStatementResult->getScope();
$finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope, true);
$finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope);
$alwaysTerminating = $alwaysTerminating && $branchScopeStatementResult->isAlwaysTerminating();
if (count($branchScopeStatementResult->getEndStatements()) > 0) {
$endStatements = array_merge($endStatements, $branchScopeStatementResult->getEndStatements());
Expand All @@ -1170,7 +1170,7 @@ public function processStmtNode(

if ($stmt->else === null) {
if (!$ifAlwaysTrue && !$lastElseIfConditionIsTrue) {
$finalScope = $scope->mergeWith($finalScope, true);
$finalScope = $scope->mergeWith($finalScope);
$alwaysTerminating = false;
}
} else {
Expand All @@ -1182,7 +1182,7 @@ public function processStmtNode(
$throwPoints = array_merge($throwPoints, $branchScopeStatementResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $branchScopeStatementResult->getImpurePoints());
$branchScope = $branchScopeStatementResult->getScope();
$finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope, true);
$finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope);
$alwaysTerminating = $alwaysTerminating && $branchScopeStatementResult->isAlwaysTerminating();
if (count($branchScopeStatementResult->getEndStatements()) > 0) {
$endStatements = array_merge($endStatements, $branchScopeStatementResult->getEndStatements());
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ private function specifyTypesFromCallableCall(TypeSpecifierContext $context, Fun
}
}

if ($assertions === null || $assertions->getAll() === []) {
if ($assertions === null || $assertions->getAll() === [] || $parametersAcceptor === null) {
return null;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14411.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14411;

use function PHPStan\Testing\assertType;

/** @phpstan-impure */
function get_mixed(): mixed {
return random_int(0, 1) ? 'foo' : null;
}

/** @phpstan-impure */
function get_optional_int(): ?int {
return random_int(0, 1) ? 42 : null;
}

function (): void {
$a = get_mixed();

if ($a !== null) {
$b = $a;
}
else {
$b = get_optional_int();
}
if ($b !== null) {
assertType('mixed', $a);
if ($a === null) {
echo 'this is absolutely possible';
}
}
};
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-5051.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testWithBooleans($data): void
assertType('1|2|3|10', $data);
assertType('bool', $update);
} else {
assertType('1|2', $data);
assertType('1|2|3|10', $data);
assertType('bool', $update);
}

Expand All @@ -81,15 +81,15 @@ public function testWithBooleans($data): void

if ($data === 3) {
assertType('bool', $update);
assertType('true', $foo);
assertType('bool', $foo);
} else {
assertType('bool', $update);
assertType('bool', $foo);
}

if ($data === 1 || $data === 2) {
assertType('bool', $update);
assertType('false', $foo);
assertType('bool', $foo);
} else {
assertType('bool', $update);
assertType('bool', $foo);
Expand Down
57 changes: 36 additions & 21 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,12 @@ public function testBug4173(): void
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-4173.php'], []);
$this->analyse([__DIR__ . '/data/bug-4173.php'], [
[
'Variable $value might not be defined.', // could be fixed
30,
],
]);
}

public function testBug5805(): void
Expand Down Expand Up @@ -1114,13 +1119,29 @@ public function testDynamicAccess(): void
18,
],
[
'Undefined variable: $bar',
'Variable $foo might not be defined.',
36,
],
[
'Variable $foo might not be defined.',
37,
],
[
'Variable $bar might not be defined.',
38,
],
[
'Undefined variable: $foo',
'Variable $bar might not be defined.',
40,
],
[
'Variable $foo might not be defined.',
41,
],
[
'Variable $bar might not be defined.',
42,
],
[
'Undefined variable: $buz',
44,
Expand All @@ -1137,6 +1158,14 @@ public function testDynamicAccess(): void
'Undefined variable: $buz',
49,
],
[
'Variable $bar might not be defined.',
49,
],
[
'Variable $foo might not be defined.',
49,
],
[
'Variable $foo might not be defined.',
50,
Expand Down Expand Up @@ -1458,24 +1487,6 @@ public function testBug13920(): void
$this->analyse([__DIR__ . '/data/bug-13920.php'], []);
}

public function testBug12992(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-12992.php'], []);
}

public function testBug14227(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-14227.php'], []);
}

public function testBug14117(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand All @@ -1484,6 +1495,10 @@ public function testBug14117(): void
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-14117.php'], [
[
'Variable $value might not be defined.',
33,
],
[
'Variable $value might not be defined.',
49,
Expand Down
40 changes: 0 additions & 40 deletions tests/PHPStan/Rules/Variables/data/bug-12992.php

This file was deleted.

Loading
Loading