Skip to content

Commit

Permalink
[PHPStan] Reduce PHPStan errors for narrow public - take 1 (#2672)
Browse files Browse the repository at this point in the history
* [PHPStan] Reduce PHPStan errors for narrow public - take 1
  • Loading branch information
samsonasik committed Jul 16, 2022
1 parent 97ef9ff commit ac94ddd
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Naming\Contract\RenameValueObjectInterface;
use Rector\Naming\ValueObject\ParamRename;

final class PropertyDocBlockManipulator
Expand All @@ -17,20 +16,17 @@ public function __construct(
) {
}

/**
* @param ParamRename $renameValueObject
*/
public function renameParameterNameInDocBlock(RenameValueObjectInterface $renameValueObject): void
public function renameParameterNameInDocBlock(ParamRename $paramRename): void
{
$functionLike = $renameValueObject->getFunctionLike();
$functionLike = $paramRename->getFunctionLike();

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$paramTagValueNode = $phpDocInfo->getParamTagValueNodeByName($renameValueObject->getCurrentName());
$paramTagValueNode = $phpDocInfo->getParamTagValueNodeByName($paramRename->getCurrentName());
if (! $paramTagValueNode instanceof ParamTagValueNode) {
return;
}

$paramTagValueNode->parameterName = '$' . $renameValueObject->getExpectedName();
$paramTagValueNode->parameterName = '$' . $paramRename->getExpectedName();
$paramTagValueNode->setAttribute(PhpDocAttributeKey::ORIG_NODE, null);
}
}
7 changes: 2 additions & 5 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ public function getChildrenOfClassReflection(ClassReflection $desiredClassReflec
public function getPossibleUnionPropertyType(
Property $property,
Type $varType,
?Scope $scope,
Scope $scope,
Name | ComplexType | null $propertyTypeNode
): PropertyType {
if ($varType instanceof UnionType) {
return new PropertyType($varType, $propertyTypeNode);
}

if (! $scope instanceof Scope) {
return new PropertyType($varType, $propertyTypeNode);
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
throw new ShouldNotHappenException();
Expand Down Expand Up @@ -116,6 +112,7 @@ public function getPossibleUnionPropertyType(
}

/**
* @api
* @return string[]
*/
public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrName): array
Expand Down
3 changes: 3 additions & 0 deletions packages/NodeRemoval/BreakingRemovalGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function ensureNodeCanBeRemove(Node $node): void
));
}

/**
* @api
*/
public function isLegalNodeRemoval(Node $node): bool
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
Expand Down
6 changes: 3 additions & 3 deletions packages/NodeTypeResolver/TypeAnalyzer/StringTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\NodeTypeResolver\TypeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand All @@ -16,9 +16,9 @@ public function __construct(
) {
}

public function isStringOrUnionStringOnlyType(Node $node): bool
public function isStringOrUnionStringOnlyType(Expr $expr): bool
{
$nodeType = $this->nodeTypeResolver->getType($node);
$nodeType = $this->nodeTypeResolver->getType($expr);
if ($nodeType instanceof StringType) {
return true;
}
Expand Down
6 changes: 1 addition & 5 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,8 @@ public function isSubtype(Type $checkedType, Type $mainType): bool
return $this->arrayTypeComparator->isSubtype($checkedType, $mainType);
}

public function areTypesPossiblyIncluded(?Type $assumptionType, ?Type $exactType): bool
public function areTypesPossiblyIncluded(Type $assumptionType, ?Type $exactType): bool
{
if (! $assumptionType instanceof Type) {
return true;
}

if (! $exactType instanceof Type) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/PostRector/Collector/NodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function addNodeBeforeNode(Node $addedNode, Node $positionNode): void
}

/**
* @api
* @param Node[] $addedNodes
* @deprecated Return created nodes right in refactor() method to keep context instead.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Collector/NodesToRemoveCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getCount(): int
}

/**
* @return Node[]
* @return array<int, Node>
*/
public function getNodesToRemove(): array
{
Expand Down
6 changes: 3 additions & 3 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class UseNodesToAddCollector implements NodeCollectorInterface
private array $functionUseImportTypesInFilePath = [];

/**
* @var array<string, FullyQualifiedObjectType[]|AliasedObjectType[]>
* @var array<string, FullyQualifiedObjectType[]>
*/
private array $useImportTypesInFilePath = [];

Expand All @@ -36,12 +36,12 @@ public function isActive(): bool
return $this->useImportTypesInFilePath !== [] || $this->functionUseImportTypesInFilePath !== [];
}

