Skip to content

Commit 08d45ff

Browse files
committed
Take advantage of variadic mergeWith, pt. 1
1 parent 84b2aee commit 08d45ff

3 files changed

Lines changed: 27 additions & 41 deletions

File tree

src/Node/ClassPropertiesNode.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use function array_diff_key;
2727
use function array_key_exists;
2828
use function array_keys;
29+
use function array_slice;
30+
use function count;
2931
use function in_array;
3032
use function strtolower;
3133

@@ -269,7 +271,7 @@ private function collectUninitializedProperties(array $constructors, array $unin
269271
}
270272

271273
$returnStatementsNode = $this->returnStatementNodes[$lowerConstructorName];
272-
$methodScope = null;
274+
$scopesToMerge = [];
273275
foreach ($returnStatementsNode->getExecutionEnds() as $executionEnd) {
274276
$statementResult = $executionEnd->getStatementResult();
275277
$endNode = $executionEnd->getNode();
@@ -281,26 +283,20 @@ private function collectUninitializedProperties(array $constructors, array $unin
281283
}
282284
}
283285
}
284-
if ($methodScope === null) {
285-
$methodScope = $statementResult->getScope();
286-
continue;
287-
}
288-
289-
$methodScope = $methodScope->mergeWith($statementResult->getScope());
286+
$scopesToMerge[] = $statementResult->getScope();
290287
}
291288

292289
foreach ($returnStatementsNode->getReturnStatements() as $returnStatement) {
293-
if ($methodScope === null) {
294-
$methodScope = $returnStatement->getScope();
295-
continue;
296-
}
297-
$methodScope = $methodScope->mergeWith($returnStatement->getScope());
290+
$scopesToMerge[] = $returnStatement->getScope();
298291
}
299292

300-
if ($methodScope === null) {
293+
if (count($scopesToMerge) === 0) {
301294
continue;
302295
}
303296

297+
// @phpstan-ignore method.notFound
298+
$methodScope = $scopesToMerge[0]->mergeWith(...array_slice($scopesToMerge, 1));
299+
304300
foreach (array_keys($uninitializedProperties) as $propertyName) {
305301
if (!$methodScope->hasExpressionType(new PropertyInitializationExpr($propertyName))->yes()) {
306302
continue;

src/Rules/Properties/SetNonVirtualPropertyHookAssignRule.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\ShouldNotHappenException;
1313
use PHPStan\Type\NeverType;
14+
use function array_slice;
15+
use function count;
1416
use function sprintf;
1517

1618
/**
@@ -44,7 +46,7 @@ public function processNode(Node $node, Scope $scope): array
4446
return [];
4547
}
4648

47-
$finalHookScope = null;
49+
$scopesToMerge = [];
4850
foreach ($node->getExecutionEnds() as $executionEnd) {
4951
$statementResult = $executionEnd->getStatementResult();
5052
$endNode = $executionEnd->getNode();
@@ -56,26 +58,20 @@ public function processNode(Node $node, Scope $scope): array
5658
}
5759
}
5860
}
59-
if ($finalHookScope === null) {
60-
$finalHookScope = $statementResult->getScope();
61-
continue;
62-
}
63-
64-
$finalHookScope = $finalHookScope->mergeWith($statementResult->getScope());
61+
$scopesToMerge[] = $statementResult->getScope();
6562
}
6663

6764
foreach ($node->getReturnStatements() as $returnStatement) {
68-
if ($finalHookScope === null) {
69-
$finalHookScope = $returnStatement->getScope();
70-
continue;
71-
}
72-
$finalHookScope = $finalHookScope->mergeWith($returnStatement->getScope());
65+
$scopesToMerge[] = $returnStatement->getScope();
7366
}
7467

75-
if ($finalHookScope === null) {
68+
if (count($scopesToMerge) === 0) {
7669
return [];
7770
}
7871

72+
// @phpstan-ignore method.notFound
73+
$finalHookScope = $scopesToMerge[0]->mergeWith(...array_slice($scopesToMerge, 1));
74+
7975
$initExpr = new PropertyInitializationExpr($propertyName);
8076
$hasInit = $finalHookScope->hasExpressionType($initExpr);
8177
if ($hasInit->yes()) {

src/Rules/TooWideTypehints/TooWideParameterOutTypeCheck.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPStan\Node\ReturnStatement;
1010
use PHPStan\Reflection\ExtendedParameterReflection;
1111
use PHPStan\Rules\IdentifierRuleError;
12+
use function array_slice;
13+
use function count;
1214
use function lcfirst;
1315
use function sprintf;
1416

@@ -36,30 +38,22 @@ public function check(
3638
string $functionDescription,
3739
): array
3840
{
39-
$finalScope = null;
41+
$scopesToMerge = [];
4042
foreach ($executionEnds as $executionEnd) {
41-
$endScope = $executionEnd->getStatementResult()->getScope();
42-
if ($finalScope === null) {
43-
$finalScope = $endScope;
44-
continue;
45-
}
46-
47-
$finalScope = $finalScope->mergeWith($endScope);
43+
$scopesToMerge[] = $executionEnd->getStatementResult()->getScope();
4844
}
4945

5046
foreach ($returnStatements as $statement) {
51-
if ($finalScope === null) {
52-
$finalScope = $statement->getScope();
53-
continue;
54-
}
55-
56-
$finalScope = $finalScope->mergeWith($statement->getScope());
47+
$scopesToMerge[] = $statement->getScope();
5748
}
5849

59-
if ($finalScope === null) {
50+
if (count($scopesToMerge) === 0) {
6051
return [];
6152
}
6253

54+
// @phpstan-ignore method.notFound
55+
$finalScope = $scopesToMerge[0]->mergeWith(...array_slice($scopesToMerge, 1));
56+
6357
$errors = [];
6458
foreach ($parameters as $parameter) {
6559
if (!$parameter->passedByReference()->createsNewVariable()) {

0 commit comments

Comments
 (0)