Skip to content

Commit

Permalink
[DX] remove isStaticCallNamed() (#5913)
Browse files Browse the repository at this point in the history
Co-authored-by: kaizen-ci <[email protected]>
  • Loading branch information
TomasVotruba and kaizen-ci authored Mar 20, 2021
1 parent c01d969 commit 9d101f6
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 123 deletions.
56 changes: 0 additions & 56 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -213,61 +212,6 @@ public function endsWith(string $currentName, string $expectedName): bool
return (bool) Strings::match($currentName, $suffixNamePattern);
}

public function isLocalMethodCallNamed(Node $node, string $name): bool
{
if (! $node instanceof MethodCall) {
return false;
}

if ($node->var instanceof StaticCall) {
return false;
}

if ($node->var instanceof MethodCall) {
return false;
}

if (! $this->isName($node->var, 'this')) {
return false;
}

return $this->isName($node->name, $name);
}

/**
* @param string[] $names
*/
public function isLocalMethodCallsNamed(Node $node, array $names): bool
{
foreach ($names as $name) {
if ($this->isLocalMethodCallNamed($node, $name)) {
return true;
}
}

return false;
}

/**
* @deprecated Helper function causes to lose the type on the outside. Better avoid it
*/
public function isStaticCallNamed(Node $node, string $className, string $methodName): bool
{
if (! $node instanceof StaticCall) {
return false;
}

if ($node->class instanceof New_) {
if (! $this->isName($node->class->class, $className)) {
return false;
}
} elseif (! $this->isName($node->class, $className)) {
return false;
}

return $this->isName($node->name, $methodName);
}

/**
* @param ObjectType[] $desiredObjectTypes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Type\ObjectType;
use Rector\CakePHP\Naming\CakePHPFullyQualifiedClassNameResolver;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -94,7 +95,12 @@ private function collectAppUseStaticCalls(Node $node): array
return false;
}

return $this->nodeNameResolver->isStaticCallNamed($node, 'App', 'uses');
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('App'))->yes()) {
return false;
}

return $this->isName($node->name, 'uses');
});

return $appUsesStaticCalls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private function isVariableNameAlreadyDefined(If_ $if, string $variableName): bo
return false;
}

return $scope->hasVariableType($variableName)->yes();
return $scope->hasVariableType($variableName)
->yes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\DependencyInjection\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -139,7 +140,15 @@ private function hasParentCallOfMethod(ClassMethod $classMethod, string $method)
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use (
$method
): bool {
return $this->nodeNameResolver->isStaticCallNamed($node, 'parent', $method);
if (! $node instanceof StaticCall) {
return false;
}

if (! $this->isName($node->class, 'parent')) {
return false;
}

return $this->isName($node->name, $method);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\MockeryToProphecy\Collector\MockVariableCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -96,12 +97,21 @@ private function replaceMockCreationsAndCollectVariableNames(ClassMethod $classM
}

$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node): ?MethodCall {
if (! $this->nodeNameResolver->isStaticCallNamed($node, 'Mockery', 'mock')) {
if (! $node instanceof StaticCall) {
return null;
}

$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('Mockery'))->yes()) {
return null;
}

if (! $this->isName($node->name, 'mock')) {
return null;
}

/** @var StaticCall $node */
$collectedVariableTypesByNames = $this->mockVariableCollector->collectMockVariableName($node);

$this->mockVariableTypesByNames = array_merge(
$this->mockVariableTypesByNames,
$collectedVariableTypesByNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -43,7 +44,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->nodeNameResolver->isStaticCallNamed($node, 'Mockery', 'close')) {
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('Mockery'))->yes()) {
return null;
}

if (! $this->isName($node->name, 'close')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -58,7 +59,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_Settings', 'setChartRenderer')) {
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('PHPExcel_Settings'))->yes()) {
return null;
}

if (! $this->nodeNameResolver->isName($node->name, 'setChartRenderer')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -60,7 +61,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_Cell_DataType', 'dataTypeForValue')) {
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('PHPExcel_Cell_DataType'))->yes()) {
return null;
}

if (! $this->nodeNameResolver->isName($node->name, 'dataTypeForValue')) {
return null;
}

Expand Down
23 changes: 17 additions & 6 deletions rules/PHPOffice/Rector/StaticCall/ChangePdfWriterRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -62,17 +64,17 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_Settings', 'setPdfRendererName')) {
$this->removeNode($node);
return null;
}
$callerType = $this->nodeTypeResolver->resolve($node->class);

if ($this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_Settings', 'setPdfRenderer')) {
if ($this->isSettingsPdfRendererStaticCall($callerType, $node)) {
$this->removeNode($node);
return null;
}

if ($this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_IOFactory', 'createWriter')) {
if ($callerType->isSuperTypeOf(new ObjectType('PHPExcel_IOFactory'))->yes() && $this->nodeNameResolver->isName(
$node->name,
'createWriter'
)) {
if (! isset($node->args[1])) {
return null;
}
Expand All @@ -85,4 +87,13 @@ public function refactor(Node $node): ?Node

return $node;
}

private function isSettingsPdfRendererStaticCall(Type $callerType, StaticCall $staticCall): bool
{
if (! $callerType->isSuperTypeOf(new ObjectType('PHPExcel_Settings'))->yes()) {
return false;
}

return $this->nodeNameResolver->isNames($staticCall->name, ['setPdfRendererName', 'setPdfRenderer']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -61,7 +62,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isStaticCallNamed($node, 'PHPExcel_IOFactory', 'addSearchLocation')) {
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('PHPExcel_IOFactory'))->yes()) {
return null;
}

if (! $this->isName($node->name, 'addSearchLocation')) {
return null;
}

Expand Down
25 changes: 24 additions & 1 deletion rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\Php70\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -97,7 +99,7 @@ public function refactor(Node $node): ?Node
/** @var Expression $stmt */
$stmt = $node->stmts[0];

if ($this->nodeNameResolver->isLocalMethodCallNamed($stmt->expr, MethodName::CONSTRUCT)) {
if ($this->isLocalMethodCallNamed($stmt->expr, MethodName::CONSTRUCT)) {
$this->removeNode($node);

return null;
Expand Down Expand Up @@ -204,4 +206,25 @@ private function resolveParentClassName(StaticCall $staticCall): ?string

return $parentClassReflection->getName();
}

private function isLocalMethodCallNamed(Expr $expr, string $name): bool
{
if (! $expr instanceof MethodCall) {
return false;
}

if ($expr->var instanceof StaticCall) {
return false;
}

if ($expr->var instanceof MethodCall) {
return false;
}

if (! $this->isName($expr->var, 'this')) {
return false;
}

return $this->isName($expr->name, $name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -57,7 +58,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->nodeNameResolver->isStaticCallNamed($node, 'ReflectionFunction', 'export')) {
$callerType = $this->nodeTypeResolver->resolve($node->class);
if (! $callerType->isSuperTypeOf(new ObjectType('ReflectionFunction'))->yes()) {
return null;
}

if (! $this->isName($node->name, 'export')) {
return null;
}

Expand Down
Loading

0 comments on commit 9d101f6

Please sign in to comment.