Skip to content

Commit

Permalink
[DX] Add strict PHPStan rules - step #3 (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 28, 2021
1 parent fa9b4ef commit 6340d26
Show file tree
Hide file tree
Showing 29 changed files with 83 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public function autowireUnionTypeNodeMapper(PhpParserNodeMapper $phpParserNodeMa
$this->phpParserNodeMapper = $phpParserNodeMapper;
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Node\IntersectionType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function autowireNullableTypeNodeMapper(PhpParserNodeMapper $phpParserNod
$this->phpParserNodeMapper = $phpParserNodeMapper;
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return NullableType::class;
Expand Down
3 changes: 0 additions & 3 deletions packages/StaticTypeMapper/PhpParser/UnionTypeNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public function autowireUnionTypeNodeMapper(PhpParserNodeMapper $phpParserNodeMa
$this->phpParserNodeMapper = $phpParserNodeMapper;
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return UnionType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function isParentClassLocked(ClassReflection $classReflection, string $p
continue;
}

if ($parentClassReflection->getfileName() === $fileName) {
if ($parentClassReflection->getFileName() === $fileName) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function processArgs(
} elseif (is_array($replaceArgumentDefaultValue->getValueBefore())) {
$newArgs = $this->processArrayReplacement($expr->args, $replaceArgumentDefaultValue);

if ($newArgs) {
if (is_array($newArgs)) {
$expr->args = $newArgs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function matchReturnOrAssignNode(Foreach_ $foreach): ?Node
});
}

private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $return): ?Node
private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $return): Return_|null
{
if (! $this->nodeComparator->areNodesEqual($foreach->valueVar, $return->expr)) {
return null;
Expand Down Expand Up @@ -160,7 +160,7 @@ private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $
$coalesce = new Coalesce(new ArrayDimFetch(
$foreach->expr,
$checkedNode
), $this->return && $this->return->expr !== null ? $this->return->expr : $checkedNode);
), $this->return instanceof Return_ && $this->return->expr !== null ? $this->return->expr : $checkedNode);

if ($this->return !== null) {
return new Return_($coalesce);
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/Naming/ClassNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getNamespace(string $fullyQualifiedName): ?string
{
$fullyQualifiedName = trim($fullyQualifiedName, '\\');

return Strings::before($fullyQualifiedName, '\\', -1) ?: null;
return Strings::before($fullyQualifiedName, '\\', -1);
}

public function getNameFromFileInfo(SmartFileInfo $smartFileInfo): string
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/NodeFactory/JsonArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function replaceNodeObjectHashPlaceholdersWithNodes(Array_ $array, array

$placeholderNode = $this->matchPlaceholderNode($onlyItem->value, $placeholderNodes);

if ($placeholderNode && $this->implodeAnalyzer->isImplodeToJson($placeholderNode)) {
if ($placeholderNode instanceof Expr && $this->implodeAnalyzer->isImplodeToJson($placeholderNode)) {
/**
* @var FuncCall $placeholderNode
* @var Arg $firstArg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node
}

$oldVariableName = $this->getName($node->var);
if (! $oldVariableName) {
if (! is_string($oldVariableName)) {
return null;
}

Expand Down
23 changes: 4 additions & 19 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;

final class DeadParamTagValueNodeAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private TypeComparator $typeComparator
private TypeComparator $typeComparator,
private GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer,
) {
}

Expand Down Expand Up @@ -65,7 +67,7 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return $this->isEmptyDescription($paramTagValueNode, $param->type);
}

if (! $this->hasGenericType($paramTagValueNode->type)) {
if (! $this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type)) {
return $this->isEmptyDescription($paramTagValueNode, $param->type);
}

Expand Down Expand Up @@ -136,23 +138,6 @@ private function isUnionIdentifier(PhpDocTagNode $phpDocTagNode): bool
return true;
}

private function hasGenericType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
{
$types = $bracketsAwareUnionTypeNode->types;

foreach ($types as $type) {
if ($type instanceof GenericTypeNode) {
if ($type->type instanceof IdentifierTypeNode && $type->type->name === 'array') {
continue;
}

return true;
}
}

return false;
}

private function matchParamByName(string $desiredParamName, FunctionLike $functionLike): ?Param
{
foreach ($functionLike->getParams() as $param) {
Expand Down
22 changes: 3 additions & 19 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
use PhpParser\Node\Stmt\Trait_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;

final class DeadReturnTagValueNodeAnalyzer
{
public function __construct(
private TypeComparator $typeComparator,
private BetterNodeFinder $betterNodeFinder,
private GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer,
) {
}

Expand Down Expand Up @@ -55,27 +56,10 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, FunctionLike $fun
return $returnTagValueNode->description === '';
}

if (! $this->hasGenericType($returnTagValueNode->type)) {
if (! $this->genericTypeNodeAnalyzer->hasGenericType($returnTagValueNode->type)) {
return $returnTagValueNode->description === '';
}

return false;
}

private function hasGenericType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
{
$types = $bracketsAwareUnionTypeNode->types;

foreach ($types as $type) {
if ($type instanceof GenericTypeNode) {
if ($type->type instanceof IdentifierTypeNode && $type->type->name === 'array') {
continue;
}

return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ private function shouldSkip(Assign $assign): bool
return true;
}

return $variable->name instanceof Variable && $this->betterNodeFinder->findFirstNext(
if (! $variable->name instanceof Variable) {
return false;
}

return (bool) $this->betterNodeFinder->findFirstNext(
$assign,
fn (Node $node): bool => $node instanceof Variable
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ private function shouldSkipWithAnnotationsOrAttributes(ClassMethod $classMethod)

private function matchClassMethodOnlyStmt(ClassMethod $classMethod): null | Stmt | Expr
{
if ($classMethod->stmts === null) {
$classMethodStmts = $classMethod->stmts;
if ($classMethodStmts === null) {
return null;
}

if (count((array) $classMethod->stmts) !== 1) {
if (count($classMethodStmts) !== 1) {
return null;
}

// recount empty notes
$stmtsValues = array_values($classMethod->stmts);
$stmtsValues = array_values($classMethodStmts);

return $this->unwrapExpression($stmtsValues[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function getNodeNames(array $nodes): array
$nodeNames = [];
foreach ($nodes as $node) {
$nodeName = $this->getName($node);
if ($nodeName) {
if (is_string($nodeName)) {
$nodeNames[] = $nodeName;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?array
{
if ($node->elseifs) {
if ($node->elseifs !== []) {
return null;
}

Expand All @@ -92,7 +92,7 @@ public function refactor(Node $node): ?array
*/
private function refactorIsMatch(If_ $if): ?array
{
if ($if->elseifs) {
if ($if->elseifs !== []) {
return null;
}

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

namespace Rector\DeadCode\Rector\Node;

use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignRef;
Expand Down Expand Up @@ -121,7 +120,7 @@ public function refactor(Node $node): ?Node
}

$comments = $node->getComments();
if (isset($comments[1]) && $comments[1] instanceof Comment) {
if (isset($comments[1])) {
$this->commentRemover->rollbackComments($node, $comments[1]);
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function refactor(Node $node): ?Node
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);

// skip typed properties
if ($parent instanceof Property && $parent->type) {
if ($parent instanceof Property && $parent->type !== null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function refactor(Node $node): ?Node
/** @var Case_|null $previousCase */
$previousCase = null;
foreach ($node->cases as $case) {
if ($previousCase && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
if ($previousCase instanceof Case_ && $this->areSwitchStmtsEqualsAndWithBreak($case, $previousCase)) {
$previousCase->stmts = [];
}

Expand Down
28 changes: 28 additions & 0 deletions rules/DeadCode/TypeNodeAnalyzer/GenericTypeNodeAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\TypeNodeAnalyzer;

use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;

final class GenericTypeNodeAnalyzer
{
public function hasGenericType(UnionTypeNode $unionTypeNode): bool
{
$types = $unionTypeNode->types;

foreach ($types as $type) {
if ($type instanceof GenericTypeNode) {
if ($type->type->name === 'array') {
continue;
}

return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,9 @@ private function cleanBitwiseOrFlags(FuncCall $funcCall, BitwiseOr $bitwiseOr, E

private function assignThirdArgsValue(FuncCall $funcCall, BitwiseOr $bitwiseOr): void
{
if ($bitwiseOr instanceof BitwiseOr && $bitwiseOr->right instanceof ConstFetch && $this->isName(
$bitwiseOr->right,
self::FLAG
)) {
if ($bitwiseOr->right instanceof ConstFetch && $this->isName($bitwiseOr->right, self::FLAG)) {
$bitwiseOr = $bitwiseOr->left;
}

if ($bitwiseOr instanceof BitwiseOr && $bitwiseOr->left instanceof ConstFetch && $this->isName(
$bitwiseOr->left,
self::FLAG
)) {
} elseif ($bitwiseOr->left instanceof ConstFetch && $this->isName($bitwiseOr->left, self::FLAG)) {
$bitwiseOr = $bitwiseOr->right;
}

Expand Down
27 changes: 11 additions & 16 deletions rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,8 @@ private function createArrayMerge(Array_ $array, array $items): FuncCall
* as to invoke it only once and avoid potential bugs,
* such as a method executing some side-effect
*/
private function createVariableFromNonVariable(
Array_ $array,
ArrayItem $arrayItem,
int | string $position
): Variable {
private function createVariableFromNonVariable(Array_ $array, ArrayItem $arrayItem, int $position): Variable
{
/** @var Scope $nodeScope */
$nodeScope = $array->getAttribute(AttributeKey::SCOPE);
// The variable name will be item0Unpacked, item1Unpacked, etc,
Expand Down Expand Up @@ -277,18 +274,16 @@ private function createArgFromSpreadArrayItem(Scope $nodeScope, ArrayItem $array

$iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($arrayItem)]);

if ($type !== null) {
// If we know it is an array, then print it directly
// Otherwise PHPStan throws an error:
// "Else branch is unreachable because ternary operator condition is always true."
if ($type instanceof ArrayType) {
return new Arg($arrayItem);
}
// If we know it is an array, then print it directly
// Otherwise PHPStan throws an error:
// "Else branch is unreachable because ternary operator condition is always true."
if ($type instanceof ArrayType) {
return new Arg($arrayItem);
}

// If it is iterable, then directly return `iterator_to_array`
if ($this->isIterableType($type)) {
return new Arg($iteratorToArrayFuncCall);
}
// If it is iterable, then directly return `iterator_to_array`
if ($this->isIterableType($type)) {
return new Arg($iteratorToArrayFuncCall);
}

// Print a ternary, handling either an array or an iterator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function refactor(Node $node): ?Node
return null;
}

$isAlreadyDowngraded = $node->getAttribute(self::ALREADY_DOWNGRADED);
$isAlreadyDowngraded = (bool) $node->getAttribute(self::ALREADY_DOWNGRADED, false);
if ($isAlreadyDowngraded) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion rules/EarlyReturn/NodeFactory/InvertedIfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function createFromConditions(If_ $if, array $conditions, Return_ $return
{
$ifs = [];
$ifNextReturn = $this->getIfNextReturn($if);
$stmt = $this->contextAnalyzer->isInLoop($if) && ! $ifNextReturn

$stmt = $this->contextAnalyzer->isInLoop($if) && ! $ifNextReturn instanceof Return_
? [new Continue_()]
: [$return];

Expand Down
Loading

0 comments on commit 6340d26

Please sign in to comment.