Skip to content

Commit 2d637da

Browse files
authored
Scope::getPhpVersion() allows array via phpVersion min+max config
1 parent e3867c0 commit 2d637da

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

src/Analyser/DirectInternalScopeFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
final class DirectInternalScopeFactory implements InternalScopeFactory
2020
{
2121

22+
/**
23+
* @param int|array{min: int, max: int}|null $configPhpVersion
24+
*/
2225
public function __construct(
2326
private ReflectionProvider $reflectionProvider,
2427
private InitializerExprTypeResolver $initializerExprTypeResolver,
@@ -31,6 +34,7 @@ public function __construct(
3134
private NodeScopeResolver $nodeScopeResolver,
3235
private RicherScopeGetTypeHelper $richerScopeGetTypeHelper,
3336
private PhpVersion $phpVersion,
37+
private int|array|null $configPhpVersion,
3438
private ConstantResolver $constantResolver,
3539
)
3640
{
@@ -78,6 +82,7 @@ public function create(
7882
$this->constantResolver,
7983
$context,
8084
$this->phpVersion,
85+
$this->configPhpVersion,
8186
$declareStrictTypes,
8287
$function,
8388
$namespace,

src/Analyser/LazyInternalScopeFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function create(
6767
$this->container->getByType(ConstantResolver::class),
6868
$context,
6969
$this->container->getByType(PhpVersion::class),
70+
$this->container->getParameter('phpVersion'),
7071
$declareStrictTypes,
7172
$function,
7273
$namespace,

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
use function get_class;
145145
use function implode;
146146
use function in_array;
147+
use function is_array;
147148
use function is_bool;
148149
use function is_numeric;
149150
use function is_string;
@@ -184,6 +185,7 @@ final class MutatingScope implements Scope
184185
private static int $resolveClosureTypeDepth = 0;
185186

186187
/**
188+
* @param int|array{min: int, max: int}|null $configPhpVersion
187189
* @param array<string, ExpressionTypeHolder> $expressionTypes
188190
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
189191
* @param list<string> $inClosureBindScopeClasses
@@ -207,6 +209,7 @@ public function __construct(
207209
private ConstantResolver $constantResolver,
208210
private ScopeContext $context,
209211
private PhpVersion $phpVersion,
212+
private int|array|null $configPhpVersion,
210213
private bool $declareStrictTypes = false,
211214
private PhpFunctionFromParserNodeReflection|null $function = null,
212215
?string $namespace = null,
@@ -5726,6 +5729,9 @@ public function getPhpVersion(): PhpVersions
57265729
{
57275730
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID'));
57285731
if (!$this->hasExpressionType($versionExpr)->yes()) {
5732+
if (is_array($this->configPhpVersion)) {
5733+
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
5734+
}
57295735
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
57305736
}
57315737

src/Testing/PHPStanTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public static function createScopeFactory(ReflectionProvider $reflectionProvider
163163
$container->getByType(NodeScopeResolver::class),
164164
new RicherScopeGetTypeHelper($initializerExprTypeResolver),
165165
$container->getByType(PhpVersion::class),
166+
$container->getParameter('phpVersion'),
166167
$constantResolver,
167168
),
168169
);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Methods;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/** @extends RuleTestCase<FinalPrivateMethodRule> */
9+
class FinalPrivateMethodRuleConfigPhpTest extends RuleTestCase
10+
{
11+
12+
protected function getRule(): Rule
13+
{
14+
return new FinalPrivateMethodRule();
15+
}
16+
17+
public function testRulePhpVersions(): void
18+
{
19+
$this->analyse([__DIR__ . '/data/final-private-method-config-phpversion.php'], [
20+
[
21+
'Private method FinalPrivateMethodConfigPhpVersions\PhpVersionViaNEONConfg::foo() cannot be final as it is never overridden by other classes.',
22+
8,
23+
],
24+
]);
25+
}
26+
27+
public static function getAdditionalConfigFiles(): array
28+
{
29+
return [
30+
__DIR__ . '/data/final-private-php-version.neon',
31+
];
32+
}
33+
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace FinalPrivateMethodConfigPhpVersions;
4+
5+
class PhpVersionViaNEONConfg
6+
{
7+
8+
final private function foo(): void
9+
{
10+
}
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
phpVersion:
3+
min: 80100
4+
max: 80499

0 commit comments

Comments
 (0)