Skip to content

Commit

Permalink
New option: polluteScopeWithBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 6, 2024
1 parent 2775be4 commit 946cf18
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ parameters:
phpVersion: null
polluteScopeWithLoopInitialAssignments: true
polluteScopeWithAlwaysIterableForeach: true
polluteScopeWithBlock: true
propertyAlwaysWrittenTags: []
propertyAlwaysReadTags: []
fixerTmpDir: %pro.tmpDir% #unused
Expand Down Expand Up @@ -544,6 +545,7 @@ services:
reflector: @nodeScopeResolverReflector
polluteScopeWithLoopInitialAssignments: %polluteScopeWithLoopInitialAssignments%
polluteScopeWithAlwaysIterableForeach: %polluteScopeWithAlwaysIterableForeach%
polluteScopeWithBlock: %polluteScopeWithBlock%
earlyTerminatingMethodCalls: %earlyTerminatingMethodCalls%
earlyTerminatingFunctionCalls: %earlyTerminatingFunctionCalls%
implicitThrows: %exceptions.implicitThrows%
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ parametersSchema:
phpVersion: schema(anyOf(schema(int(), min(70100), max(80499))), nullable())
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
polluteScopeWithBlock: bool()
propertyAlwaysWrittenTags: listOf(string())
propertyAlwaysReadTags: listOf(string())
additionalConstructors: listOf(string())
Expand Down
16 changes: 15 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public function __construct(
private readonly ScopeFactory $scopeFactory,
private readonly bool $polluteScopeWithLoopInitialAssignments,
private readonly bool $polluteScopeWithAlwaysIterableForeach,
private readonly bool $polluteScopeWithBlock,
private readonly array $earlyTerminatingMethodCalls,
private readonly array $earlyTerminatingFunctionCalls,
private readonly array $universalObjectCratesClasses,
Expand Down Expand Up @@ -1895,7 +1896,20 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
new ImpurePoint($scope, $stmt, 'betweenPhpTags', 'output between PHP opening and closing tags', true),
];
} elseif ($stmt instanceof Node\Stmt\Block) {
return $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context);
$result = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context);
if ($this->polluteScopeWithBlock) {
return $result;
}

return new StatementResult(
$scope->mergeWith($result->getScope()),
$result->hasYield(),
$result->isAlwaysTerminating(),
$result->getExitPoints(),
$result->getThrowPoints(),
$result->getImpurePoints(),
$result->getEndStatements(),
);
} elseif ($stmt instanceof Node\Stmt\Nop) {
$hasYield = false;
$throwPoints = $overridingThrowPoints ?? [];
Expand Down
1 change: 1 addition & 0 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private function getAnalyser(DirectRuleRegistry $ruleRegistry): Analyser
self::createScopeFactory($reflectionProvider, $typeSpecifier),
$this->shouldPolluteScopeWithLoopInitialAssignments(),
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
self::getContainer()->getParameter('polluteScopeWithBlock'),
[],
[],
self::getContainer()->getParameter('universalObjectCratesClasses'),
Expand Down
1 change: 1 addition & 0 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static function processFile(
self::createScopeFactory($reflectionProvider, $typeSpecifier),
self::getContainer()->getParameter('polluteScopeWithLoopInitialAssignments'),
self::getContainer()->getParameter('polluteScopeWithAlwaysIterableForeach'),
self::getContainer()->getParameter('polluteScopeWithBlock'),
static::getEarlyTerminatingMethodCalls(),
static::getEarlyTerminatingFunctionCalls(),
self::getContainer()->getParameter('universalObjectCratesClasses'),
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ private function createAnalyser(bool $enableIgnoreErrorsWithinPhpDocs): Analyser
self::createScopeFactory($reflectionProvider, $typeSpecifier),
false,
true,
true,
[],
[],
[stdClass::class],
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/DoNotPolluteScopeWithBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;

class DoNotPolluteScopeWithBlockTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/do-not-pollute-scope-with-block.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../conf/bleedingEdge.neon',
__DIR__ . '/do-not-pollute-scope-with-block.neon',
];
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/do-not-pollute-scope-with-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DoNotPolluteScopeWithBlock;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;

class Foo
{

public function doFoo(): void
{
$a = 1;
{
$b = 2;
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertType('1', $a);

assertVariableCertainty(TrinaryLogic::createMaybe(), $b);
assertType('2', $b);
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/do-not-pollute-scope-with-block.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
polluteScopeWithBlock: false

0 comments on commit 946cf18

Please sign in to comment.