Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4900,6 +4900,68 @@ public function processAlwaysIterableForeachScopeWithoutPollute(self $finalScope
);
}

public function processAlwaysIterableForScopeWithoutPollute(self $finalScope, self $initScope): self
{
$expressionTypes = $this->expressionTypes;
$initScopeExpressionTypes = $initScope->expressionTypes;
foreach ($finalScope->expressionTypes as $variableExprString => $variableTypeHolder) {
if (!isset($expressionTypes[$variableExprString])) {
if (isset($initScopeExpressionTypes[$variableExprString])) {
$expressionTypes[$variableExprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
continue;
}

$expressionTypes[$variableExprString] = $variableTypeHolder;
continue;
}

$expressionTypes[$variableExprString] = new ExpressionTypeHolder(
$variableTypeHolder->getExpr(),
$variableTypeHolder->getType(),
$variableTypeHolder->getCertainty()->and($expressionTypes[$variableExprString]->getCertainty()),
);
}

$nativeTypes = $this->nativeExpressionTypes;
$initScopeNativeExpressionTypes = $initScope->nativeExpressionTypes;
foreach ($finalScope->nativeExpressionTypes as $variableExprString => $variableTypeHolder) {
if (!isset($nativeTypes[$variableExprString])) {
if (isset($initScopeNativeExpressionTypes[$variableExprString])) {
$nativeTypes[$variableExprString] = ExpressionTypeHolder::createMaybe($variableTypeHolder->getExpr(), $variableTypeHolder->getType());
continue;
}

$nativeTypes[$variableExprString] = $variableTypeHolder;
continue;
}

$nativeTypes[$variableExprString] = new ExpressionTypeHolder(
$variableTypeHolder->getExpr(),
$variableTypeHolder->getType(),
$variableTypeHolder->getCertainty()->and($nativeTypes[$variableExprString]->getCertainty()),
);
}

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

public function generalizeWith(self $otherScope): self
{
$variableTypeHolders = $this->generalizeVariableTypeHolders(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ private function processStmtNode(
}
} else {
if (!$this->polluteScopeWithLoopInitialAssignments) {
$finalScope = $finalScope->mergeWith($scope);
$finalScope = $scope->processAlwaysIterableForScopeWithoutPollute($finalScope, $initScope);
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,4 +1068,13 @@ public function testBug10228(): void
$this->analyse([__DIR__ . '/data/bug-10228.php'], []);
}

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

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

namespace Bug9550;

class Foo
{
protected static function makeShortTextFromScalar(string $value, int $maxUtf8Length = 50): string
{
$maxUtf8Length = max(20, min($maxUtf8Length, 100));

$vStrLonger = mb_substr($value, 0, $maxUtf8Length + 1000);

$withThreeDots = false;
\PHPStan\dumpType($maxUtf8Length);
for ($l = $maxUtf8Length; $l > 0; --$l) {
$vStr = mb_substr($vStrLonger, 0, $l);
if ($vStr !== $vStrLonger) {
$vStrLonger = $vStr;
$vStr = mb_substr($vStr, 0, $l - 3);
$withThreeDots = true;
} else {
$vStrLonger = $vStr;
}
$vStr = str_replace(["\0", "\t", "\n", "\r"], ['\0', '\t', '\n', '\r'], $vStr);
if (mb_strlen($vStr) <= $maxUtf8Length - ($withThreeDots ? 3 : 0)) {
break;
}
}

return '\'' . $vStr . '\'' . ($withThreeDots ? '...' : '');
}
}
Loading