Skip to content

Commit

Permalink
static fixes (#2661)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 14, 2022
1 parent 14b5cb1 commit 328c437
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 40 deletions.
3 changes: 3 additions & 0 deletions packages/PostRector/Collector/PropertyToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function addConstantToClass(Class_ $class, ClassConst $classConst): void
$this->rectorChangeCollector->notifyNodeFileInfo($class);
}

/**
* @api
*/
public function addPropertyWithoutConstructorToClass(
string $propertyName,
?Type $propertyType,
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,5 @@ parameters:
- '#Class has a static method must so must contains "Static" in its name#'

- '#Class method ".+\(\)" is never used#'

- '#Parameters should use "(.*?)" types as the only types passed to this method#'
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public function refactor(Node $node): ?BinaryOp
return null;
}

return $this->binaryOpManipulator->inverseBinaryOp($node->expr);
return $this->binaryOpManipulator->inverseBooleanOr($node->expr);
}
}
12 changes: 6 additions & 6 deletions src/NodeManipulator/BinaryOpManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ public function matchFirstAndSecondConditionNode(
return new TwoNodeMatch($binaryOp->right, $binaryOp->left);
}

public function inverseBinaryOp(BinaryOp $binaryOp): ?BinaryOp
public function inverseBooleanOr(BooleanOr $booleanOr): ?BinaryOp
{
// no nesting
if ($binaryOp->left instanceof BooleanOr) {
if ($booleanOr->left instanceof BooleanOr) {
return null;
}

if ($binaryOp->right instanceof BooleanOr) {
if ($booleanOr->right instanceof BooleanOr) {
return null;
}

$inversedNodeClass = $this->resolveInversedNodeClass($binaryOp);
$inversedNodeClass = $this->resolveInversedNodeClass($booleanOr);
if ($inversedNodeClass === null) {
return null;
}

$firstInversedNode = $this->inverseNode($binaryOp->left);
$secondInversedNode = $this->inverseNode($binaryOp->right);
$firstInversedNode = $this->inverseNode($booleanOr->left);
$secondInversedNode = $this->inverseNode($booleanOr->right);

return new $inversedNodeClass($firstInversedNode, $secondInversedNode);
}
Expand Down
4 changes: 2 additions & 2 deletions src/NodeManipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ public function isIfWithoutElseAndElseIfs(If_ $if): bool
return ! (bool) $if->elseifs;
}

public function createIfNegation(Expr $expr, Stmt $stmt): If_
public function createIfNegation(Expr $expr, Return_ $return): If_
{
$expr = $this->conditionInverter->createInvertedCondition($expr);
return $this->createIfStmt($expr, $stmt);
return $this->createIfStmt($expr, $return);
}

public function createIfStmt(Expr $expr, Stmt $stmt): If_
Expand Down
13 changes: 6 additions & 7 deletions src/NodeManipulator/MagicPropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -31,10 +31,9 @@ public function __construct(
) {
}

public function isMagicOnType(PropertyFetch | StaticPropertyFetch $expr, Type $type): bool
public function isMagicOnType(PropertyFetch $propertyFetch, ObjectType $objectType): bool
{
$varNodeType = $this->nodeTypeResolver->getType($expr);

$varNodeType = $this->nodeTypeResolver->getType($propertyFetch);
if ($varNodeType instanceof ErrorType) {
return true;
}
Expand All @@ -43,16 +42,16 @@ public function isMagicOnType(PropertyFetch | StaticPropertyFetch $expr, Type $t
return false;
}

if ($varNodeType->isSuperTypeOf($type)->yes()) {
if ($varNodeType->isSuperTypeOf($objectType)->yes()) {
return false;
}

$nodeName = $this->nodeNameResolver->getName($expr->name);
$nodeName = $this->nodeNameResolver->getName($propertyFetch->name);
if ($nodeName === null) {
return false;
}

return ! $this->hasPublicProperty($expr, $nodeName);
return ! $this->hasPublicProperty($propertyFetch, $nodeName);
}

private function hasPublicProperty(PropertyFetch | StaticPropertyFetch $expr, string $propertyName): bool
Expand Down
1 change: 1 addition & 0 deletions src/PhpParser/Comparing/NodeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function areNodesEqual(Node | array | null $firstNode, Node | array | nul
}

/**
* @api
* @param Node[] $availableNodes
*/
public function isNodeEqual(Node $singleNode, array $availableNodes): bool
Expand Down
12 changes: 8 additions & 4 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public function findFirstInstanceOf(Node | array $nodes, string $type): ?Node

/**
* @param class-string<Node> $type
* @param Node|Node[] $nodes
* @param Node[] $nodes
*/
public function hasInstanceOfName(Node | array $nodes, string $type, string $name): bool
public function hasInstanceOfName(array $nodes, string $type, string $name): bool
{
Assert::isAOf($type, Node::class);
return (bool) $this->findInstanceOfName($nodes, $type, $name);
Expand Down Expand Up @@ -180,11 +180,13 @@ public function hasInstancesOf(Node | array $nodes, array $types): bool

/**
* @template T of Node
*
* @param Stmt[] $nodes
* @param class-string<T> $type
* @param Node|Node[] $nodes
*/
public function findLastInstanceOf(Node | array $nodes, string $type): ?Node
public function findLastInstanceOf(array $nodes, string $type): ?Node
{
Assert::allIsAOf($nodes, Stmt::class);
Assert::isAOf($type, Node::class);

$foundInstances = $this->nodeFinder->findInstanceOf($nodes, $type);
Expand Down Expand Up @@ -271,6 +273,7 @@ public function findPreviousAssignToExpr(Expr $expr): ?Node

/**
* Only search in previous Node/Stmt
* @api
*
* @param callable(Node $node): bool $filter
*/
Expand Down Expand Up @@ -364,6 +367,7 @@ public function findFirstNext(Node $node, callable $filter): ?Node
}

/**
* @api
* @return Expr[]
*/
public function findSameNamedExprs(Expr | Variable | Property | PropertyFetch | StaticPropertyFetch $expr): array
Expand Down
6 changes: 3 additions & 3 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Core\ProcessAnalyzer;

use PhpParser\Node;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\RectifiedNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -27,7 +27,7 @@ public function __construct(private readonly NodeComparator $nodeComparator)
{
}

public function verify(RectorInterface $rector, Node $node, File $currentFile): ?RectifiedNode
public function verify(AbstractRector $rector, Node $node, File $currentFile): ?RectifiedNode
{
$smartFileInfo = $currentFile->getSmartFileInfo();
$realPath = $smartFileInfo->getRealPath();
Expand All @@ -48,7 +48,7 @@ public function verify(RectorInterface $rector, Node $node, File $currentFile):
return $rectifiedNode;
}

private function shouldContinue(RectifiedNode $rectifiedNode, RectorInterface $rector, Node $node): bool
private function shouldContinue(RectifiedNode $rectifiedNode, AbstractRector $rector, Node $node): bool
{
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
if ($rectifiedNode->getRectorClass() === $rector::class && $rectifiedNode->getNode() === $node) {
Expand Down
23 changes: 6 additions & 17 deletions src/Reflection/ReflectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -54,6 +53,9 @@ public function autowire(AstResolver $astResolver): void
$this->astResolver = $astResolver;
}

/**
* @api
*/
public function resolveClassAndAnonymousClass(ClassLike $classLike): ClassReflection
{
if ($classLike instanceof Class_ && $this->classAnalyzer->isAnonymousClass($classLike)) {
Expand Down Expand Up @@ -82,23 +84,10 @@ public function resolveClassReflection(?Node $node): ?ClassReflection
return $scope->getClassReflection();
}

public function resolveClassReflectionSourceObject(New_|MethodCall|StaticCall $node): ?ClassReflection
public function resolveClassReflectionSourceObject(MethodCall|StaticCall $call): ?ClassReflection
{
if ($node instanceof New_ && $node->class instanceof FullyQualified) {
$className = $node->class->toString();
if ($this->reflectionProvider->hasClass($className)) {
return $this->reflectionProvider->getClass($className);
}

return null;
}

if ($node instanceof MethodCall || $node instanceof StaticCall) {
$classMethod = $this->astResolver->resolveClassMethodFromCall($node);
return $this->resolveClassReflection($classMethod);
}

return null;
$classMethod = $this->astResolver->resolveClassMethodFromCall($call);
return $this->resolveClassReflection($classMethod);
}

/**
Expand Down

0 comments on commit 328c437

Please sign in to comment.