Skip to content

Fixed false-positive with by-ref parameters and array-functions #4036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 31, 2025
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
22 changes: 13 additions & 9 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5392,6 +5392,7 @@ private function processAssignVar(
}
}

$scopeBeforeAssignEval = $scope;
$scope = $result->getScope();
$truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $assignedExpr, TypeSpecifierContext::createTruthy());
$falseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $assignedExpr, TypeSpecifierContext::createFalsey());
Expand All @@ -5404,7 +5405,7 @@ private function processAssignVar(
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType);

$nodeCallback(new VariableAssignNode($var, $assignedExpr), $result->getScope());
$nodeCallback(new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval);
$scope = $scope->assignVariable($var->name, $type, $scope->getNativeType($assignedExpr), TrinaryLogic::createYes());
foreach ($conditionalExpressions as $exprString => $holders) {
$scope = $scope->addConditionalExpressions($exprString, $holders);
Expand Down Expand Up @@ -5487,6 +5488,7 @@ private function processAssignVar(
$nativeValueToWrite = $scope->getNativeType($assignedExpr);
$originalValueToWrite = $valueToWrite;
$originalNativeValueToWrite = $nativeValueToWrite;
$scopeBeforeAssignEval = $scope;

// 3. eval assigned expr
$result = $processExprCallback($scope);
Expand Down Expand Up @@ -5542,11 +5544,11 @@ private function processAssignVar(

if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
if ($var instanceof Variable && is_string($var->name)) {
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scope);
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
} else {
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
$scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString());
}
Expand Down Expand Up @@ -5574,9 +5576,9 @@ private function processAssignVar(
}
} else {
if ($var instanceof Variable) {
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scope);
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
} elseif ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
$scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString());
}
Expand Down Expand Up @@ -5611,6 +5613,7 @@ static function (): void {
$scope = $propertyNameResult->getScope();
}