public function addUseImport(FullyQualifiedObjectType | AliasedObjectType $objectType): void
public function addUseImport(FullyQualifiedObjectType $fullyQualifiedObjectType): void
{
/** @var File $file */
$file = $this->currentFileProvider->getFile();

$this->useImportTypesInFilePath[$file->getFilePath()][] = $objectType;
$this->useImportTypesInFilePath[$file->getFilePath()][] = $fullyQualifiedObjectType;
}

public function addFunctionUseImport(FullyQualifiedObjectType $fullyQualifiedObjectType): void
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/NodeAnalyzer/CallCollectionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
/**
* @param StaticCall[]|MethodCall[] $calls
*/
public function isExists(array $calls, string $classMethodName, ?string $className): bool
public function isExists(array $calls, string $classMethodName, string $className): bool
{
foreach ($calls as $call) {
$callerRoot = $call instanceof StaticCall ? $call->class : $call->var;
Expand Down
3 changes: 1 addition & 2 deletions rules/DeadCode/NodeManipulator/CountManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\DeadCode\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Greater;
Expand All @@ -24,7 +23,7 @@ public function __construct(
) {
}

public function isCounterHigherThanOne(Node $node, Expr $expr): bool
public function isCounterHigherThanOne(Expr $node, Expr $expr): bool
{
// e.g. count($values) > 0
if ($node instanceof Greater) {
Expand Down
1 change: 1 addition & 0 deletions rules/Naming/Naming/UseImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function resolveForNode(Node $node): array
}

/**
* @api
* @return Use_[]
*/
public function resolveBareUsesForNode(Node $node): array
Expand Down
5 changes: 3 additions & 2 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand All @@ -39,7 +40,7 @@ public function __construct(

public function resolveFromNodeWithScopeCountAndFallbackName(
Expr $expr,
?Scope $scope,
MutatingScope $mutatingScope,
string $fallbackName
): string {
$name = $this->resolveFromNode($expr);
Expand All @@ -51,7 +52,7 @@ public function resolveFromNodeWithScopeCountAndFallbackName(
$name = (string) Strings::after($name, '\\', -1);
}

$countedValueName = $this->createCountedValueName($name, $scope);
$countedValueName = $this->createCountedValueName($name, $mutatingScope);
return lcfirst($countedValueName);
}

Expand Down
4 changes: 3 additions & 1 deletion rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\Naming\PropertyRenamer;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand Down Expand Up @@ -34,7 +36,7 @@ public function __construct(
) {
}

public function renamePropertyPromotion(ClassLike $classLike): void
public function renamePropertyPromotion(Class_|Interface_ $classLike): void
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::PROPERTY_PROMOTION)) {
return;
Expand Down
5 changes: 2 additions & 3 deletions rules/Php80/NodeResolver/RequireOptionalParamResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Php80\NodeResolver;

use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
Expand All @@ -14,9 +13,9 @@ final class RequireOptionalParamResolver
/**
* @return ParameterReflection[]
*/
public function resolveFromReflection(MethodReflection | FunctionReflection $functionLikeReflection): array
public function resolveFromReflection(MethodReflection $methodReflection): array
{
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionLikeReflection->getVariants());
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$optionalParams = [];
$requireParams = [];
Expand Down
6 changes: 6 additions & 0 deletions rules/Privatization/NodeManipulator/VisibilityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function makeAbstract(ClassMethod | Class_ $node): void
$this->addVisibilityFlag($node, Visibility::ABSTRACT);
}

/**
* @api
*/
public function makeNonStatic(ClassMethod | Property $node): void
{
if (! $node->isStatic()) {
Expand Down Expand Up @@ -100,6 +103,9 @@ public function makePublic(ClassMethod | Property | ClassConst $node): void
$this->replaceVisibilityFlag($node, Visibility::PUBLIC);
}

/**
* @api
*/
public function makeProtected(ClassMethod | Property | ClassConst $node): void
{
$this->replaceVisibilityFlag($node, Visibility::PROTECTED);
Expand Down
6 changes: 5 additions & 1 deletion rules/Transform/Rector/Class_/MergeInterfacesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -102,7 +103,10 @@ public function configure(array $configuration): void
private function makeImplementsUnique(Class_ $class): void
{
$alreadyAddedNames = [];
foreach ($class->implements as $key => $name) {
/** @var array<int, Interface_> $implements */
$implements = $class->implements;

foreach ($implements as $key => $name) {
$fqnName = $this->getName($name);
if (in_array($fqnName, $alreadyAddedNames, true)) {
$this->nodeRemover->removeImplements($class, $key);
Expand Down
8 changes: 4 additions & 4 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public function isLocalPropertyFetchName(Node $node, string $desiredPropertyName
return $this->nodeNameResolver->isName($node->name, $desiredPropertyName);
}

public function countLocalPropertyFetchName(ClassLike $classLike, string $propertyName): int
public function countLocalPropertyFetchName(Class_ $class, string $propertyName): int
{
$total = 0;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($classLike->stmts, function (Node $subNode) use (
$classLike,
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $subNode) use (
$class,
$propertyName,
&$total
): ?Node {
Expand All @@ -94,7 +94,7 @@ public function countLocalPropertyFetchName(ClassLike $classLike, string $proper
++$total;
}

if ($parentClassLike === $classLike) {
if ($parentClassLike === $class) {
++$total;
}

Expand Down
7 changes: 4 additions & 3 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PhpParser\Node\Stmt\Unset_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand Down Expand Up @@ -209,11 +210,11 @@ public function isPropertyChangeable(Class_ $class, Property $property): bool
return false;
}

public function resolveExistingClassPropertyNameByType(Class_ $class, Type $type): ?string
public function resolveExistingClassPropertyNameByType(Class_ $class, ObjectType $objectType): ?string
{
foreach ($class->getProperties() as $property) {
$propertyType = $this->nodeTypeResolver->getType($property);
if (! $propertyType->equals($type)) {
if (! $propertyType->equals($objectType)) {
continue;
}

Expand All @@ -223,7 +224,7 @@ public function resolveExistingClassPropertyNameByType(Class_ $class, Type $type
$promotedPropertyParams = $this->promotedPropertyResolver->resolveFromClass($class);
foreach ($promotedPropertyParams as $promotedPropertyParam) {
$paramType = $this->nodeTypeResolver->getType($promotedPropertyParam);
if (! $paramType->equals($type)) {
if (! $paramType->equals($objectType)) {
continue;
}

Expand Down
17 changes: 12 additions & 5 deletions src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
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;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* This service verify if the Node already rectified with same Rector rule before current Rector rule with condition
Expand All @@ -29,16 +29,17 @@ public function __construct(private readonly NodeComparator $nodeComparator)
{
}

public function verify(AbstractRector $rector, Node $node, File $currentFile): ?RectifiedNode
/**
* @param class-string<RectorInterface> $rectorClass
*/
public function verify(string $rectorClass, Node $node, SmartFileInfo $smartFileInfo): ?RectifiedNode
{
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
$rectorClass = $rector::class;

if ($this->hasCreatedByRule($rectorClass, $node, $originalNode)) {
return new RectifiedNode($rectorClass, $node);
}

$smartFileInfo = $currentFile->getSmartFileInfo();
$realPath = $smartFileInfo->getRealPath();

if (! isset($this->previousFileWithNodes[$realPath])) {
Expand All @@ -57,13 +58,19 @@ public function verify(AbstractRector $rector, Node $node, File $currentFile): ?
return $rectifiedNode;
}

/**
* @param class-string<RectorInterface> $rectorClass
*/
private function hasCreatedByRule(string $rectorClass, Node $node, ?Node $originalNode): bool
{
$originalNode ??= $node;
$createdByRule = $originalNode->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];
return in_array($rectorClass, $createdByRule, true);
}

/**
* @param class-string<RectorInterface> $rectorClass
*/
private function shouldContinue(
RectifiedNode $rectifiedNode,
string $rectorClass,
Expand Down
Loading

0 comments on commit ac94ddd

Please sign in to comment.