Skip to content

Commit

Permalink
remove unused related node analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 2, 2024
1 parent 4e81d35 commit 5d51203
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 343 deletions.
49 changes: 0 additions & 49 deletions rules/EarlyReturn/NodeAnalyzer/IfAndAnalyzer.php

This file was deleted.

26 changes: 0 additions & 26 deletions rules/EarlyReturn/NodeAnalyzer/SimpleScalarAnalyzer.php

This file was deleted.

55 changes: 0 additions & 55 deletions rules/EarlyReturn/NodeFactory/InvertedIfFactory.php

This file was deleted.

194 changes: 0 additions & 194 deletions rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,25 @@
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\UnionType;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\Parser\InlineCodeParser;
use Rector\PhpParser\Parser\SimplePhpParser;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;

final readonly class AnonymousFunctionFactory
{
Expand All @@ -56,11 +40,8 @@
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
private NodeFactory $nodeFactory,
private StaticTypeMapper $staticTypeMapper,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private SimplePhpParser $simplePhpParser,
private AstResolver $astResolver,
private InlineCodeParser $inlineCodeParser
) {
}
Expand Down Expand Up @@ -97,42 +78,6 @@ public function create(
return $anonymousFunctionClosure;
}

public function createFromPhpMethodReflection(PhpMethodReflection $phpMethodReflection, Expr $expr): ?Closure
{
/** @var FunctionVariantWithPhpDocs $parametersAcceptorWithPhpDocs */
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle($phpMethodReflection->getVariants());

$newParams = $this->createParams($phpMethodReflection, $parametersAcceptorWithPhpDocs->getParameters());

$innerMethodCall = $this->createInnerMethodCall($phpMethodReflection, $expr, $newParams);
if ($innerMethodCall === null) {
return null;
}

$returnTypeNode = null;
if (! $parametersAcceptorWithPhpDocs->getReturnType() instanceof MixedType) {
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$parametersAcceptorWithPhpDocs->getReturnType(),
TypeKind::RETURN
);
}

$uses = [];
if ($expr instanceof Variable && ! $this->nodeNameResolver->isName($expr, 'this')) {
$uses[] = new ClosureUse($expr);
}

// does method return something?
$stmts = $this->resolveStmts($parametersAcceptorWithPhpDocs, $innerMethodCall);

return new Closure([
'params' => $newParams,
'returnType' => $returnTypeNode,
'uses' => $uses,
'stmts' => $stmts,
]);
}

public function createAnonymousFunctionFromExpr(Expr $expr): ?Closure
{
$stringValue = $this->inlineCodeParser->stringify($expr);
Expand Down Expand Up @@ -239,143 +184,4 @@ private function createUseVariablesFromParams(array $nodes, array $params): arra

return $filteredVariables;
}

/**
* @param ParameterReflection[] $parameterReflections
* @return Param[]
*/
private function createParams(PhpMethodReflection $phpMethodReflection, array $parameterReflections): array
{
$classReflection = $phpMethodReflection->getDeclaringClass();
$className = $classReflection->getName();
$methodName = $phpMethodReflection->getName();
/** @var ClassMethod $classMethod */
$classMethod = $this->astResolver->resolveClassMethod($className, $methodName);

$params = [];
foreach ($parameterReflections as $key => $parameterReflection) {
$variable = new Variable($parameterReflection->getName());
$defaultExpr = $this->resolveParamDefaultExpr($parameterReflection, $key, $classMethod);
$type = $this->resolveParamType($parameterReflection);
$byRef = $parameterReflection->passedByReference()
->yes();

$params[] = new Param($variable, $defaultExpr, $type, $byRef);
}

return $params;
}

private function resolveParamType(ParameterReflection $parameterReflection): Name|ComplexType|Identifier|null
{
if ($parameterReflection->getType() instanceof MixedType) {
return null;
}

return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$parameterReflection->getType(),
TypeKind::PARAM
);
}

private function resolveParamDefaultExpr(
ParameterReflection $parameterReflection,
int $key,
ClassMethod $classMethod
): ?Expr {
if (! $parameterReflection->getDefaultValue() instanceof Type) {
return null;
}

$paramDefaultExpr = $classMethod->params[$key]->default;
if (! $paramDefaultExpr instanceof Expr) {
return null;
}

return $this->nodeFactory->createReprintedNode($paramDefaultExpr);
}

/**
* @param Param[] $params
*/
private function createInnerMethodCall(
PhpMethodReflection $phpMethodReflection,
Expr $expr,
array $params
): MethodCall | StaticCall | null {
if ($phpMethodReflection->isStatic()) {
$expr = $this->normalizeClassConstFetchForStatic($expr);
if (! $expr instanceof Node) {
return null;
}

$innerMethodCall = new StaticCall($expr, $phpMethodReflection->getName());
} else {
$expr = $this->resolveExpr($expr);
if (! $expr instanceof Expr) {
return null;
}

$innerMethodCall = new MethodCall($expr, $phpMethodReflection->getName());
}

$innerMethodCall->args = $this->nodeFactory->createArgsFromParams($params);

return $innerMethodCall;
}

private function normalizeClassConstFetchForStatic(Expr $expr): null | Name | FullyQualified | Expr
{
if (! $expr instanceof ClassConstFetch) {
return $expr;
}

if (! $this->nodeNameResolver->isName($expr->name, 'class')) {
return $expr;
}

// dynamic name, nothing we can do
$className = $this->nodeNameResolver->getName($expr->class);
if ($className === null) {
return null;
}

$name = new Name($className);
if ($name->isSpecialClassName()) {
return $name;
}

return new FullyQualified($className);
}

private function resolveExpr(Expr $expr): New_ | Expr | null
{
if (! $expr instanceof ClassConstFetch) {
return $expr;
}

if (! $this->nodeNameResolver->isName($expr->name, 'class')) {
return $expr;
}

// dynamic name, nothing we can do
$className = $this->nodeNameResolver->getName($expr->class);
return $className === null
? null
: new New_(new FullyQualified($className));
}

/**
* @return Stmt[]
*/
private function resolveStmts(
FunctionVariantWithPhpDocs $functionVariantWithPhpDocs,
StaticCall|MethodCall $innerMethodCall
): array {
if ($functionVariantWithPhpDocs->getReturnType()->isVoid()->yes()) {
return [new Expression($innerMethodCall)];
}

return [new Return_($innerMethodCall)];
}
}
5 changes: 0 additions & 5 deletions src/NodeManipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ public function isIfWithOnly(If_ $if, string $stmtClass): bool
return $this->hasOnlyStmtOfType($if, $stmtClass);
}

public function isIfWithOnlyOneStmt(If_ $if): bool
{
return count($if->stmts) === 1;
}

public function isIfWithoutElseAndElseIfs(If_ $if): bool
{
if ($if->else instanceof Else_) {
Expand Down
Loading

0 comments on commit 5d51203

Please sign in to comment.