$scopeBeforeAssignEval = $scope;
$result = $processExprCallback($scope);
$hasYield = $hasYield || $result->hasYield();
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
Expand All @@ -5625,7 +5628,7 @@ static function (): void {
if ($propertyName !== null && $propertyHolderType->hasProperty($propertyName)->yes()) {
$propertyReflection = $propertyHolderType->getProperty($propertyName, $scope);
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
if ($propertyReflection->canChangeTypeAfterAssignment()) {
if ($propertyReflection->hasNativeType()) {
$propertyNativeType = $propertyReflection->getNativeType();
Expand Down Expand Up @@ -5671,7 +5674,7 @@ static function (): void {
} else {
// fallback
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
// simulate dynamic property assign by __set to get throw points
if (!$propertyHolderType->hasMethod('__set')->no()) {
Expand Down Expand Up @@ -5705,6 +5708,7 @@ static function (): void {
$scope = $propertyNameResult->getScope();
}

$scopeBeforeAssignEval = $scope;
$result = $processExprCallback($scope);
$hasYield = $hasYield || $result->hasYield();
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
Expand All @@ -5714,7 +5718,7 @@ static function (): void {
if ($propertyName !== null) {
$propertyReflection = $scope->getPropertyReflection($propertyHolderType, $propertyName);
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
if ($propertyReflection->hasNativeType()) {
$propertyNativeType = $propertyReflection->getNativeType();
Expand Down Expand Up @@ -5745,7 +5749,7 @@ static function (): void {
} else {
// fallback
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scopeBeforeAssignEval);
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
}
} elseif ($var instanceof List_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class TypesAssignedToPropertiesRuleTest extends RuleTestCase

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;

protected function getRule(): Rule
{
return new TypesAssignedToPropertiesRule(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, false, false, true), new PropertyReflectionFinder());
return new TypesAssignedToPropertiesRule(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true), new PropertyReflectionFinder());
}

public function testTypesAssignedToProperties(): void
Expand Down Expand Up @@ -779,4 +781,35 @@ public function testPropertyHooks(): void
]);
}

public function testBug13093c(): void
{
$this->analyse([__DIR__ . '/data/bug-13093c.php'], []);
}

public function testBug13093d(): void
{
$this->analyse([__DIR__ . '/data/bug-13093d.php'], []);
}

public function testBug8825(): void
{
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-8825.php'], []);
}

public function testBug7844(): void
{
$this->analyse([__DIR__ . '/data/bug-7844.php'], []);
}

public function testBug7844b(): void
{
$this->analyse([__DIR__ . '/data/bug-7844b.php'], []);
}

public function testBug12675(): void
{
$this->analyse([__DIR__ . '/data/bug-12675.php'], []);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12675.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug12675;

class HelloWorld
{
private string $username = "";
private string $domain = "";

public function with_shift(string $email): void
{
$pieces = explode("@", $email);
if (2 !== count($pieces)) {

throw new \Exception("Bad, very bad...");
}

$this->username = array_shift($pieces);
$this->domain = array_shift($pieces);

echo "{$this->username}@{$this->domain}";
}

public function with_pop(string $email): void
{
$pieces = explode("@", $email);
if (2 !== count($pieces)) {

throw new \Exception("Bad, very bad...");
}

$this->domain = array_pop($pieces);
$this->username = array_pop($pieces);

echo "{$this->username}@{$this->domain}";
}
}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13093c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Bug13093c;

use function array_shift;

final class ParallelProcessRunner
{
/**
* @var array<int, string>
*/
private array $nextMutantProcessKillerContainer = [];

private string $prop;

public function fillBucketOnce(array &$killer): int
{
if ($this->nextMutantProcessKillerContainer !== []) {
$this->prop = array_shift($this->nextMutantProcessKillerContainer);
}

return 0;
}

}

final class ParallelProcessRunner2
{
/**
* @var array<int, string>
*/
private array $nextMutantProcessKillerContainer = [];

private string $prop;

public function fillBucketOnce(array &$killer): int
{
$name = 'prop';
if ($this->nextMutantProcessKillerContainer !== []) {
$this->{$name} = array_shift($this->nextMutantProcessKillerContainer);
}

return 0;
}

}

50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13093d.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Bug13093d;

use function array_shift;

final class ParallelProcessRunner
{
/**
* @var array<int, string>
*/
private array $nextMutantProcessKillerContainer = [];

static private string $prop;

public function fillBucketOnce(array &$killer): int
{
if ($this->nextMutantProcessKillerContainer !== []) {
self::$prop = array_shift($this->nextMutantProcessKillerContainer);
}

return 0;
}

}

final class ParallelProcessRunner2
{
/**
* @var array<int, string>
*/
private array $nextMutantProcessKillerContainer = [];

static private string $prop;

public function fillBucketOnce(array &$killer): int
{
$name = 'prop';
if ($this->nextMutantProcessKillerContainer !== []) {
self::${$name} = array_shift($this->nextMutantProcessKillerContainer);
}

return 0;
}

}


20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7844.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug7844;

class C
{
/** @var float */
public $val = .0;

/** @var array<float> */
public $data = array();

public function foo(): void
{
if (count($this->data) > 0) {
$this->val = array_shift($this->data);
}
}
}

39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7844b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Bug7844b;

class Obj {}

class HelloWorld
{
public Obj $p1;
public Obj $p2;
public Obj $p3;
public Obj $p4;
public Obj $p5;

/** @param non-empty-list<Obj> $objs */
public function __construct(array $objs)
{
\assert($objs !== []);
$this->p1 = $objs[0];

\assert($objs !== []);
$this->p2 = $objs[array_key_last($objs)];

\assert($objs !== []);
$this->p3 = \array_pop($objs);

\assert($objs !== []);
$this->p4 = \array_shift($objs);

\assert($objs !== []);
$p = \array_shift($objs);
$this->p5 = $p;

\assert($objs !== []);
$this->doSomething(\array_pop($objs));
}

private function doSomething(Obj $obj): void {}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-8825.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug8825;

class Endboss
{

private bool $isBool;

/**
* @param mixed[] $actionParameters
*/
public function __construct(
array $actionParameters
)
{
$this->isBool = $actionParameters['my_key'] ?? false;
}

public function use(): void
{
$this->isBool->someMethod();
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/ParameterOutAssignedTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public function testBenevolentArrayKey(): void
$this->analyse([__DIR__ . '/data/benevolent-array-key.php'], []);
}

public function testBug13093(): void
{
$this->analyse([__DIR__ . '/data/bug-13093.php'], []);
}

public function testBug13093b(): void
{
$this->analyse([__DIR__ . '/data/bug-13093b.php'], []);
}

}
Loading
Loading