Skip to content

Commit

Permalink
[PHPStan] Reduce PHPStan errors for narrow public - take 2 (#2673)
Browse files Browse the repository at this point in the history
* [PHPStan] Reduce PHPStan errors for narrow public - take 2

* back comment

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 17, 2022
1 parent ac94ddd commit ce32685
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 59 deletions.
6 changes: 6 additions & 0 deletions packages/BetterPhpDocParser/Comment/CommentsMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function keepComments(Node $newNode, array $mergedNodes): void
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, null);
}

/**
* @api
*/
public function keepParent(Node $newNode, Node $oldNode): void
{
$parent = $oldNode->getAttribute(AttributeKey::PARENT_NODE);
Expand All @@ -55,6 +58,9 @@ public function keepParent(Node $newNode, Node $oldNode): void
$newNode->setAttribute(AttributeKey::COMMENTS, $comments);
}

/**
* @api
*/
public function keepChildren(Node $newNode, Node $oldNode): void
{
$childrenComments = $this->collectChildrenComments($oldNode);
Expand Down
3 changes: 3 additions & 0 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function createFromNode(Node $node): ?PhpDocInfo
return $phpDocInfo;
}

/**
* @api
*/
public function createEmpty(Node $node): PhpDocInfo
{
/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
Expand Down
6 changes: 3 additions & 3 deletions packages/Caching/FileSystem/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\Caching\FileSystem;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Dependency\DependencyResolver as PHPStanDependencyResolver;
Expand All @@ -22,14 +22,14 @@ public function __construct(
/**
* @return string[]
*/
public function resolveDependencies(Node $node, MutatingScope $mutatingScope): array
public function resolveDependencies(Stmt $stmt, MutatingScope $mutatingScope): array
{
$analysedFileAbsolutesPaths = $this->privatesAccessor->getPrivateProperty(
$this->nodeScopeResolver,
'analysedFiles'
);

$nodeDependencies = $this->phpStanDependencyResolver->resolveDependencies($node, $mutatingScope);
$nodeDependencies = $this->phpStanDependencyResolver->resolveDependencies($stmt, $mutatingScope);
return $nodeDependencies->getFileDependencies($mutatingScope->getFile(), $analysedFileAbsolutesPaths);
}
}
1 change: 1 addition & 0 deletions packages/Comments/CommentRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
}

/**
* @param Node[]|Node|null $node
* @return Node[]|null
*/
public function removeFromNode(array | Node | null $node): array | null
Expand Down
3 changes: 3 additions & 0 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function isName(Node | array $node, string $name): bool
return false;
}

