Skip to content

Commit

Permalink
[PHP 8.0] Run union types for filled param type too (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 29, 2021
1 parent 2a394c7 commit dfd0483
Show file tree
Hide file tree
Showing 77 changed files with 220 additions and 466 deletions.
6 changes: 1 addition & 5 deletions packages/NodeCollector/NodeCollector/ParsedNodeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -100,10 +99,7 @@ private function addClass(Class_ $class): void
$this->classes[$className] = $class;
}

/**
* @param Interface_|Trait_ $classLike
*/
private function collectInterfaceOrTrait(ClassLike $classLike): void
private function collectInterfaceOrTrait(Interface_ | Trait_ $classLike): void
{
$name = $this->nodeNameResolver->getName($classLike);
if ($name === null) {
Expand Down
6 changes: 1 addition & 5 deletions packages/NodeNameResolver/Error/InvalidNameNodeReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\NodeNameResolver\Error;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Contract\Rector\RectorInterface;
Expand All @@ -27,10 +26,7 @@ public function __construct(
) {
}

/**
* @param MethodCall|StaticCall $node
*/
public function reportInvalidNodeForName(Node $node): void
public function reportInvalidNodeForName(MethodCall | StaticCall $node): void
{
$message = sprintf('Pick more specific node than "%s", e.g. "$node->name"', $node::class);

Expand Down
5 changes: 1 addition & 4 deletions packages/NodeRemoval/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public function removeParam(ClassMethod $classMethod, int | Param $keyOrParam):
unset($classMethod->params[$key]);
}

/**
* @param FuncCall|MethodCall|StaticCall $node
*/
public function removeArg(Node $node, int $key): void
public function removeArg(FuncCall | MethodCall | StaticCall $node, int $key): void
{
if ($node->args === null) {
throw new ShouldNotHappenException();
Expand Down
5 changes: 1 addition & 4 deletions packages/PostRector/Collector/NodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ private function resolveNearestStmtPosition(Node $node): string
return spl_object_hash($foundNode);
}

/**
* @param Expr|Stmt $node
*/
private function wrapToExpression(Node $node): Stmt
private function wrapToExpression(Expr | Stmt $node): Stmt
{
return $node instanceof Stmt ? $node : new Expression($node);
}
Expand Down
6 changes: 1 addition & 5 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Type\ObjectType;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -42,10 +41,7 @@ public function isActive(): bool
return $this->useImportTypesInFilePath !== [] || $this->functionUseImportTypesInFilePath !== [];
}

/**
* @param FullyQualifiedObjectType|AliasedObjectType $objectType
*/
public function addUseImport(ObjectType $objectType): void
public function addUseImport(FullyQualifiedObjectType | AliasedObjectType $objectType): void
{
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public function getShortName(): string
return $this->getClassName();
}

/**
* @param AliasedObjectType|FullyQualifiedObjectType $comparedObjectType
*/
public function areShortNamesEqual(ObjectType $comparedObjectType): bool
public function areShortNamesEqual(self | FullyQualifiedObjectType $comparedObjectType): bool
{
return $this->getShortName() === $comparedObjectType->getShortName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public function getShortNameType(): ShortenedObjectType
return new ShortenedObjectType($this->getShortName(), $this->getClassName());
}

/**
* @param AliasedObjectType|FullyQualifiedObjectType $comparedObjectType
*/
public function areShortNamesEqual(ObjectType $comparedObjectType): bool
{
public function areShortNamesEqual(
AliasedObjectType | \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $comparedObjectType
): bool {
return $this->getShortName() === $comparedObjectType->getShortName();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;

final class ParamDoc
{
/**
* @param ClassMethod|Property|ClassConst|ClassMethod $stmt
*/
public function addAsFirstMethod(Class_ $class, Stmt $stmt): void
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;

final class ParamDoc
{
public function addAsFirstMethod(Class_ $class, \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $stmt): void
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php80\Rector\FunctionLike\UnionTypesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(UnionTypesRector::class);

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::UNION_TYPES);
};
10 changes: 4 additions & 6 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
Expand Down Expand Up @@ -39,11 +38,10 @@ public function processReplaces(
return $node;
}

/**
* @param MethodCall|StaticCall|FuncCall $expr
*/
private function processArgs(Expr $expr, ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue): void
{
private function processArgs(
MethodCall | StaticCall | FuncCall $expr,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
): void {
$position = $replaceArgumentDefaultValue->getPosition();

$argValue = $this->valueResolver->getValue($expr->args[$position]->value);
Expand Down
6 changes: 1 addition & 5 deletions rules/Arguments/NodeAnalyzer/ArgumentAddingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Arguments\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -33,10 +32,7 @@ public function __construct(
) {
}

/**
* @param MethodCall|StaticCall $expr
*/
public function isInCorrectScope(Expr $expr, ArgumentAdder $argumentAdder): bool
public function isInCorrectScope(MethodCall | StaticCall $expr, ArgumentAdder $argumentAdder): bool
{
if ($argumentAdder->getScope() === null) {
return true;
Expand Down
5 changes: 1 addition & 4 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ public function configure(array $configuration): void
$this->addedArguments = $addedArguments;
}

/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
private function isObjectTypeMatch(Node $node, ObjectType $objectType): bool
private function isObjectTypeMatch(MethodCall | StaticCall | ClassMethod $node, ObjectType $objectType): bool
{
if ($node instanceof MethodCall) {
return $this->isObjectType($node->var, $objectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
Expand Down Expand Up @@ -103,10 +102,9 @@ private function shouldSkip(Assign $assign): bool
}

/**
* @param Array_|List_ $expr
* @return Assign[]
*/
private function createStandaloneAssigns(Expr $expr, Array_ $rightArray): array
private function createStandaloneAssigns(Array_ | List_ $expr, Array_ $rightArray): array
{
$standaloneAssigns = [];
foreach ($expr->items as $key => $leftArrayItem) {
Expand All @@ -126,21 +124,15 @@ private function createStandaloneAssigns(Expr $expr, Array_ $rightArray): array
return $standaloneAssigns;
}

/**
* @param Array_|List_ $expr
*/
private function isValueSwap(Expr $expr, Array_ $secondArray): bool
private function isValueSwap(Array_ | List_ $expr, Array_ $secondArray): bool
{
$firstArrayItemsHash = $this->getArrayItemsHash($expr);
$secondArrayItemsHash = $this->getArrayItemsHash($secondArray);

return $firstArrayItemsHash === $secondArrayItemsHash;
}

/**
* @param Array_|List_ $node
*/
private function getArrayItemsHash(Node $node): string
private function getArrayItemsHash(Array_ | List_ $node): string
{
$arrayItemsHashes = [];
foreach ($node->items as $arrayItem) {
Expand Down
5 changes: 1 addition & 4 deletions rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ private function isIfBodyABoolReturnNode(If_ $if): bool
return $this->valueResolver->isTrueOrFalse($ifStatment->expr);
}

/**
* @param Identical|Equal $binaryOp
*/
private function createInArrayFunction(Expr $expr, BinaryOp $binaryOp, Foreach_ $foreach): FuncCall
private function createInArrayFunction(Expr $expr, Identical | Equal $binaryOp, Foreach_ $foreach): FuncCall
{
$arguments = $this->nodeFactory->createArgs([$expr, $foreach->expr]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ private function shouldSkip(If_ $if): bool
return ! $this->nodeComparator->areNodesEqual($if->cond->vars[0], $firstElseStmt->expr->var);
}

/**
* @param If_|Else_ $node
*/
private function hasOnlyStatementAssign(Node $node): bool
private function hasOnlyStatementAssign(If_ | Else_ $node): bool
{
if (count($node->stmts) !== 1) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ private function isReturnWithVarAnnotation(Return_ $return): bool
return ! $phpDocInfo->getVarType() instanceof MixedType;
}

/**
* @param AssignOp|Assign $previousNode
*/
private function isPreviousExpressionVisuallySimilar(Expression $previousExpression, Node $previousNode): bool
{
private function isPreviousExpressionVisuallySimilar(
Expression $previousExpression,
AssignOp | Assign $previousNode
): bool {
$prePreviousExpression = $previousExpression->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
if (! $prePreviousExpression instanceof Expression) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Core\Exception\ShouldNotHappenException;
Expand All @@ -16,10 +15,9 @@
final class ClassConstFetchFactory
{
/**
* @param ObjectType|UnionType $type
* @return ClassConstFetch[]
*/
public function createFromType(Type $type): array
public function createFromType(ObjectType | UnionType $type): array
{
$classConstTypes = [];
if ($type instanceof ShortenedObjectType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ private function isExclusivelyObjectType(Type $type): bool

/**
* @param Param[] $params
* @param ObjectType|UnionType $type
* @return ObjectType|UnionType
*/
private function getToBeProcessedTypes(array $params, string $key, Type $type): ?Type
private function getToBeProcessedTypes(array $params, string $key, ObjectType | UnionType $type): ?Type
{
foreach ($params as $param) {
$paramName = ltrim($key, '$');
Expand Down
Loading

0 comments on commit dfd0483

Please sign in to comment.