Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic parameter type extension #3823

Draft
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ services:
class: PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider
factory: PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider

-
class: PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider
factory: PHPStan\DependencyInjection\Type\LazyDynamicParameterTypeExtensionProvider

-
class: PHPStan\File\FileHelper
arguments:
Expand Down
53 changes: 53 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
use PHPStan\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection;
use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource;
use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider;
use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
Expand Down Expand Up @@ -262,6 +263,7 @@ public function __construct(
private readonly DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider,
private readonly ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider,
private readonly ParameterClosureTypeExtensionProvider $parameterClosureTypeExtensionProvider,
private readonly DynamicParameterTypeExtensionProvider $dynamicParameterTypeExtensionProvider,
private readonly ScopeFactory $scopeFactory,
private readonly bool $polluteScopeWithLoopInitialAssignments,
private readonly bool $polluteScopeWithAlwaysIterableForeach,
Expand Down Expand Up @@ -5002,6 +5004,12 @@ private function processArgs(

if ($overwritingParameterType !== null) {
$parameterType = $overwritingParameterType;
} else {
$overwritingParameterType = $this->getDynamicParameterTypeFromParameterTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass);

if ($overwritingParameterType !== null) {
$parameterType = $overwritingParameterType;
}
}
}

Expand Down Expand Up @@ -5054,6 +5062,12 @@ private function processArgs(

if ($overwritingParameterType !== null) {
$parameterType = $overwritingParameterType;
} else {
$overwritingParameterType = $this->getDynamicParameterTypeFromParameterTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass);

if ($overwritingParameterType !== null) {
$parameterType = $overwritingParameterType;
}
}
}

Expand All @@ -5065,6 +5079,15 @@ private function processArgs(
}
} else {
$exprType = $scope->getType($arg->value);

if ($parameter !== null) {
$overwritingParameterType = $this->getDynamicParameterTypeFromParameterTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass);

if ($overwritingParameterType !== null) {
$exprType = $overwritingParameterType;
}
}

$exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep());
$throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints());
Expand Down Expand Up @@ -5225,6 +5248,36 @@ private function getParameterTypeFromParameterClosureTypeExtension(CallLike $cal
return null;
}

/**
* @param MethodReflection|FunctionReflection|null $calleeReflection
*/
private function getDynamicParameterTypeFromParameterTypeExtension(CallLike $callLike, $calleeReflection, ParameterReflection $parameter, MutatingScope $scope): ?Type
{
if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) {
foreach ($this->dynamicParameterTypeExtensionProvider->getFunctionDynamicParameterTypeExtensions() as $functionDynamicParameterTypeExtension) {
if ($functionDynamicParameterTypeExtension->isFunctionSupported($calleeReflection, $parameter)) {
return $functionDynamicParameterTypeExtension->getTypeFromFunctionCall($calleeReflection, $callLike, $parameter, $scope);
}
}
} elseif ($calleeReflection instanceof MethodReflection) {
if ($callLike instanceof StaticCall) {
foreach ($this->dynamicParameterTypeExtensionProvider->getStaticMethodDynamicParameterTypeExtensions() as $staticMethodDynamicParameterTypeExtension) {
if ($staticMethodDynamicParameterTypeExtension->isStaticMethodSupported($calleeReflection, $parameter)) {
return $staticMethodDynamicParameterTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope);
}
}
} elseif ($callLike instanceof MethodCall) {
foreach ($this->dynamicParameterTypeExtensionProvider->getMethodDynamicParameterTypeExtensions() as $methodDynamicParameterTypeExtension) {
if ($methodDynamicParameterTypeExtension->isMethodSupported($calleeReflection, $parameter)) {
return $methodDynamicParameterTypeExtension->getTypeFromMethodCall($calleeReflection, $callLike, $parameter, $scope);
}
}
}
}

return null;
}

/**
* @param MethodReflection|FunctionReflection|null $calleeReflection
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection\Type;

use PHPStan\Type\FunctionDynamicParameterTypeExtension;
use PHPStan\Type\MethodDynamicParameterTypeExtension;
use PHPStan\Type\StaticMethodDynamicParameterTypeExtension;

interface DynamicParameterTypeExtensionProvider
{

/** @return FunctionDynamicParameterTypeExtension[] */
public function getFunctionDynamicParameterTypeExtensions(): array;

/** @return MethodDynamicParameterTypeExtension[] */
public function getMethodDynamicParameterTypeExtensions(): array;

/** @return StaticMethodDynamicParameterTypeExtension[] */
public function getStaticMethodDynamicParameterTypeExtensions(): array;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection\Type;