/**
* @api
*/
public function isCaseSensitiveName(Node $node, string $name): bool
{
if ($name === '') {
Expand Down
6 changes: 6 additions & 0 deletions packages/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ public function isInLoop(Node $node): bool
return false;
}

/**
* @api
*/
public function isInSwitch(Node $node): bool
{
return (bool) $this->betterNodeFinder->findParentType($node, Switch_::class);
}

/**
* @api
*/
public function isInIf(Node $node): bool
{
$breakNodes = array_merge([If_::class], self::BREAK_NODES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
}

/**
* @api
* Find node based on $callable or null, when the nesting scope is broken
* @param callable(Node $node): bool $callable
* @param array<class-string<Node>> $allowedTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ArrayType;
Expand Down Expand Up @@ -33,17 +34,17 @@ public function __construct(
* Special case for "preg_match(), preg_match_all()" - with 3rd argument
* @see https://github.com/rectorphp/rector/issues/786
*/
public function correct(Node $node, Type $originalType): Type
public function correct(Expr $expr, Type $originalType): Type
{
if (! $node instanceof Variable) {
if (! $expr instanceof Variable) {
return $originalType;
}

if ($originalType instanceof ArrayType) {
return $originalType;
}

$variableUsages = $this->getVariableUsages($node);
$variableUsages = $this->getVariableUsages($expr);
foreach ($variableUsages as $variableUsage) {
$possiblyArg = $variableUsage->getAttribute(AttributeKey::PARENT_NODE);
if (! $possiblyArg instanceof Arg) {
Expand All @@ -68,7 +69,7 @@ public function correct(Node $node, Type $originalType): Type
$thirdArg = $funcCallNode->args[2];

// are the same variables
if (! $this->nodeComparator->areNodesEqual($thirdArg->value, $node)) {
if (! $this->nodeComparator->areNodesEqual($thirdArg->value, $expr)) {
continue;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\ClassAutoloadingException;
Expand Down Expand Up @@ -230,9 +231,9 @@ public function getNativeType(Expr $expr): Type
return $this->accessoryNonEmptyStringTypeCorrector->correct($type);
}

public function isNumberType(Node $node): bool
public function isNumberType(Expr $expr): bool
{
$nodeType = $this->getType($node);
$nodeType = $this->getType($expr);
if ($nodeType instanceof IntegerType) {
return true;
}
Expand All @@ -241,6 +242,7 @@ public function isNumberType(Node $node): bool
}

/**
* @api
* @param class-string<Type> $desiredType
*/
public function isNullableTypeOfSpecificType(Node $node, string $desiredType): bool
Expand Down
34 changes: 17 additions & 17 deletions packages/NodeTypeResolver/TypeAnalyzer/ArrayTypeAnalyzer.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 PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
Expand Down Expand Up @@ -39,22 +39,22 @@ public function __construct(
) {
}

public function isArrayType(Node $node): bool
public function isArrayType(Expr $expr): bool
{
$nodeType = $this->nodeTypeResolver->getType($node);
$nodeType = $this->nodeTypeResolver->getType($expr);

$nodeType = $this->pregMatchTypeCorrector->correct($node, $nodeType);
$nodeType = $this->pregMatchTypeCorrector->correct($expr, $nodeType);
if ($this->isIntersectionArrayType($nodeType)) {
return true;
}

// PHPStan false positive, when variable has type[] docblock, but default array is missing
if (($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch)) {
if ($this->isPropertyFetchWithArrayDefault($node)) {
if (($expr instanceof PropertyFetch || $expr instanceof StaticPropertyFetch)) {
if ($this->isPropertyFetchWithArrayDefault($expr)) {
return true;
}

if ($this->isPropertyFetchWithArrayDocblockWithoutDefault($node)) {
if ($this->isPropertyFetchWithArrayDocblockWithoutDefault($expr)) {
return false;
}
}
Expand All @@ -64,7 +64,7 @@ public function isArrayType(Node $node): bool
return false;
}

if ($this->isPropertyFetchWithArrayDefault($node)) {
if ($this->isPropertyFetchWithArrayDefault($expr)) {
return true;
}
}
Expand Down Expand Up @@ -97,18 +97,18 @@ private function isIntersectionArrayType(Type $nodeType): bool
return true;
}

private function isPropertyFetchWithArrayDocblockWithoutDefault(Node $node): bool
private function isPropertyFetchWithArrayDocblockWithoutDefault(Expr $expr): bool
{
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
return false;
}

$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);
$classLike = $this->betterNodeFinder->findParentType($expr, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return false;
}

$propertyName = $this->nodeNameResolver->getName($node->name);
$propertyName = $this->nodeNameResolver->getName($expr->name);
if ($propertyName === null) {
return false;
}
Expand All @@ -135,18 +135,18 @@ private function isPropertyFetchWithArrayDocblockWithoutDefault(Node $node): boo
/**
* phpstan bug workaround - https://phpstan.org/r/0443f283-244c-42b8-8373-85e7deb3504c
*/
private function isPropertyFetchWithArrayDefault(Node $node): bool
private function isPropertyFetchWithArrayDefault(Expr $expr): bool
{
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
return false;
}

$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);
$classLike = $this->betterNodeFinder->findParentType($expr, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return false;
}

$propertyName = $this->nodeNameResolver->getName($node->name);
$propertyName = $this->nodeNameResolver->getName($expr->name);
if ($propertyName === null) {
return false;
}
Expand All @@ -159,7 +159,7 @@ private function isPropertyFetchWithArrayDefault(Node $node): bool
}

// B. another object property
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($node);
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($expr);
if ($phpPropertyReflection instanceof PhpPropertyReflection) {
$reflectionProperty = $phpPropertyReflection->getNativeReflection();
return is_array($reflectionProperty->getDefaultValue());
Expand Down
10 changes: 5 additions & 5 deletions packages/NodeTypeResolver/TypeAnalyzer/CountableTypeAnalyzer.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\ObjectType;
use Rector\NodeTypeResolver\NodeTypeCorrector\PregMatchTypeCorrector;
use Rector\NodeTypeResolver\NodeTypeResolver;
Expand All @@ -28,17 +28,17 @@ public function __construct(
];
}

public function isCountableType(Node $node): bool
public function isCountableType(Expr $expr): bool
{
$nodeType = $this->nodeTypeResolver->getType($node);
$nodeType = $this->pregMatchTypeCorrector->correct($node, $nodeType);
$nodeType = $this->nodeTypeResolver->getType($expr);
$nodeType = $this->pregMatchTypeCorrector->correct($expr, $nodeType);

foreach ($this->countableObjectTypes as $countableObjectType) {
if ($countableObjectType->isSuperTypeOf($nodeType)->yes()) {
return true;
}
}

return $this->arrayTypeAnalyzer->isArrayType($node);
return $this->arrayTypeAnalyzer->isArrayType($expr);
}
}
5 changes: 3 additions & 2 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\PostRector\Collector;

use PhpParser\Node;
use PhpParser\Node\Name;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\Naming\Naming\UseImportsResolver;
Expand Down Expand Up @@ -77,9 +78,9 @@ public function getUseImportTypesByNode(File $file, Node $node): array
return $objectTypes;
}

public function hasImport(File $file, Node $node, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
public function hasImport(File $file, Name $name, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
{
$useImports = $this->getUseImportTypesByNode($file, $node);
$useImports = $this->getUseImportTypesByNode($file, $name);

foreach ($useImports as $useImport) {
if ($useImport->equals($fullyQualifiedObjectType)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/VendorLocker/VendorLockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Rector\VendorLocker;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodParamVendorLockResolver;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnVendorLockResolver;
Expand All @@ -20,7 +20,7 @@ public function __construct(
) {
}

public function isClassMethodParamLockedIn(Node $node): bool
public function isClassMethodParamLockedIn(ClassMethod|Function_ $node): bool
{
if (! $node instanceof ClassMethod) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function __construct(
) {
}

/**
* @api
*/
public function isVariadic(FunctionLike $functionLike): bool
{
$isVariadic = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Rector\DeadCode\TypeNodeAnalyzer;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;

final class MixedArrayTypeNodeAnalyzer
{
public function hasMixedArrayType(UnionTypeNode $unionTypeNode): bool
public function hasMixedArrayType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
{
$types = $unionTypeNode->types;
$types = $bracketsAwareUnionTypeNode->types;

foreach ($types as $type) {
if ($type instanceof SpacingAwareArrayTypeNode) {
Expand Down
11 changes: 3 additions & 8 deletions rules/DogFood/NodeAnalyzer/ContainerConfiguratorCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\DogFood\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
Expand Down Expand Up @@ -39,16 +38,12 @@ public function isMethodCallWithServicesSetRectorRule(MethodCall $methodCall): b
return is_a($serviceClass, RectorInterface::class, true);
}

public function isMethodCallNamed(Expr $expr, string $variableName, string $methodName): bool
public function isMethodCallNamed(MethodCall $methodCall, string $variableName, string $methodName): bool
{
if (! $expr instanceof MethodCall) {
if (! $this->nodeNameResolver->isName($methodCall->var, $variableName)) {
return false;
}

if (! $this->nodeNameResolver->isName($expr->var, $variableName)) {
return false;
}

return $this->nodeNameResolver->isName($expr->name, $methodName);
return $this->nodeNameResolver->isName($methodCall->name, $methodName);
}
}
Loading

0 comments on commit ce32685

Please sign in to comment.