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
3 changes: 3 additions & 0 deletions src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
}

} else {
if ($expr->name instanceof Expr) {
$scope = $scope->invalidateAllOnExpression($normalizedExpr->var);
}
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
}
$hasYield = $hasYield || $result->hasYield();
Expand Down
42 changes: 42 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,48 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require
);
}

public function invalidateAllOnExpression(Expr $expressionToInvalidate): self
{
$expressionTypes = $this->expressionTypes;
$nativeExpressionTypes = $this->nativeExpressionTypes;
$invalidated = false;
$exprStringToInvalidate = $this->getNodeKey($expressionToInvalidate);

foreach ($expressionTypes as $exprString => $exprTypeHolder) {
$exprExpr = $exprTypeHolder->getExpr();
if (!$this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $exprExpr, $exprString, true)) {
continue;
}

unset($expressionTypes[$exprString]);
unset($nativeExpressionTypes[$exprString]);
$invalidated = true;
}

if (!$invalidated) {
return $this;
}

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$expressionTypes,
$nativeExpressionTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
);
}

private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr $exprToInvalidate, Expr $expr, string $exprString, bool $requireMoreCharacters = false, ?ClassReflection $invalidatingClass = null): bool
{
if ($requireMoreCharacters && $exprStringToInvalidate === $exprString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,9 @@ public function testBug13874(): void
$this->analyse([__DIR__ . '/data/bug-13874.php'], []);
}

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

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

namespace Bug3831;

class Template {

/** @var list<string> */
public $footer = [];

public function render() : string {
$content = '';
$this->footer = [];

// dynamic method call - could modify $this->footer
$this->{'compileSection'}();

if (count($this->footer) > 0) {
$content = str_replace('some', 'thing', $content);
}
return $content;
}

private function compileSection(): void {
$this->footer[] = 'section-name';
}

}
Loading