Skip to content

Commit 1492a87

Browse files
authored
Merge branch refs/heads/2.1.x into 2.2.x
2 parents f4e7ae1 + 06417d4 commit 1492a87

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

src/Analyser/MutatingScope.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,16 +2889,28 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require
28892889
$invalidated = true;
28902890
continue;
28912891
}
2892-
foreach ($holders as $holder) {
2892+
$filteredHolders = [];
2893+
foreach ($holders as $key => $holder) {
2894+
$shouldKeep = true;
28932895
$conditionalTypeHolders = $holder->getConditionExpressionTypeHolders();
28942896
foreach ($conditionalTypeHolders as $conditionalTypeHolderExprString => $conditionalTypeHolder) {
28952897
if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $conditionalTypeHolder->getExpr(), $conditionalTypeHolderExprString, false, $invalidatingClass)) {
28962898
$invalidated = true;
2897-
continue 3;
2899+
$shouldKeep = false;
2900+
break;
28982901
}
28992902
}
2903+
if (!$shouldKeep) {
2904+
continue;
2905+
}
2906+
2907+
$filteredHolders[$key] = $holder;
2908+
}
2909+
if (count($filteredHolders) <= 0) {
2910+
continue;
29002911
}
2901-
$newConditionalExpressions[$conditionalExprString] = $holders;
2912+
2913+
$newConditionalExpressions[$conditionalExprString] = $filteredHolders;
29022914
}
29032915

29042916
if (!$invalidated) {

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,28 @@ public function testBug14019(): void
14031403
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14019.php'], []);
14041404
}
14051405

1406+
#[RequiresPhp('>= 8.0')]
1407+
public function testBug14274(): void
1408+
{
1409+
$this->cliArgumentsVariablesRegistered = true;
1410+
$this->polluteScopeWithLoopInitialAssignments = false;
1411+
$this->checkMaybeUndefinedVariables = true;
1412+
$this->polluteScopeWithAlwaysIterableForeach = true;
1413+
1414+
$this->analyse([__DIR__ . '/data/bug-14274.php'], []);
1415+
}
1416+
1417+
#[RequiresPhp('>= 8.0')]
1418+
public function testBug12373(): void
1419+
{
1420+
$this->cliArgumentsVariablesRegistered = true;
1421+
$this->polluteScopeWithLoopInitialAssignments = false;
1422+
$this->checkMaybeUndefinedVariables = true;
1423+
$this->polluteScopeWithAlwaysIterableForeach = true;
1424+
1425+
$this->analyse([__DIR__ . '/data/bug-12373.php'], []);
1426+
}
1427+
14061428
public function testBug14117(): void
14071429
{
14081430
$this->cliArgumentsVariablesRegistered = true;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug12373;
6+
7+
class HelloWorld
8+
{
9+
public function sayHello(int $id): void
10+
{
11+
$foo = [];
12+
13+
if ($id)
14+
{
15+
$foo = 'foo';
16+
}
17+
else
18+
{
19+
$value = 'my value';
20+
}
21+
22+
$foo = "foo";
23+
24+
if (!$id)
25+
{
26+
echo 'value: ' . $value;
27+
}
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug14274;
6+
7+
class PreApplyEvent {}
8+
9+
final class ComposerPatchesValidator {
10+
/**
11+
* Validates the status of the patcher plugin.
12+
*/
13+
public function validate(mixed $event): void {
14+
$messages = [];
15+
16+
[$plugin_installed_in_active, $is_active_root_requirement, $active_configuration_ok] = $this->computePatcherStatus();
17+
if ($event instanceof PreApplyEvent) {
18+
[$plugin_installed_in_stage, $is_stage_root_requirement, $stage_configuration_ok] = $this->computePatcherStatus();
19+
$has_staged_update = TRUE;
20+
}
21+
else {
22+
// No staged update exists.
23+
$has_staged_update = FALSE;
24+
}
25+
26+
if ($has_staged_update && $plugin_installed_in_active !== $plugin_installed_in_stage) {
27+
$messages[] = 'package-manager-faq-composer-patches-installed-or-removed';
28+
}
29+
30+
// If the patcher is not listed in the runtime or dev dependencies, that's
31+
// an error as well.
32+
if (($plugin_installed_in_active && !$is_active_root_requirement) || ($has_staged_update && $plugin_installed_in_stage && !$is_stage_root_requirement)) {
33+
$messages[] = 'It must be a root dependency.';
34+
}
35+
36+
// If the plugin is misconfigured in either the active or stage directories,
37+
// flag an error.
38+
if (($plugin_installed_in_active && !$active_configuration_ok) || ($has_staged_update && $plugin_installed_in_stage && !$stage_configuration_ok)) {
39+
$messages[] = 'The composer-exit-on-patch-failure key is not set to true.';
40+
}
41+
}
42+
43+
/**
44+
* @return bool[]
45+
*/
46+
private function computePatcherStatus(): array {
47+
return [];
48+
}
49+
}

0 commit comments

Comments
 (0)