Skip to content

Commit 24a51ee

Browse files
committed
simplify
1 parent f8a4f56 commit 24a51ee

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6240,26 +6240,25 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
62406240
}
62416241
}
62426242

6243-
if (count($dimFetchStack) === 1) {
6244-
$dimFetch = $dimFetchStack[0];
6245-
if ($dimFetch->dim !== null) {
6246-
$additionalExpressions[] = [$dimFetch, $originalValueToWrite];
6243+
$offsetValueType = $valueToWrite;
6244+
$lastDimKey = array_key_last($dimFetchStack);
6245+
foreach ($dimFetchStack as $key => $dimFetch) {
6246+
if ($dimFetch->dim === null) {
6247+
$additionalExpressions = [];
6248+
break;
62476249
}
6248-
} else {
6249-
$offsetValueType = $valueToWrite;
6250-
foreach ($dimFetchStack as $dimFetch) {
6251-
if ($dimFetch->dim === null) {
6252-
$additionalExpressions = [];
6253-
break;
6254-
}
62556250

6251+
if ($key === $lastDimKey) {
6252+
$offsetValueType = $originalValueToWrite;
6253+
} else {
62566254
$offsetType = $scope->getType($dimFetch->dim);
62576255
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
62586256
if ($offsetValueType instanceof ErrorType) {
62596257
$offsetValueType = new ConstantArrayType([], []);
62606258
}
6261-
$additionalExpressions[] = [$dimFetch, $offsetValueType];
62626259
}
6260+
6261+
$additionalExpressions[] = [$dimFetch, $offsetValueType];
62636262
}
62646263

62656264
return $valueToWrite;

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,4 +1265,9 @@ public function testBug7225(): void
12651265
$this->analyse([__DIR__ . '/data/bug-7225.php'], []);
12661266
}
12671267

1268+
public function testDeepDimFetch(): void
1269+
{
1270+
$this->analyse([__DIR__ . '/data/deep-dim-fetch.php'], []);
1271+
}
1272+
12681273
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DeepDimFetch;
4+
5+
use function is_callable;
6+
use function PHPStan\debugScope;
7+
8+
final class NameScope {}
9+
10+
final class FileTypeMapper
11+
{
12+
13+
/** @var (true|callable(): NameScope|NameScope)[][] */
14+
private array $inProcess = [];
15+
16+
public function getNameScope(
17+
string $fileName,
18+
?string $className,
19+
?string $traitName,
20+
?string $functionName,
21+
): NameScope
22+
{
23+
$nameScopeKey = $this->getNameScopeKey($fileName, $className, $traitName, $functionName);
24+
if (!isset($this->inProcess[$fileName][$nameScopeKey])) { // wrong $fileName due to traits
25+
throw new \RuntimeException();
26+
}
27+
28+
if ($this->inProcess[$fileName][$nameScopeKey] === true) { // PHPDoc has cyclic dependency
29+
throw new \RuntimeException();
30+
}
31+
32+
if (is_callable($this->inProcess[$fileName][$nameScopeKey])) {
33+
$resolveCallback = $this->inProcess[$fileName][$nameScopeKey];
34+
$this->inProcess[$fileName][$nameScopeKey] = true;
35+
$this->inProcess[$fileName][$nameScopeKey] = $resolveCallback();
36+
}
37+
38+
return $this->inProcess[$fileName][$nameScopeKey];
39+
}
40+
41+
private function getNameScopeKey(
42+
?string $file,
43+
?string $class,
44+
?string $trait,
45+
?string $function,
46+
): string
47+
{
48+
return '';
49+
}
50+
51+
}

0 commit comments

Comments
 (0)