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: 2 additions & 0 deletions src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
'property assignment',
true,
);
$invalidateExpressions[] = new InvalidateExprNode($node->getPropertyFetch());
},
ExpressionContext::createDeep(),
);
Expand Down Expand Up @@ -257,6 +258,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
'property assignment',
true,
);
$invalidateExpressions[] = new InvalidateExprNode($node->getPropertyFetch());
return;
}

Expand Down
10 changes: 7 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ public function processClosureNode(
'property assignment',
true,
);
$invalidateExpressions[] = new InvalidateExprNode($node->getPropertyFetch());
return;
}
if ($node instanceof ExecutionEndNode) {
Expand Down Expand Up @@ -2849,7 +2850,8 @@ public function processImmediatelyCalledCallable(MutatingScope $scope, array $in
continue;
}

$scope = $scope->invalidateExpression($invalidateExpression->getExpr(), true);
$requireMoreCharacters = $invalidateExpression->getExpr() instanceof Variable;
$scope = $scope->invalidateExpression($invalidateExpression->getExpr(), $requireMoreCharacters);
}

return $scope;
Expand Down Expand Up @@ -3368,7 +3370,9 @@ public function processArgs(
$scope = $scope->restoreThis($restoreThisScope);
}

$deferredInvalidateExpressions[] = [$invalidateExpressions, $uses];
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
$deferredInvalidateExpressions[] = [$invalidateExpressions, $uses];
}
} elseif ($arg->value instanceof Expr\ArrowFunction) {
if (
$closureBindScope === null
Expand Down Expand Up @@ -3416,8 +3420,8 @@ public function processArgs(
if ($exprType->isCallable()->yes()) {
$acceptors = $exprType->getCallableParametersAcceptors($scope);
if (count($acceptors) === 1) {
$deferredInvalidateExpressions[] = [$acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables()];
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
$deferredInvalidateExpressions[] = [$acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables()];
$callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $acceptors[0]->getThrowPoints());
if (!$this->implicitThrows) {
$callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit()));
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ public function testBug8926(): void
$this->analyse([__DIR__ . '/data/bug-8926.php'], []);
}

public function testBug11417(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-11417.php'], [
[
'If condition is always true.',
66,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

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

#[RequiresPhp('>= 8.0')]
public function testBug13384b(): void
{
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-10903.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug10903;

class HelloWorld
{
public bool $hasFooKey = false;

public function sayHello(): void
{
/** @var array<string, string> $arr */
$arr = ['foo' => 'bar'];
$this->hasFooKey = false;

$filteredArr = \array_filter($arr, function ($value, $key) {
if (\stripos($key, 'foo') !== false) {
$this->hasFooKey = true;

return false;
}

return true;
}, ARRAY_FILTER_USE_BOTH);

if ($this->hasFooKey) {
// do something
}
}
}
72 changes: 72 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11417.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Bug11417;

class Wrap {
/**
* @param-immediately-invoked-callable $cb
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the same test with later-invoked-callable ; $this->conn should not be invalidate for later-invoked-callable.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make tests completed successfully as well — all tests pass. The commit has already been pushed.

*/
public static function run(callable $cb): void
{
$cb();
}
}

class HelloWorld
{
private ?string $conn = null;

public function getConn(): string
{
if (!is_null($this->conn)) {
return $this->conn;
}

Wrap::run(function() {
$this->conn = "conn";
});

if (is_null($this->conn)) {
throw new \Exception("conn failed");
}

return $this->conn;
}

public function disc(): void
{
$this->conn = null;
}
}

class WrapLater {
/**
* @param-later-invoked-callable $cb
*/
public static function run(callable $cb): void
{
$cb();
}
}

class HelloWorldLater
{
private ?string $conn = null;

public function getConn(): string
{
if (!is_null($this->conn)) {
return $this->conn;
}

WrapLater::run(function() {
$this->conn = "conn";
});

if (is_null($this->conn)) {
throw new \Exception("conn failed");
}

return $this->conn;
}
}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8926.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,38 @@ function error(array $arr) : void {
echo "...\n";
}
}

/** @param int[] $arr */
function errorArrayFilter(array $arr) : void {
$this->test = false;
$prices = array_filter($arr, function($elt) {
if ($elt === 1) {
$this->test = true;
}

return $elt === 2;
});


if ($this->test) {
echo "...\n";
}
}

/** @param int[] $arr */
function successLocal(array $arr) : void {
$test = false;
$prices = array_filter($arr, function($elt) use(&$test) {
if ($elt === 1) {
$test = true;
}

return $elt === 2;
});


if ($test) {
echo "...\n";
}
}
}
Loading