use PHPStan\DependencyInjection\Container;

final class LazyDynamicParameterTypeExtensionProvider implements DynamicParameterTypeExtensionProvider
{

public const FUNCTION_TAG = 'phpstan.functionDynamicParameterTypeExtension';
public const METHOD_TAG = 'phpstan.methodDynamicParameterTypeExtension';
public const STATIC_METHOD_TAG = 'phpstan.staticMethodDynamicParameterTypeExtension';

public function __construct(private Container $container)
{
}

public function getFunctionDynamicParameterTypeExtensions(): array
{
return $this->container->getServicesByTag(self::FUNCTION_TAG);
}

public function getMethodDynamicParameterTypeExtensions(): array
{
return $this->container->getServicesByTag(self::METHOD_TAG);
}

public function getStaticMethodDynamicParameterTypeExtensions(): array
{
return $this->container->getServicesByTag(self::STATIC_METHOD_TAG);
}

}
2 changes: 2 additions & 0 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Collectors\Collector;
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Dependency\DependencyResolver;
use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
Expand Down Expand Up @@ -98,6 +99,7 @@ private function getAnalyser(DirectRuleRegistry $ruleRegistry): Analyser
self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class),
$readWritePropertiesExtensions !== [] ? new DirectReadWritePropertiesExtensionProvider($readWritePropertiesExtensions) : self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
self::getContainer()->getByType(ParameterClosureTypeExtensionProvider::class),
self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class),
self::createScopeFactory($reflectionProvider, $typeSpecifier),
$this->shouldPolluteScopeWithLoopInitialAssignments(),
$this->shouldPolluteScopeWithAlwaysIterableForeach(),
Expand Down
2 changes: 2 additions & 0 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\ScopeContext;
use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
Expand Down Expand Up @@ -78,6 +79,7 @@ public static function processFile(
self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class),
self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
self::getContainer()->getByType(ParameterClosureTypeExtensionProvider::class),
self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class),
self::createScopeFactory($reflectionProvider, $typeSpecifier),
self::getContainer()->getParameter('polluteScopeWithLoopInitialAssignments'),
self::getContainer()->getParameter('polluteScopeWithAlwaysIterableForeach'),
Expand Down
32 changes: 32 additions & 0 deletions src/Type/FunctionDynamicParameterTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParameterReflection;

/**
* This is the interface for parameter closure type extensions for functions.
*
* To register it in the configuration file use the `phpstan.functionDynamicParameterTypeExtension` service tag:
*
* ```
* services:
* -
* class: App\PHPStan\MyExtension
* tags:
* - phpstan.functionDynamicParameterTypeExtension
* ```
*
* @api
*/
interface FunctionDynamicParameterTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool;

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type;

}
32 changes: 32 additions & 0 deletions src/Type/MethodDynamicParameterTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;

/**
* This is the interface for parameter closure type extensions for methods.
*
* To register it in the configuration file use the `phpstan.methodDynamicParameterTypeExtension` service tag:
*
* ```
* services:
* -
* class: App\PHPStan\MyExtension
* tags:
* - phpstan.methodDynamicParameterTypeExtension
* ```
*
* @api
*/
interface MethodDynamicParameterTypeExtension
{

public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool;

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type;

}
32 changes: 32 additions & 0 deletions src/Type/StaticMethodDynamicParameterTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;

/**
* This is the interface for parameter closure type extensions for static methods.
*
* To register it in the configuration file use the `phpstan.staticMethodDynamicParameterTypeExtension` service tag:
*
* ```
* services:
* -
* class: App\PHPStan\MyExtension
* tags:
* - phpstan.staticMethodDynamicParameterTypeExtension
* ```
*
* @api
*/
interface StaticMethodDynamicParameterTypeExtension
{

public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool;

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type;

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Dependency\DependencyResolver;
use PHPStan\Dependency\ExportedNodeResolver;
use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
Expand Down Expand Up @@ -721,6 +722,7 @@ private function createAnalyser(): Analyser
self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class),
self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
self::getContainer()->getByType(ParameterClosureTypeExtensionProvider::class),
self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class),
self::createScopeFactory($reflectionProvider, $typeSpecifier),
false,
true,
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/DynamicParameterTypeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use const PHP_VERSION_ID;

class DynamicParameterTypeExtensionTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
{
if (PHP_VERSION_ID < 70400) {
return [];
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/dynamic-parameter-type-extension-arrow-function.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/dynamic-parameter-type-extension-closure.php');
// yield from $this->gatherAssertTypes(__DIR__ . '/data/dynamic-parameter-type-extension-non-closure.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}

$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/dynamic-parameter-type-extension.neon',
];
}

}
Loading
Loading