Skip to content

Commit

Permalink
Scope::getPhpVersion() allows array via phpVersion min+max config
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Nov 24, 2024
1 parent e3867c0 commit 2d637da
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Analyser/DirectInternalScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
final class DirectInternalScopeFactory implements InternalScopeFactory
{

/**
* @param int|array{min: int, max: int}|null $configPhpVersion
*/
public function __construct(
private ReflectionProvider $reflectionProvider,
private InitializerExprTypeResolver $initializerExprTypeResolver,
Expand All @@ -31,6 +34,7 @@ public function __construct(
private NodeScopeResolver $nodeScopeResolver,
private RicherScopeGetTypeHelper $richerScopeGetTypeHelper,
private PhpVersion $phpVersion,
private int|array|null $configPhpVersion,
private ConstantResolver $constantResolver,
)
{
Expand Down Expand Up @@ -78,6 +82,7 @@ public function create(
$this->constantResolver,
$context,
$this->phpVersion,
$this->configPhpVersion,
$declareStrictTypes,
$function,
$namespace,
Expand Down
1 change: 1 addition & 0 deletions src/Analyser/LazyInternalScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function create(
$this->container->getByType(ConstantResolver::class),
$context,
$this->container->getByType(PhpVersion::class),
$this->container->getParameter('phpVersion'),
$declareStrictTypes,
$function,
$namespace,
Expand Down
6 changes: 6 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
use function get_class;
use function implode;
use function in_array;
use function is_array;
use function is_bool;
use function is_numeric;
use function is_string;
Expand Down Expand Up @@ -184,6 +185,7 @@ final class MutatingScope implements Scope
private static int $resolveClosureTypeDepth = 0;

/**
* @param int|array{min: int, max: int}|null $configPhpVersion
* @param array<string, ExpressionTypeHolder> $expressionTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param list<string> $inClosureBindScopeClasses
Expand All @@ -207,6 +209,7 @@ public function __construct(
private ConstantResolver $constantResolver,
private ScopeContext $context,
private PhpVersion $phpVersion,
private int|array|null $configPhpVersion,
private bool $declareStrictTypes = false,
private PhpFunctionFromParserNodeReflection|null $function = null,
?string $namespace = null,
Expand Down Expand Up @@ -5726,6 +5729,9 @@ public function getPhpVersion(): PhpVersions
{
$versionExpr = new ConstFetch(new Name('PHP_VERSION_ID'));
if (!$this->hasExpressionType($versionExpr)->yes()) {
if (is_array($this->configPhpVersion)) {
return new PhpVersions(IntegerRangeType::fromInterval($this->configPhpVersion['min'], $this->configPhpVersion['max']));
}
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
}

Expand Down
1 change: 1 addition & 0 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public static function createScopeFactory(ReflectionProvider $reflectionProvider
$container->getByType(NodeScopeResolver::class),
new RicherScopeGetTypeHelper($initializerExprTypeResolver),
$container->getByType(PhpVersion::class),
$container->getParameter('phpVersion'),
$constantResolver,
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Methods;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/** @extends RuleTestCase<FinalPrivateMethodRule> */
class FinalPrivateMethodRuleConfigPhpTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new FinalPrivateMethodRule();
}

public function testRulePhpVersions(): void
{
$this->analyse([__DIR__ . '/data/final-private-method-config-phpversion.php'], [
[
'Private method FinalPrivateMethodConfigPhpVersions\PhpVersionViaNEONConfg::foo() cannot be final as it is never overridden by other classes.',
8,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/data/final-private-php-version.neon',
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace FinalPrivateMethodConfigPhpVersions;

class PhpVersionViaNEONConfg
{

final private function foo(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
phpVersion:
min: 80100
max: 80499

0 comments on commit 2d637da

Please sign in to comment.