mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
Updated Rector to commit 325c6be3ae
325c6be3ae
[TypeDeclaration] Add ParamTypeByMethodCallTypeRector (#544)
This commit is contained in:
parent
276fac662d
commit
f7dcf79c1e
|
@ -6,6 +6,8 @@ namespace RectorPrefix20210730;
|
|||
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromCallersRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector;
|
||||
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector;
|
||||
|
@ -21,4 +23,5 @@ return static function (\Symfony\Component\DependencyInjection\Loader\Configurat
|
|||
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector::class);
|
||||
// $services->set(AddParamTypeFromCallersRector::class);
|
||||
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector::class);
|
||||
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector::class);
|
||||
};
|
||||
|
|
117
rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Normal file
117
rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Normal file
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\UnionType;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
final class CallerParamMatcher
|
||||
{
|
||||
/**
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @var \Rector\Core\PhpParser\AstResolver
|
||||
*/
|
||||
private $astResolver;
|
||||
/**
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\AstResolver $astResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->astResolver = $astResolver;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\FuncCall $call
|
||||
* @return null|\PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType
|
||||
*/
|
||||
public function matchCallParamType($call, \PhpParser\Node\Param $param, \PHPStan\Analyser\Scope $scope)
|
||||
{
|
||||
$callParam = $this->matchCallParam($call, $param, $scope);
|
||||
if (!$callParam instanceof \PhpParser\Node\Param) {
|
||||
return null;
|
||||
}
|
||||
return $callParam->type;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\FuncCall $call
|
||||
*/
|
||||
public function matchCallParam($call, \PhpParser\Node\Param $param, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node\Param
|
||||
{
|
||||
$callArgPosition = $this->matchCallArgPosition($call, $param);
|
||||
if ($callArgPosition === null) {
|
||||
return null;
|
||||
}
|
||||
$classMethodOrFunction = $this->astResolver->resolveClassMethodOrFunctionFromCall($call, $scope);
|
||||
if ($classMethodOrFunction === null) {
|
||||
return null;
|
||||
}
|
||||
return $classMethodOrFunction->params[$callArgPosition] ?? null;
|
||||
}
|
||||
public function matchParentParam(\PhpParser\Node\Expr\StaticCall $parentStaticCall, \PhpParser\Node\Param $param, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node\Param
|
||||
{
|
||||
$methodName = $this->nodeNameResolver->getName($parentStaticCall->name);
|
||||
if ($methodName === null) {
|
||||
return null;
|
||||
}
|
||||
// match current param to parent call position
|
||||
$parentStaticCallArgPosition = $this->matchCallArgPosition($parentStaticCall, $param);
|
||||
if ($parentStaticCallArgPosition === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->resolveParentMethodParam($scope, $methodName, $parentStaticCallArgPosition);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\FuncCall $call
|
||||
* @return int|null
|
||||
*/
|
||||
private function matchCallArgPosition($call, \PhpParser\Node\Param $param)
|
||||
{
|
||||
$paramName = $this->nodeNameResolver->getName($param);
|
||||
foreach ($call->args as $argPosition => $arg) {
|
||||
if (!$arg->value instanceof \PhpParser\Node\Expr\Variable) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->nodeNameResolver->isName($arg->value, $paramName)) {
|
||||
continue;
|
||||
}
|
||||
return $argPosition;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function resolveParentMethodParam(\PHPStan\Analyser\Scope $scope, string $methodName, int $paramPosition) : ?\PhpParser\Node\Param
|
||||
{
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
foreach ($classReflection->getParents() as $parentClassReflection) {
|
||||
if (!$parentClassReflection->hasMethod($methodName)) {
|
||||
continue;
|
||||
}
|
||||
$parentClassMethod = $this->astResolver->resolveClassMethod($parentClassReflection->getName(), $methodName);
|
||||
if (!$parentClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
||||
continue;
|
||||
}
|
||||
return $parentClassMethod->params[$paramPosition] ?? null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
final class ParentParamMatcher
|
||||
{
|
||||
/**
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @var \Rector\Core\PhpParser\AstResolver
|
||||
*/
|
||||
private $astResolver;
|
||||
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\AstResolver $astResolver)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->astResolver = $astResolver;
|
||||
}
|
||||
public function matchParentParam(\PhpParser\Node\Expr\StaticCall $parentStaticCall, \PhpParser\Node\Param $param, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node\Param
|
||||
{
|
||||
$methodName = $this->nodeNameResolver->getName($parentStaticCall->name);
|
||||
if ($methodName === null) {
|
||||
return null;
|
||||
}
|
||||
// match current param to parent call position
|
||||
$parentStaticCallArgPosition = $this->matchParentStaticCallArgPosition($parentStaticCall, $param);
|
||||
if ($parentStaticCallArgPosition === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->resolveParentMethodParam($scope, $methodName, $parentStaticCallArgPosition);
|
||||
}
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
private function matchParentStaticCallArgPosition(\PhpParser\Node\Expr\StaticCall $parentStaticCall, \PhpParser\Node\Param $param)
|
||||
{
|
||||
$paramName = $this->nodeNameResolver->getName($param);
|
||||
foreach ($parentStaticCall->args as $argPosition => $arg) {
|
||||
if (!$arg->value instanceof \PhpParser\Node\Expr\Variable) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->nodeNameResolver->isName($arg->value, $paramName)) {
|
||||
continue;
|
||||
}
|
||||
return $argPosition;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function resolveParentMethodParam(\PHPStan\Analyser\Scope $scope, string $methodName, int $paramPosition) : ?\PhpParser\Node\Param
|
||||
{
|
||||
/** @var ClassReflection $classReflection */
|
||||
$classReflection = $scope->getClassReflection();
|
||||
foreach ($classReflection->getParents() as $parnetClassReflection) {
|
||||
if (!$parnetClassReflection->hasMethod($methodName)) {
|
||||
continue;
|
||||
}
|
||||
$parentClassMethod = $this->astResolver->resolveClassMethod($parnetClassReflection->getName(), $methodName);
|
||||
if (!$parentClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
||||
continue;
|
||||
}
|
||||
return $parentClassMethod->params[$paramPosition] ?? null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\UnionType;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher;
|
||||
use RectorPrefix20210730\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\ParamTypeByMethodCallTypeRectorTest
|
||||
*/
|
||||
final class ParamTypeByMethodCallTypeRector extends \Rector\Core\Rector\AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher
|
||||
*/
|
||||
private $callerParamMatcher;
|
||||
/**
|
||||
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
|
||||
*/
|
||||
private $simpleCallableNodeTraverser;
|
||||
public function __construct(\Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher $callerParamMatcher, \RectorPrefix20210730\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
|
||||
{
|
||||
$this->callerParamMatcher = $callerParamMatcher;
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change param type based on passed method call type', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeTypedService
|
||||
{
|
||||
public function run(string $name)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
final class UseDependency
|
||||
{
|
||||
public function __construct(
|
||||
private SomeTypedService $someTypedService
|
||||
) {
|
||||
}
|
||||
|
||||
public function go($value)
|
||||
{
|
||||
$this->someTypedService->run($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeTypedService
|
||||
{
|
||||
public function run(string $name)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
final class UseDependency
|
||||
{
|
||||
public function __construct(
|
||||
private SomeTypedService $someTypedService
|
||||
) {
|
||||
}
|
||||
|
||||
public function go(string $value)
|
||||
{
|
||||
$this->someTypedService->run($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [\PhpParser\Node\Stmt\ClassMethod::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
||||
{
|
||||
if ($this->shouldSkipClassMethod($node)) {
|
||||
return null;
|
||||
}
|
||||
/** @var array<StaticCall|MethodCall|FuncCall> $callers */
|
||||
$callers = $this->betterNodeFinder->findInstancesOf((array) $node->stmts, [\PhpParser\Node\Expr\StaticCall::class, \PhpParser\Node\Expr\MethodCall::class, \PhpParser\Node\Expr\FuncCall::class]);
|
||||
$hasChanged = \false;
|
||||
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
||||
foreach ($node->params as $param) {
|
||||
if ($this->shouldSkipParam($param, $node)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($callers as $caller) {
|
||||
$paramType = $this->callerParamMatcher->matchCallParamType($caller, $param, $scope);
|
||||
if ($paramType === null) {
|
||||
continue;
|
||||
}
|
||||
$this->mirrorParamType($param, $paramType);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
}
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function shouldSkipClassMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
|
||||
{
|
||||
if ($classMethod->params === []) {
|
||||
return \true;
|
||||
}
|
||||
$scope = $classMethod->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
||||
if (!$scope instanceof \PHPStan\Analyser\Scope) {
|
||||
return \true;
|
||||
}
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
||||
return \true;
|
||||
}
|
||||
return !$classReflection->isClass();
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType $paramType
|
||||
*/
|
||||
private function mirrorParamType(\PhpParser\Node\Param $decoratedParam, $paramType) : void
|
||||
{
|
||||
// mimic type
|
||||
$newParamType = $paramType;
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($newParamType, function (\PhpParser\Node $node) : void {
|
||||
// original attributes have to removed to avoid tokens crashing from origin positions
|
||||
$node->setAttributes([]);
|
||||
});
|
||||
$decoratedParam->type = $newParamType;
|
||||
}
|
||||
/**
|
||||
* Should skip param because one of them is conditional types?
|
||||
*/
|
||||
private function isParamConditioned(\PhpParser\Node\Param $param, \PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
|
||||
{
|
||||
$paramName = $this->nodeNameResolver->getName($param->var);
|
||||
if ($paramName === null) {
|
||||
return \false;
|
||||
}
|
||||
/** @var Variable[] $variables */
|
||||
$variables = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, \PhpParser\Node\Expr\Variable::class);
|
||||
foreach ($variables as $variable) {
|
||||
if (!$this->isName($variable, $paramName)) {
|
||||
continue;
|
||||
}
|
||||
$conditional = $this->betterNodeFinder->findParentType($variable, \PhpParser\Node\Stmt\If_::class);
|
||||
if ($conditional instanceof \PhpParser\Node\Stmt\If_) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function shouldSkipParam(\PhpParser\Node\Param $param, \PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
|
||||
{
|
||||
if ($this->isParamConditioned($param, $classMethod)) {
|
||||
return \true;
|
||||
}
|
||||
// already has type, skip
|
||||
return $param->type !== null;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ use PHPStan\Analyser\Scope;
|
|||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
|
@ -20,12 +20,12 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
final class ParamTypeByParentCallTypeRector extends \Rector\Core\Rector\AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher
|
||||
*/
|
||||
private $parentParamMatcher;
|
||||
public function __construct(\Rector\TypeDeclaration\NodeAnalyzer\ParentParamMatcher $parentParamMatcher)
|
||||
private $callerParamMatcher;
|
||||
public function __construct(\Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher $callerParamMatcher)
|
||||
{
|
||||
$this->parentParamMatcher = $parentParamMatcher;
|
||||
$this->callerParamMatcher = $callerParamMatcher;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
|
@ -82,15 +82,17 @@ CODE_SAMPLE
|
|||
if (!$parentStaticCall instanceof \PhpParser\Node\Expr\StaticCall) {
|
||||
return null;
|
||||
}
|
||||
/** @var Scope $scope */
|
||||
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
||||
if (!$scope instanceof \PHPStan\Analyser\Scope) {
|
||||
return null;
|
||||
}
|
||||
$hasChanged = \false;
|
||||
foreach ($node->params as $param) {
|
||||
// already has type, skip
|
||||
if ($param->type !== null) {
|
||||
continue;
|
||||
}
|
||||
$parentParam = $this->parentParamMatcher->matchParentParam($parentStaticCall, $param, $scope);
|
||||
$parentParam = $this->callerParamMatcher->matchParentParam($parentStaticCall, $param, $scope);
|
||||
if (!$parentParam instanceof \PhpParser\Node\Param) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '29c28fda876e20ae62b54f1a7d11e617db11ce74';
|
||||
public const PACKAGE_VERSION = '325c6be3ae5e782fef6359c40cdde58fdd0eb24b';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2021-07-30 10:29:09';
|
||||
public const RELEASE_DATE = '2021-07-30 10:29:31';
|
||||
public static function resolvePackageVersion() : string
|
||||
{
|
||||
$process = new \RectorPrefix20210730\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||
|
|
|
@ -4,6 +4,8 @@ declare (strict_types=1);
|
|||
namespace Rector\Core\PhpParser;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Param;
|
||||
|
@ -17,6 +19,7 @@ use PhpParser\Node\Stmt\Property;
|
|||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PhpParser\NodeFinder;
|
||||
use PhpParser\Parser;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Php\PhpFunctionReflection;
|
||||
|
@ -156,6 +159,17 @@ final class AstResolver
|
|||
$this->classMethodsByClassAndMethod[$classReflection->getName()][$methodReflection->getName()] = $classMethod;
|
||||
return $classMethod;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $call
|
||||
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
|
||||
*/
|
||||
public function resolveClassMethodOrFunctionFromCall($call, \PHPStan\Analyser\Scope $scope)
|
||||
{
|
||||
if ($call instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||
return $this->resolveFunctionFromFuncCall($call, $scope);
|
||||
}
|
||||
return $this->resolveClassMethodFromCall($call);
|
||||
}
|
||||
public function resolveFunctionFromFunctionReflection(\PHPStan\Reflection\Php\PhpFunctionReflection $phpFunctionReflection) : ?\PhpParser\Node\Stmt\Function_
|
||||
{
|
||||
if (isset($this->functionsByName[$phpFunctionReflection->getName()])) {
|
||||
|
@ -350,4 +364,18 @@ final class AstResolver
|
|||
}
|
||||
return null;
|
||||
}
|
||||
private function resolveFunctionFromFuncCall(\PhpParser\Node\Expr\FuncCall $funcCall, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node\Stmt\Function_
|
||||
{
|
||||
if ($funcCall->name instanceof \PhpParser\Node\Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->reflectionProvider->hasFunction($funcCall->name, $scope)) {
|
||||
return null;
|
||||
}
|
||||
$reflectionFunction = $this->reflectionProvider->getFunction($funcCall->name, $scope);
|
||||
if (!$reflectionFunction instanceof \PHPStan\Reflection\Php\PhpFunctionReflection) {
|
||||
return null;
|
||||
}
|
||||
return $this->resolveFunctionFromFunctionReflection($reflectionFunction);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -315,13 +315,6 @@ final class NodeFactory
|
|||
$this->phpDocInfoFactory->createFromNode($property);
|
||||
return $property;
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function createClassConst(string $name, $value, int $modifier) : \PhpParser\Node\Stmt\ClassConst
|
||||
{
|
||||
return $this->createClassConstant($name, $value, $modifier);
|
||||
}
|
||||
public function createGetterClassMethod(string $propertyName, \PHPStan\Type\Type $type) : \PhpParser\Node\Stmt\ClassMethod
|
||||
{
|
||||
$methodBuilder = new \RectorPrefix20210730\Symplify\Astral\ValueObject\NodeBuilder\MethodBuilder('get' . \ucfirst($propertyName));
|
||||
|
@ -498,17 +491,14 @@ final class NodeFactory
|
|||
}
|
||||
return $this->createBooleanAndFromNodes($newNodes);
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function createClassConstant(string $name, $value, int $modifier) : \PhpParser\Node\Stmt\ClassConst
|
||||
public function createClassConstant(string $name, \PhpParser\Node\Expr $expr, int $modifier) : \PhpParser\Node\Stmt\ClassConst
|
||||
{
|
||||
$value = \PhpParser\BuilderHelpers::normalizeValue($value);
|
||||
$const = new \PhpParser\Node\Const_($name, $value);
|
||||
$expr = \PhpParser\BuilderHelpers::normalizeValue($expr);
|
||||
$const = new \PhpParser\Node\Const_($name, $expr);
|
||||
$classConst = new \PhpParser\Node\Stmt\ClassConst([$const]);
|
||||
$classConst->flags |= $modifier;
|
||||
// add @var type by default
|
||||
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($value);
|
||||
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($expr);
|
||||
if (!$staticType instanceof \PHPStan\Type\MixedType) {
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classConst);
|
||||
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $staticType);
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -4,4 +4,4 @@
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736::getLoader();
|
||||
return ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b::getLoader();
|
||||
|
|
3
vendor/composer/autoload_classmap.php
vendored
3
vendor/composer/autoload_classmap.php
vendored
|
@ -3122,9 +3122,9 @@ return array(
|
|||
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => $baseDir . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParentParamMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ParentParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
|
||||
|
@ -3142,6 +3142,7 @@ return array(
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736
|
||||
class ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,15 +22,15 @@ class ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitac759ecff770480ca8ee70b75e2fa736::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitf3685a2494790da0f278887fe4cd342b::getInitializer($loader));
|
||||
} else {
|
||||
$classMap = require __DIR__ . '/autoload_classmap.php';
|
||||
if ($classMap) {
|
||||
|
@ -42,19 +42,19 @@ class ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736
|
|||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInitac759ecff770480ca8ee70b75e2fa736::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInitf3685a2494790da0f278887fe4cd342b::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequireac759ecff770480ca8ee70b75e2fa736($fileIdentifier, $file);
|
||||
composerRequiref3685a2494790da0f278887fe4cd342b($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequireac759ecff770480ca8ee70b75e2fa736($fileIdentifier, $file)
|
||||
function composerRequiref3685a2494790da0f278887fe4cd342b($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
|
11
vendor/composer/autoload_static.php
vendored
11
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitac759ecff770480ca8ee70b75e2fa736
|
||||
class ComposerStaticInitf3685a2494790da0f278887fe4cd342b
|
||||
{
|
||||
public static $files = array (
|
||||
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
||||
|
@ -3477,9 +3477,9 @@ class ComposerStaticInitac759ecff770480ca8ee70b75e2fa736
|
|||
'Rector\\TypeDeclaration\\Matcher\\PropertyAssignMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Matcher/PropertyAssignMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\AutowiredClassMethodOrPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/AutowiredClassMethodOrPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallTypesResolver' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParentParamMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ParentParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
|
||||
|
@ -3497,6 +3497,7 @@ class ComposerStaticInitac759ecff770480ca8ee70b75e2fa736
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
|
@ -3852,9 +3853,9 @@ class ComposerStaticInitac759ecff770480ca8ee70b75e2fa736
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitac759ecff770480ca8ee70b75e2fa736::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitac759ecff770480ca8ee70b75e2fa736::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitac759ecff770480ca8ee70b75e2fa736::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitf3685a2494790da0f278887fe4cd342b::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitf3685a2494790da0f278887fe4cd342b::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitf3685a2494790da0f278887fe4cd342b::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
2
vendor/composer/installed.json
vendored
2
vendor/composer/installed.json
vendored
|
@ -851,7 +851,7 @@
|
|||
"conflict": {
|
||||
"phpstan\/phpstan-shim": "*"
|
||||
},
|
||||
"time": "2021-07-30T08:22:18+00:00",
|
||||
"time": "2021-07-30T10:12:08+00:00",
|
||||
"default-branch": true,
|
||||
"bin": [
|
||||
"phpstan",
|
||||
|
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
|
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
|||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20210730\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736', false) && !interface_exists('ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736', false) && !trait_exists('ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736', false)) {
|
||||
spl_autoload_call('RectorPrefix20210730\ComposerAutoloaderInitac759ecff770480ca8ee70b75e2fa736');
|
||||
if (!class_exists('ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b', false) && !interface_exists('ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b', false) && !trait_exists('ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b', false)) {
|
||||
spl_autoload_call('RectorPrefix20210730\ComposerAutoloaderInitf3685a2494790da0f278887fe4cd342b');
|
||||
}
|
||||
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
|
||||
spl_autoload_call('RectorPrefix20210730\Doctrine\Inflector\Inflector');
|
||||
|
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
|
|||
return \RectorPrefix20210730\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequireac759ecff770480ca8ee70b75e2fa736')) {
|
||||
function composerRequireac759ecff770480ca8ee70b75e2fa736() {
|
||||
return \RectorPrefix20210730\composerRequireac759ecff770480ca8ee70b75e2fa736(...func_get_args());
|
||||
if (!function_exists('composerRequiref3685a2494790da0f278887fe4cd342b')) {
|
||||
function composerRequiref3685a2494790da0f278887fe4cd342b() {
|
||||
return \RectorPrefix20210730\composerRequiref3685a2494790da0f278887fe4cd342b(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('parseArgs')) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user