Skip to content
Merged
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
18 changes: 15 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2893,16 +2893,28 @@ public function invalidateExpression(Expr $expressionToInvalidate, bool $require
$invalidated = true;
continue;
}
foreach ($holders as $holder) {
$filteredHolders = [];
foreach ($holders as $key => $holder) {
$shouldKeep = true;
$conditionalTypeHolders = $holder->getConditionExpressionTypeHolders();
foreach ($conditionalTypeHolders as $conditionalTypeHolderExprString => $conditionalTypeHolder) {
if ($this->shouldInvalidateExpression($exprStringToInvalidate, $expressionToInvalidate, $conditionalTypeHolder->getExpr(), $conditionalTypeHolderExprString, false, $invalidatingClass)) {
$invalidated = true;
continue 3;
$shouldKeep = false;
break;
}
}
if (!$shouldKeep) {
continue;
}

$filteredHolders[$key] = $holder;
}
if (count($filteredHolders) <= 0) {
continue;
}
$newConditionalExpressions[$conditionalExprString] = $holders;

$newConditionalExpressions[$conditionalExprString] = $filteredHolders;
}

if (!$invalidated) {
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,28 @@ public function testBug14019(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14019.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug14274(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-14274.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug12373(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-12373.php'], []);
}

public function testBug14117(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-12373.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // lint >= 8.0

declare(strict_types=1);

namespace Bug12373;

class HelloWorld
{
public function sayHello(int $id): void
{
$foo = [];

if ($id)
{
$foo = 'foo';
}
else
{
$value = 'my value';
}

$foo = "foo";

if (!$id)
{
echo 'value: ' . $value;
}
}
}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14274.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php // lint >= 8.0

declare(strict_types=1);

namespace Bug14274;

class PreApplyEvent {}

final class ComposerPatchesValidator {
/**
* Validates the status of the patcher plugin.
*/
public function validate(mixed $event): void {
$messages = [];

[$plugin_installed_in_active, $is_active_root_requirement, $active_configuration_ok] = $this->computePatcherStatus();
if ($event instanceof PreApplyEvent) {
[$plugin_installed_in_stage, $is_stage_root_requirement, $stage_configuration_ok] = $this->computePatcherStatus();
$has_staged_update = TRUE;
}
else {
// No staged update exists.
$has_staged_update = FALSE;
}

if ($has_staged_update && $plugin_installed_in_active !== $plugin_installed_in_stage) {
$messages[] = 'package-manager-faq-composer-patches-installed-or-removed';
}

// If the patcher is not listed in the runtime or dev dependencies, that's
// an error as well.
if (($plugin_installed_in_active && !$is_active_root_requirement) || ($has_staged_update && $plugin_installed_in_stage && !$is_stage_root_requirement)) {
$messages[] = 'It must be a root dependency.';
}

// If the plugin is misconfigured in either the active or stage directories,
// flag an error.
if (($plugin_installed_in_active && !$active_configuration_ok) || ($has_staged_update && $plugin_installed_in_stage && !$stage_configuration_ok)) {
$messages[] = 'The composer-exit-on-patch-failure key is not set to true.';
}
}

/**
* @return bool[]
*/
private function computePatcherStatus(): array {
return [];
}
}
Loading