Skip to content

Commit

Permalink
Reflection cleanup + inline union types for couple of param docs (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jun 29, 2021
1 parent 75c7757 commit 2a394c7
Show file tree
Hide file tree
Showing 34 changed files with 133 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): void
}

// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::KIND_RETURN);
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::KIND_RETURN
);
$currentReturnTagValueNode = $phpDocInfo->getReturnTagValue();

if ($currentReturnTagValueNode !== null) {
Expand Down
3 changes: 1 addition & 2 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ private function isPropertyAssignedInClassMethod(
ClassMethod $classMethod,
string $propertyName,
string $kindPropertyFetch
): bool
{
): bool {
if ($classMethod->stmts === null) {
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/NodeNestingScope/ParentScopeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public function __construct(
) {
}

/**
* @return ClassMethod|Function_|Class_|Namespace_|Closure|null
*/
public function find(Node $node): ?Node
public function find(Node $node): ClassMethod | Function_ | Class_ | Namespace_ | Closure | null
{
return $this->betterNodeFinder->findParentTypes($node, [
Closure::class,
Expand Down
11 changes: 4 additions & 7 deletions packages/NodeRemoval/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,16 @@ public function removeNodes(array $nodes): void
}
}

/**
* @param Closure|ClassMethod|Function_ $node
*/
public function removeStmt(Node $node, int $key): void
public function removeStmt(Closure | ClassMethod | Function_ $functionLike, int $key): void
{
if ($node->stmts === null) {
if ($functionLike->stmts === null) {
throw new ShouldNotHappenException();
}

// notify about remove node
$this->rectorChangeCollector->notifyNodeFileInfo($node->stmts[$key]);
$this->rectorChangeCollector->notifyNodeFileInfo($functionLike->stmts[$key]);

unset($node->stmts[$key]);
unset($functionLike->stmts[$key]);
}

public function removeParam(ClassMethod $classMethod, int | Param $keyOrParam): void
Expand Down
5 changes: 1 addition & 4 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ public function __construct(
) {
}

/**
* @param MethodCall|StaticCall|ClassMethod|Expr\FuncCall $node
*/
public function processReplaces(
Node $node,
MethodCall | StaticCall | ClassMethod | FuncCall $node,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
): ?Node {
if ($node instanceof ClassMethod) {
Expand Down
9 changes: 4 additions & 5 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,10 @@ private function processPositionWithDefaultValues(
}
}

/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
private function shouldSkipParameter(Node $node, ArgumentAdder $argumentAdder): bool
{
private function shouldSkipParameter(
ClassMethod | MethodCall | StaticCall $node,
ArgumentAdder $argumentAdder
): bool {
$position = $argumentAdder->getPosition();
$argumentName = $argumentAdder->getArgumentName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -88,10 +87,7 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param ClassMethod|Function_ $functionLike
*/
private function matchFuncGetArgsVariableAssign(FunctionLike $functionLike): ?Assign
private function matchFuncGetArgsVariableAssign(ClassMethod | Function_ $functionLike): ?Assign
{
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ private function resolveCurrentStmtVariableName(Stmt $stmt): ?string
return null;
}

/**
* @param ClassMethod|Function_|Closure $node
*/
private function shouldAddEmptyLine(?string $currentStmtVariableName, Node $node, int $key): bool
{
private function shouldAddEmptyLine(
?string $currentStmtVariableName,
ClassMethod | Function_ | Closure $node,
int $key
): bool {
if (! $this->isNewVariableThanBefore($currentStmtVariableName)) {
return false;
}
Expand Down Expand Up @@ -173,10 +173,7 @@ private function isNewVariableThanBefore(?string $currentStmtVariableName): bool
return $this->previousStmtVariableName !== $currentStmtVariableName;
}

/**
* @param ClassMethod|Function_|Closure $node
*/
private function isPrecededByEmptyLine(Node $node, int $key): bool
private function isPrecededByEmptyLine(ClassMethod | Function_ | Closure $node, int $key): bool
{
if ($node->stmts === null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -105,10 +104,7 @@ private function isNullableParam(Param $param): bool
return $param->type instanceof NullableType;
}

/**
* @param ClassMethod|Function_|Closure $functionLike
*/
private function refactorParamType(Param $param, FunctionLike $functionLike): bool
private function refactorParamType(Param $param, ClassMethod | Function_ | Closure $functionLike): bool
{
if (! $this->isNullableParam($param)) {
return false;
Expand All @@ -120,10 +116,7 @@ private function refactorParamType(Param $param, FunctionLike $functionLike): bo
return true;
}

/**
* @param ClassMethod|Function_|Closure $functionLike
*/
private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void
private function decorateWithDocBlock(ClassMethod | Function_ | Closure $functionLike, Param $param): void
{
if ($param->type === null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand All @@ -30,10 +29,9 @@ public function __construct(
}

/**
* @param ClassMethod|Function_|Closure $functionLike
* @return bool True if node was changed
*/
public function decorate(FunctionLike $functionLike): bool
public function decorate(ClassMethod | Function_ | Closure $functionLike): bool
{
if ($functionLike->returnType === null) {
return false;
Expand All @@ -49,11 +47,13 @@ public function decorate(FunctionLike $functionLike): bool
}

/**
* @param ClassMethod|Function_ $functionLike
* @param array<class-string<Type>> $requiredTypes
*/
public function decorateParam(Param $param, FunctionLike $functionLike, array $requiredTypes): void
{
public function decorateParam(
Param $param,
ClassMethod | Function_ | Closure $functionLike,
array $requiredTypes
): void {
if ($param->type === null) {
return;
}
Expand All @@ -67,12 +67,9 @@ public function decorateParam(Param $param, FunctionLike $functionLike, array $r
$this->moveParamTypeToParamDoc($functionLike, $param, $type);
}

/**
* @param ClassMethod|Function_ $functionLike
*/
public function decorateParamWithSpecificType(
Param $param,
FunctionLike $functionLike,
ClassMethod | Function_ | Closure $functionLike,
Type $requireType
): void {
if ($param->type === null) {
Expand All @@ -88,11 +85,12 @@ public function decorateParamWithSpecificType(
}

/**
* @param ClassMethod|Function_|Closure $functionLike
* @return bool True if node was changed
*/
public function decorateReturnWithSpecificType(FunctionLike $functionLike, Type $requireType): bool
{
public function decorateReturnWithSpecificType(
ClassMethod | Function_ | Closure $functionLike,
Type $requireType
): bool {
if ($functionLike->returnType === null) {
return false;
}
Expand All @@ -115,11 +113,11 @@ private function isTypeMatchOrSubType(Node $typeNode, Type $requireType): bool
return is_a($returnType, $requireType::class, true);
}

/**
* @param ClassMethod|Function_ $functionLike
*/
private function moveParamTypeToParamDoc(FunctionLike $functionLike, Param $param, Type $type): void
{
private function moveParamTypeToParamDoc(
ClassMethod | Function_ | Closure $functionLike,
Param $param,
Type $type
): void {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$paramName = $this->nodeNameResolver->getName($param);
$this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ private function getDifferentParamTypeFromReflectionMethod(
return null;
}

/**
* @param ClassMethod|Function_ $functionLike
*/
private function refactorParam(Param $param, FunctionLike $functionLike): void
private function refactorParam(Param $param, ClassMethod | Function_ $functionLike): void
{
if (! $this->isNullableParam($param, $functionLike)) {
return;
Expand All @@ -240,10 +237,7 @@ private function refactorParam(Param $param, FunctionLike $functionLike): void
$param->type = null;
}

/**
* @param ClassMethod|Function_ $functionLike
*/
private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void
private function decorateWithDocBlock(ClassMethod | Function_ $functionLike, Param $param): void
{
if ($param->type === null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ private function processUses(Closure $node): void
$this->cleanTrailingComma($node, $node->uses);
}

/**
* @param ClassMethod|Function_|Closure $node
*/
private function processParams(Node $node): ?Node
private function processParams(ClassMethod | Function_ | Closure $node): ?Node
{
if ($node->params === []) {
return null;
Expand Down
20 changes: 6 additions & 14 deletions rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
Expand Down Expand Up @@ -47,13 +46,10 @@ public function __construct(
) {
}

/**
* @param ClassMethod|Function_|Closure $functionLike
*/
public function shouldSkipVariable(
string $currentName,
string $expectedName,
FunctionLike $functionLike,
ClassMethod | Function_ | Closure $functionLike,
Variable $variable
): bool {
// is the suffix? → also accepted
Expand Down Expand Up @@ -152,19 +148,15 @@ private function isVariableAlreadyDefined(Variable $variable, string $currentVar
return $trinaryLogic->maybe();
}

/**
* @param ClassMethod|Function_|Closure $functionLike
*/
private function skipOnConflictOtherVariable(FunctionLike $functionLike, string $newName): bool
private function skipOnConflictOtherVariable(ClassMethod | Function_ | Closure $functionLike, string $newName): bool
{
return $this->betterNodeFinder->hasInstanceOfName((array) $functionLike->stmts, Variable::class, $newName);
}

/**
* @param ClassMethod|Function_|Closure $functionLike
*/
private function isUsedInClosureUsesName(string $expectedName, FunctionLike $functionLike): bool
{
private function isUsedInClosureUsesName(
string $expectedName,
ClassMethod | Function_ | Closure $functionLike
): bool {
if (! $functionLike instanceof Closure) {
return false;
}
Expand Down
6 changes: 1 addition & 5 deletions rules/Naming/Matcher/ForeachMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Naming\Matcher;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -47,10 +46,7 @@ public function match(Foreach_ $foreach): ?VariableAndCallForeach
return new VariableAndCallForeach($foreach->valueVar, $call, $variableName, $functionLike);
}

/**
* @return ClassMethod|Function_|Closure|null
*/
private function getFunctionLike(Foreach_ $foreach): ?Node
private function getFunctionLike(Foreach_ $foreach): ClassMethod | Function_ | Closure | null
{
return $this->betterNodeFinder->findParentTypes(
$foreach,
Expand Down
6 changes: 1 addition & 5 deletions rules/Naming/Matcher/VariableAndCallAssignMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Naming\Matcher;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -48,10 +47,7 @@ public function match(Assign $assign): ?VariableAndCallAssign
return new VariableAndCallAssign($assign->var, $call, $assign, $variableName, $functionLike);
}

/**
* @return ClassMethod|Function_|Closure|null
*/
private function getFunctionLike(Assign $assign): ?Node
private function getFunctionLike(Assign $assign): ClassMethod | Function_ | Closure | null
{
return $this->betterNodeFinder->findParentTypes(
$assign,
Expand Down
Loading

0 comments on commit 2a394c7

Please sign in to comment.