Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Rector\CodeQuality\Rector\Catch_;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\NodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -69,15 +74,15 @@ public function refactor(Node $node): ?Node
$catchedThrowableVariable = $node->var;

$this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($catchedThrowableVariable): ?int {
if (! $node instanceof Node\Stmt\Throw_) {
if (! $node instanceof Throw_) {
return null;
}

if (! $node->expr instanceof Node\Expr\New_) {
if (! $node->expr instanceof New_) {
return null;
}

if (! $node->expr->class instanceof Node\Name) {
if (! $node->expr->class instanceof Name) {
return null;
}

Expand All @@ -88,10 +93,10 @@ public function refactor(Node $node): ?Node

if (! isset($node->expr->args[1])) {
// get previous code
$node->expr->args[1] = new Node\Arg(new Node\Expr\MethodCall($catchedThrowableVariable, 'getCode'));
$node->expr->args[1] = new Arg(new MethodCall($catchedThrowableVariable, 'getCode'));
}

$node->expr->args[2] = new Node\Arg($catchedThrowableVariable);
$node->expr->args[2] = new Arg($catchedThrowableVariable);

// nothing more to add
return NodeTraverser::STOP_TRAVERSAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
Expand Down Expand Up @@ -129,7 +131,7 @@ private function collectPropertyNamesWithMissingDefaultArray(Class_ $class): arr
return null;
}

/** @var Node\Stmt\Property $property */
/** @var Property $property */
$property = $node->getAttribute(AttributeKey::PARENT_NODE);

// we need docblock
Expand Down Expand Up @@ -222,7 +224,7 @@ private function clearNotNullBeforeCount(Class_ $class, array $propertyNames): v
$isNextNodeCountingProperty = (bool) $this->betterNodeFinder->findFirst($node->right, function (Node $node) use (
$propertyNames
): ?bool {
if (! $node instanceof Expr\FuncCall) {
if (! $node instanceof FuncCall) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Namespace_;
Expand Down Expand Up @@ -150,19 +151,15 @@ private function importNamesAndCollectNewUseStatements(Name $name): ?Name
{
$originalName = $name->getAttribute('originalName');

if ($originalName instanceof Name) {
// already short
if (! Strings::contains($originalName->toString(), '\\')) {
return null;
}
} else {
if (! $originalName instanceof Name) {
// not sure what to do
return null;
}

// the short name is already used, skip it
// @todo this is duplicated check of - $this->useAddingCommander->isShortImported?
$shortName = $this->classNaming->getShortName($name->toString());

if ($this->isShortNameAlreadyUsedForDifferentFqn($name, $shortName)) {
return null;
}
Expand Down Expand Up @@ -203,16 +200,25 @@ private function importNamesAndCollectNewUseStatements(Name $name): ?Name
// 1. name is fully qualified → import it
private function shouldSkipName(Name $name, string $fullyQualifiedName): bool
{
// not namespaced class
if (! Strings::contains($fullyQualifiedName, '\\')) {
$shortName = $this->classNaming->getShortName($fullyQualifiedName);

$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof ConstFetch) { // is true, false, null etc.
return true;
}

$shortName = $this->classNaming->getShortName($fullyQualifiedName);
if ($this->isNames($name, ['self', 'parent', 'static'])) {
return true;
}

// skip native function calls
if ($parentNode instanceof FuncCall && ! Strings::contains($fullyQualifiedName, '\\')) {
return true;
}

// nothing to change
if ($shortName === $fullyQualifiedName) {
return true;
return false;
}

return $this->useAddingCommander->canImportBeAdded($name, $fullyQualifiedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class StockRepository
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source;

use Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Stock\Query;
use InvalidArgumentException;
use Doctrine\DBAL\Connection;
final class StockRepository
{
Expand All @@ -44,7 +45,7 @@ final class StockRepository
$query = 5;

if (!($query instanceof Stock\Querying\Query)) {
throw new \InvalidArgumentException('Wrong query provided');
throw new InvalidArgumentException('Wrong query provided');
}

/** @var Querying\Query $query */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

class SomeException extends \Exception
{
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

use Exception;
class SomeException extends Exception
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function provideNamespacedClasses(): Iterator
yield [__DIR__ . '/Fixture/already_with_use.php.inc'];
yield [__DIR__ . '/Fixture/already_class_name.php.inc'];
yield [__DIR__ . '/Fixture/no_class.php.inc'];
yield [__DIR__ . '/Fixture/short.php.inc'];

// keep
yield [__DIR__ . '/Fixture/keep.php.inc'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Rector\DeadCode\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
Expand Down Expand Up @@ -117,8 +119,8 @@ private function isParentCallMatching(ClassMethod $classMethod, ?StaticCall $sta
}

/**
* @param Node\Arg[] $args
* @param Node\Param[] $params
* @param Arg[] $args
* @param Param[] $params
*/
private function areArgsAndParamsEqual(array $args, array $params): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rector\DeadCode\Rector\Switch_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Switch_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Node\Stmt\Case_|null $previousCase */
/** @var Case_|null $previousCase */
$previousCase = null;
foreach ($node->cases as $case) {
if ($previousCase && $this->areNodesEqual($case->stmts, $previousCase->stmts)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Rector\Nette\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -89,22 +91,22 @@ public function refactor(Node $node): ?Node
return null;
}

private function refactorJsonEncode(Node\Expr\FuncCall $funcCall): Node\Expr\StaticCall
private function refactorJsonEncode(FuncCall $funcCall): StaticCall
{
$args = $funcCall->args;
if (isset($args[1])) {
$secondArgumentValue = $args[1]->value;

if ($this->isName($secondArgumentValue, 'JSON_PRETTY_PRINT')) {
$prettyClassConstant = $this->createClassConstant('Nette\Utils\Json', 'PRETTY');
$args[1] = new Node\Arg($prettyClassConstant);
$args[1] = new Arg($prettyClassConstant);
}
}

return $this->createStaticCall('Nette\Utils\Json', 'encode', $args);
}

private function refactorJsonDecode(Node\Expr\FuncCall $funcCall): Node\Expr\StaticCall
private function refactorJsonDecode(FuncCall $funcCall): StaticCall
{
$args = $funcCall->args;

Expand All @@ -115,7 +117,7 @@ private function refactorJsonDecode(Node\Expr\FuncCall $funcCall): Node\Expr\Sta
unset($args[1]);
} elseif ($this->isTrue($secondArgumentValue)) {
$forceArrayClassConstant = $this->createClassConstant('Nette\Utils\Json', 'FORCE_ARRAY');
$args[1] = new Node\Arg($forceArrayClassConstant);
$args[1] = new Arg($forceArrayClassConstant);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -607,7 +608,7 @@ private function resolveParamStaticType(Node $node): ?Type
$this->callableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $node) use ($paramName, &$paramStaticType): ?int {
if (! $node instanceof Node\Expr\Variable) {
if (! $node instanceof Variable) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -306,15 +308,15 @@ private function decorateClassMethodWithReturnType(ClassMethod $classMethod): vo
*/
private function createSingleMethod(
array $methodNamesWithPriorities,
Node\Expr $expr,
Expr $expr,
Array_ $eventsToMethodsArray
): void {
[$methodName, $priority] = $methodNamesWithPriorities[0];

if ($priority) {
$methodNameWithPriorityArray = new Array_();
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($methodName));
$methodNameWithPriorityArray->items[] = new ArrayItem(new Node\Scalar\LNumber((int) $priority));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber((int) $priority));

$eventsToMethodsArray->items[] = new ArrayItem($methodNameWithPriorityArray, $expr);
} else {
Expand All @@ -328,7 +330,7 @@ private function createSingleMethod(
*/
private function createMultipleMethods(
array $methodNamesWithPriorities,
Node\Expr $expr,
Expr $expr,
Array_ $eventsToMethodsArray
): void {
$multipleMethodsArray = new Array_();
Expand All @@ -339,7 +341,7 @@ private function createMultipleMethods(
if ($priority) {
$methodNameWithPriorityArray = new Array_();
$methodNameWithPriorityArray->items[] = new ArrayItem(new String_($methodName));
$methodNameWithPriorityArray->items[] = new ArrayItem(new Node\Scalar\LNumber((int) $priority));
$methodNameWithPriorityArray->items[] = new ArrayItem(new LNumber((int) $priority));

$multipleMethodsArray->items[] = new ArrayItem($methodNameWithPriorityArray);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rector\TypeDeclaration\TypeInferer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function inferPropertyInClassLike(string $propertyName, ClassLike $classL
* - $this->propertyName = $expr;
* - $this->propertyName[] = $expr;
*/
private function matchPropertyAssignExpr(Assign $assign, string $propertyName): ?Node\Expr
private function matchPropertyAssignExpr(Assign $assign, string $propertyName): ?Expr
{
if ($assign->var instanceof PropertyFetch) {
if (! $this->nameResolver->isName($assign->var, $propertyName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function inferParam(Param $param): array

$paramName = $this->nameResolver->getName($param);

/** @var Node\Stmt\ClassMethod $classMethod */
/** @var ClassMethod $classMethod */
$classMethod = $param->getAttribute(AttributeKey::PARENT_NODE);

$propertyStaticTypes = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\IntersectionType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\Contract\TypeInferer\PropertyTypeInfererInterface;
Expand All @@ -25,7 +25,7 @@ public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInf
/**
* @return string[]
*/
public function inferProperty(Node\Stmt\Property $property): array
public function inferProperty(Property $property): array
{
/** @var ClassLike $class */
$class = $property->getAttribute(AttributeKey::CLASS_NODE);
Expand Down
Loading