mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
Improve reflection for Variadic analyzer (#310)
This commit is contained in:
parent
20ad16b1e6
commit
4f4dce928e
|
@ -39,7 +39,8 @@ final class SpacingAwareArrayTypeNode extends ArrayTypeNode implements Stringabl
|
|||
|
||||
private function isGenericArrayCandidate(TypeNode $typeNode): bool
|
||||
{
|
||||
if (! $this->getAttribute(ArrayTypeMapper::HAS_GENERIC_TYPE_PARENT)) {
|
||||
$hasGenericTypeParent = (bool) $this->getAttribute(ArrayTypeMapper::HAS_GENERIC_TYPE_PARENT);
|
||||
if (! $hasGenericTypeParent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
10
phpstan.neon
10
phpstan.neon
|
@ -513,3 +513,13 @@ parameters:
|
|||
message: '#Cannot return include_once/require_once#'
|
||||
path: rules/Renaming/Rector/Name/RenameClassRector.php
|
||||
|
||||
# required assigns - parent looping
|
||||
-
|
||||
message: '#foreach\(\.\.\.\), while\(\), for\(\) or if\(\.\.\.\) cannot contains a complex expression\. Extract it to a new variable assign on line before#'
|
||||
paths:
|
||||
- packages/NodeNestingScope/FlowOfControlLocator.php
|
||||
- src/PhpParser/Node/BetterNodeFinder.php
|
||||
- rules/Php70/Rector/FuncCall/MultiDirnameRector.php
|
||||
- src/Application/FileProcessor.php
|
||||
- rules/CodingStyle/Rector/Assign/ManualJsonStringToJsonEncodeArrayRector.php
|
||||
- rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php
|
||||
|
|
|
@ -93,7 +93,8 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if (is_a($this->getStaticType($node->expr), ObjectType::class)) {
|
||||
$exprType = $this->getStaticType($node->expr);
|
||||
if ($exprType instanceof ObjectType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -128,9 +128,12 @@ CODE_SAMPLE
|
|||
if (! $this->valueResolver->isFalse($returnedExpr)) {
|
||||
return ! $this->valueResolver->isTrueOrFalse($nextNode->expr);
|
||||
}
|
||||
if (! \str_contains($this->print($if->cond), '!=')) {
|
||||
|
||||
$condString = $this->print($if->cond);
|
||||
if (! \str_contains($condString, '!=')) {
|
||||
return ! $this->valueResolver->isTrueOrFalse($nextNode->expr);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -124,7 +124,8 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
// refactor here
|
||||
if ($this->refactorClassMethod($classMethod, $classReflection, $ancestors, $interfaces) !== null) {
|
||||
$changedClassMethod = $this->refactorClassMethod($classMethod, $classReflection, $ancestors, $interfaces);
|
||||
if ($changedClassMethod !== null) {
|
||||
$hasChanged = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,8 @@ use PHPStan\Reflection\FunctionReflection;
|
|||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Php\PhpMethodReflection;
|
||||
use PHPStan\Reflection\Type\UnionTypeMethodReflection;
|
||||
use Rector\Core\NodeAnalyzer\VariadicAnalyzer;
|
||||
use Rector\Core\PHPStan\Reflection\CallReflectionResolver;
|
||||
use Rector\Core\PHPStan\Reflection\VariadicAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
@ -88,44 +88,30 @@ final class RemoveExtraParametersRector extends AbstractRector
|
|||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall|MethodCall|StaticCall $node
|
||||
*/
|
||||
private function shouldSkip(Node $node): bool
|
||||
private function shouldSkip(FuncCall | MethodCall | StaticCall $call): bool
|
||||
{
|
||||
if ($node->args === []) {
|
||||
if ($call->args === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($node instanceof StaticCall) {
|
||||
if (! $node->class instanceof Name) {
|
||||
if ($call instanceof StaticCall) {
|
||||
if (! $call->class instanceof Name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->isName($node->class, 'parent')) {
|
||||
if ($this->isName($call->class, 'parent')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$functionReflection = $this->callReflectionResolver->resolveCall($node);
|
||||
if ($functionReflection === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($functionReflection->getVariants() === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->variadicAnalyzer->hasVariadicParameters($functionReflection);
|
||||
return $this->variadicAnalyzer->hasVariadicParameters($call);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodReflection|FunctionReflection $reflection
|
||||
*/
|
||||
private function resolveMaximumAllowedParameterCount(object $reflection): int
|
||||
{
|
||||
private function resolveMaximumAllowedParameterCount(
|
||||
MethodReflection | FunctionReflection $functionLikeReflection
|
||||
): int {
|
||||
$parameterCounts = [0];
|
||||
foreach ($reflection->getVariants() as $parametersAcceptor) {
|
||||
foreach ($functionLikeReflection->getVariants() as $parametersAcceptor) {
|
||||
$parameterCounts[] = count($parametersAcceptor->getParameters());
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,8 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if (! $this->getStaticType($node->args[1]->value) instanceof ObjectType) {
|
||||
$firstArgStaticType = $this->getStaticType($node->args[1]->value);
|
||||
if (! $firstArgStaticType instanceof ObjectType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,8 @@ final class MbStrrposEncodingArgumentPositionRector extends AbstractRector
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->getStaticType($node->args[2]->value) instanceof IntegerType) {
|
||||
$secondArgType = $this->getStaticType($node->args[2]->value);
|
||||
if ($secondArgType instanceof IntegerType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,12 +68,14 @@ final class PhpSpecMethodToPHPUnitMethodRector extends AbstractPhpSpecToPHPUnitR
|
|||
// reorder instantiation + expected exception
|
||||
$previousStmt = null;
|
||||
foreach ((array) $classMethod->stmts as $key => $stmt) {
|
||||
if ($previousStmt &&
|
||||
\str_contains($this->print($stmt), 'duringInstantiation') &&
|
||||
\str_contains($this->print($previousStmt), 'beConstructedThrough')
|
||||
) {
|
||||
$classMethod->stmts[$key - 1] = $stmt;
|
||||
$classMethod->stmts[$key] = $previousStmt;
|
||||
$printedStmt = $this->print($stmt);
|
||||
|
||||
if ($previousStmt && \str_contains($printedStmt, 'duringInstantiation')) {
|
||||
$printedPreviousStmt = $this->print($previousStmt);
|
||||
if (\str_contains($printedPreviousStmt, 'beConstructedThrough')) {
|
||||
$classMethod->stmts[$key - 1] = $stmt;
|
||||
$classMethod->stmts[$key] = $previousStmt;
|
||||
}
|
||||
}
|
||||
|
||||
$previousStmt = $stmt;
|
||||
|
|
|
@ -87,9 +87,11 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
$printedClass = $this->print($class);
|
||||
|
||||
foreach ($this->useImportsToRestore as $useImportToRestore) {
|
||||
$annotationToSeek = '#\*\s+\@' . $useImportToRestore->getAlias() . '#';
|
||||
if (! Strings::match($this->print($class), $annotationToSeek)) {
|
||||
if (! Strings::match($printedClass, $annotationToSeek)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
70
src/NodeAnalyzer/VariadicAnalyzer.php
Normal file
70
src/NodeAnalyzer/VariadicAnalyzer.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Php\PhpFunctionReflection;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class VariadicAnalyzer
|
||||
{
|
||||
public function __construct(
|
||||
private BetterNodeFinder $betterNodeFinder,
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
private AstResolver $astResolver,
|
||||
private ReflectionResolver $reflectionResolver
|
||||
) {
|
||||
}
|
||||
|
||||
public function hasVariadicParameters(FuncCall | StaticCall | MethodCall $call): bool
|
||||
{
|
||||
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($call);
|
||||
if ($functionLikeReflection === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hasVariadicVariant($functionLikeReflection)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($functionLikeReflection instanceof PhpFunctionReflection) {
|
||||
$function = $this->astResolver->resolveFunctionFromFunctionReflection($functionLikeReflection);
|
||||
if (! $function instanceof Function_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->betterNodeFinder->findFirst($function->stmts, function (Node $node): bool {
|
||||
if (! $node instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isNames($node, ['func_get_args', 'func_num_args', 'func_get_arg']);
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function hasVariadicVariant(MethodReflection | FunctionReflection $functionLikeReflection): bool
|
||||
{
|
||||
foreach ($functionLikeReflection->getVariants() as $parametersAcceptor) {
|
||||
// can be any number of arguments → nothing to limit here
|
||||
if ($parametersAcceptor->isVariadic()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\PHPStan\Reflection;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Parser;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Php\PhpFunctionReflection;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class VariadicAnalyzer
|
||||
{
|
||||
public function __construct(
|
||||
private Parser $parser,
|
||||
private SmartFileSystem $smartFileSystem,
|
||||
private BetterNodeFinder $betterNodeFinder,
|
||||
private NodeNameResolver $nodeNameResolver
|
||||
) {
|
||||
}
|
||||
|
||||
public function hasVariadicParameters(MethodReflection | FunctionReflection $functionReflection): bool
|
||||
{
|
||||
$variants = $functionReflection->getVariants();
|
||||
|
||||
foreach ($variants as $variant) {
|
||||
// can be any number of arguments → nothing to limit here
|
||||
if ($variant->isVariadic()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($functionReflection instanceof PhpFunctionReflection) {
|
||||
$pathsFunctionName = explode('\\', $functionReflection->getName());
|
||||
$functionName = array_pop($pathsFunctionName);
|
||||
|
||||
$fileName = (string) $functionReflection->getFileName();
|
||||
/** @var Node[] $contentNodes */
|
||||
$contentNodes = $this->parser->parse($this->smartFileSystem->readFile($fileName));
|
||||
|
||||
/** @var Function_ $function */
|
||||
$function = $this->betterNodeFinder->findFirst($contentNodes, function (Node $node) use (
|
||||
$functionName
|
||||
): bool {
|
||||
if (! $node instanceof Function_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isName($node, $functionName);
|
||||
});
|
||||
|
||||
return (bool) $this->betterNodeFinder->findFirst($function->stmts, function (Node $node): bool {
|
||||
if (! $node instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isNames($node, ['func_get_args', 'func_num_args', 'func_get_arg']);
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -214,10 +214,18 @@ final class AstResolver
|
|||
return $this->classesByName[$classReflection->getName()];
|
||||
}
|
||||
|
||||
/** @var string $fileName */
|
||||
$fileName = $classReflection->getFileName();
|
||||
|
||||
$nodes = $this->parser->parse($this->smartFileSystem->readFile($fileName));
|
||||
// probably internal class
|
||||
if ($fileName === false) {
|
||||
// avoid parsing falsy-file again
|
||||
$this->classesByName[$classReflection->getName()] = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
$fileContent = $this->smartFileSystem->readFile($fileName);
|
||||
|
||||
$nodes = $this->parser->parse($fileContent);
|
||||
if ($nodes === null) {
|
||||
// avoid parsing falsy-file again
|
||||
$this->classesByName[$classReflection->getName()] = null;
|
||||
|
|
|
@ -4,10 +4,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\Core\Reflection;
|
||||
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\TypeUtils;
|
||||
|
@ -96,6 +99,20 @@ final class ReflectionResolver
|
|||
return $this->resolveMethodReflection($callerType->getClassName(), $methodName);
|
||||
}
|
||||
|
||||
public function resolveFunctionLikeReflectionFromCall(
|
||||
MethodCall | StaticCall | FuncCall $call
|
||||
): MethodReflection | FunctionReflection | null {
|
||||
if ($call instanceof MethodCall) {
|
||||
return $this->resolveMethodReflectionFromMethodCall($call);
|
||||
}
|
||||
|
||||
if ($call instanceof StaticCall) {
|
||||
return $this->resolveMethodReflectionFromStaticCall($call);
|
||||
}
|
||||
|
||||
return $this->resolveFunctionReflectionFromFuncCall($call);
|
||||
}
|
||||
|
||||
public function resolveMethodReflectionFromClassMethod(ClassMethod $classMethod): ?MethodReflection
|
||||
{
|
||||
$class = $classMethod->getAttribute(AttributeKey::CLASS_NAME);
|
||||
|
@ -116,4 +133,19 @@ final class ReflectionResolver
|
|||
|
||||
return $this->resolveMethodReflection($newClassType->getClassName(), MethodName::CONSTRUCT);
|
||||
}
|
||||
|
||||
private function resolveFunctionReflectionFromFuncCall(FuncCall $funcCall): ?FunctionReflection
|
||||
{
|
||||
$functionName = $this->nodeNameResolver->getName($funcCall);
|
||||
if ($functionName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$functionNameFullyQualified = new FullyQualified($functionName);
|
||||
if (! $this->reflectionProvider->hasFunction($functionNameFullyQualified, null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->reflectionProvider->getFunction($functionNameFullyQualified, null);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue6496\Fixture;
|
||||
|
||||
use ScrumWorks\OpenApiSchema\Annotation as OA;
|
||||
|
||||
final class WalletStaticDataView
|
||||
{
|
||||
/**
|
||||
* @OA\Property(description="Map transaction type to label")
|
||||
*/
|
||||
public array $transactionLabelMap;
|
||||
}
|
Loading…
Reference in New Issue
Block a user