Skip to content

Commit

Permalink
Static type improvements (#2662)
Browse files Browse the repository at this point in the history
* static fixes

* misc

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 15, 2022
1 parent 8a6b704 commit 84012e4
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 67 deletions.
5 changes: 2 additions & 3 deletions rules/CodingStyle/NodeAnalyzer/SpreadVariablesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
Expand All @@ -17,11 +16,11 @@ final class SpreadVariablesCollector
/**
* @return array<int, ParameterReflection>
*/
public function resolveFromMethodReflection(MethodReflection | FunctionReflection $functionLikeReflection): array
public function resolveFromMethodReflection(MethodReflection $methodReflection): array
{
$spreadParameterReflections = [];

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionLikeReflection->getVariants());
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
foreach ($parametersAcceptor->getParameters() as $key => $parameterReflection) {
if (! $parameterReflection->isVariadic()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Naming\Guard\PropertyConflictingNameGuard;

use PhpParser\Node\Stmt\ClassLike;
use Rector\Naming\Contract\RenameValueObjectInterface;
use Rector\Naming\ExpectedNameResolver\MatchPropertyTypeExpectedNameResolver;
use Rector\Naming\PhpArray\ArrayFilter;
use Rector\Naming\ValueObject\PropertyRename;
Expand All @@ -20,13 +19,10 @@ public function __construct(
) {
}

/**
* @param PropertyRename $renameValueObject
*/
public function isConflicting(RenameValueObjectInterface $renameValueObject): bool
public function isConflicting(PropertyRename $propertyRename): bool
{
$conflictingPropertyNames = $this->resolve($renameValueObject->getClassLike());
return in_array($renameValueObject->getExpectedName(), $conflictingPropertyNames, true);
$conflictingPropertyNames = $this->resolve($propertyRename->getClassLike());
return in_array($propertyRename->getExpectedName(), $conflictingPropertyNames, true);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function createCountedValueName(string $valueName, ?Scope $scope): string
return $valueName;
}

/**
* @api
*/
public function resolveFromFuncCallFirstArgumentWithSuffix(
FuncCall $funcCall,
string $suffix,
Expand Down
6 changes: 3 additions & 3 deletions rules/Naming/RenameGuard/PropertyRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\Naming\RenameGuard;

use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface;
use Rector\Naming\Contract\RenameValueObjectInterface;
use Rector\Naming\ValueObject\PropertyRename;

final class PropertyRenameGuard
{
Expand All @@ -17,10 +17,10 @@ public function __construct(
) {
}

public function shouldSkip(RenameValueObjectInterface $renameValueObject): bool
public function shouldSkip(PropertyRename $propertyRename): bool
{
foreach ($this->conflictingNameGuards as $conflictingNameGuard) {
if ($conflictingNameGuard->isConflicting($renameValueObject)) {
if ($conflictingNameGuard->isConflicting($propertyRename)) {
return true;
}
}
Expand Down
10 changes: 5 additions & 5 deletions rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function hasPhpAttribute(Property | ClassLike | ClassMethod | Param $node
return false;
}

public function hasInheritedPhpAttribute(ClassLike $classLike, string $attributeClass): bool
public function hasInheritedPhpAttribute(Class_ $class, string $attributeClass): bool
{
$className = (string) $this->nodeNameResolver->getName($classLike);
$className = (string) $this->nodeNameResolver->getName($class);
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}
Expand All @@ -49,13 +49,13 @@ public function hasInheritedPhpAttribute(ClassLike $classLike, string $attribute

foreach ($ancestorClassReflections as $ancestorClassReflection) {
$ancestorClassName = $ancestorClassReflection->getName();
$class = $this->astResolver->resolveClassFromName($ancestorClassName);
$resolvedClass = $this->astResolver->resolveClassFromName($ancestorClassName);

if (! $class instanceof Class_) {
if (! $resolvedClass instanceof Class_) {
continue;
}

if ($this->hasPhpAttribute($class, $attributeClass)) {
if ($this->hasPhpAttribute($resolvedClass, $attributeClass)) {
return true;
}
}
Expand Down
30 changes: 15 additions & 15 deletions rules/Php80/NodeManipulator/TokenManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function __construct(
/**
* @param Node[] $nodes
*/
public function refactorArrayToken(array $nodes, Expr $singleTokenExpr): void
public function refactorArrayToken(array $nodes, Variable $singleTokenVariable): void
{
$this->replaceTokenDimFetchZeroWithGetTokenName($nodes, $singleTokenExpr);
$this->replaceTokenDimFetchZeroWithGetTokenName($nodes, $singleTokenVariable);

// replace "$token[1]"; with "$token->value"
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node): ?PropertyFetch {
Expand All @@ -73,17 +73,17 @@ public function refactorArrayToken(array $nodes, Expr $singleTokenExpr): void
/**
* @param Node[] $nodes
*/
public function refactorNonArrayToken(array $nodes, Expr $singleTokenExpr): void
public function refactorNonArrayToken(array $nodes, Variable $singleTokenVariable): void
{
// replace "$content = $token;" → "$content = $token->text;"
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (
$singleTokenExpr
$singleTokenVariable
) {
if (! $node instanceof Assign) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($node->expr, $singleTokenExpr)) {
if (! $this->nodeComparator->areNodesEqual($node->expr, $singleTokenVariable)) {
return null;
}

Expand All @@ -92,12 +92,12 @@ public function refactorNonArrayToken(array $nodes, Expr $singleTokenExpr): void
return null;
}

$node->expr = new PropertyFetch($singleTokenExpr, 'text');
$node->expr = new PropertyFetch($singleTokenVariable, 'text');
});

// replace "$name = null;" → "$name = $token->getTokenName();"
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (
$singleTokenExpr
$singleTokenVariable
): ?Assign {
if (! $node instanceof Assign) {
return null;
Expand All @@ -120,7 +120,7 @@ public function refactorNonArrayToken(array $nodes, Expr $singleTokenExpr): void
return null;
}

$node->expr = new MethodCall($singleTokenExpr, 'getTokenName');
$node->expr = new MethodCall($singleTokenVariable, 'getTokenName');

return $node;
});
Expand All @@ -129,10 +129,10 @@ public function refactorNonArrayToken(array $nodes, Expr $singleTokenExpr): void
/**
* @param Node[] $nodes
*/
public function refactorTokenIsKind(array $nodes, Expr $singleTokenExpr): void
public function refactorTokenIsKind(array $nodes, Variable $singleTokenVariable): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (
$singleTokenExpr
$singleTokenVariable
): ?MethodCall {
if (! $node instanceof Identical) {
return null;
Expand All @@ -150,7 +150,7 @@ public function refactorTokenIsKind(array $nodes, Expr $singleTokenExpr): void
$arrayDimFetch = $arrayDimFetchAndConstFetch->getArrayDimFetch();
$constFetch = $arrayDimFetchAndConstFetch->getConstFetch();

if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $singleTokenExpr)) {
if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $singleTokenVariable)) {
return null;
}

Expand Down Expand Up @@ -212,10 +212,10 @@ public function removeIsArray(array $nodes, Variable $singleTokenVariable): void
*
* @param Node[] $nodes
*/
private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Expr $singleTokenExpr): void
private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Variable $singleTokenVariable): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (
$singleTokenExpr
$singleTokenVariable
): ?MethodCall {
if (! $node instanceof FuncCall) {
return null;
Expand Down Expand Up @@ -249,7 +249,7 @@ private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Expr $si
return null;
}

if (! $this->nodeComparator->areNodesEqual($possibleTokenArray->var, $singleTokenExpr)) {
if (! $this->nodeComparator->areNodesEqual($possibleTokenArray->var, $singleTokenVariable)) {
return null;
}

Expand All @@ -259,7 +259,7 @@ private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Expr $si
$this->assignedNameExpr = $parentNode->var;
}

return new MethodCall($singleTokenExpr, 'getTokenName');
return new MethodCall($singleTokenVariable, 'getTokenName');
});
}

Expand Down
2 changes: 2 additions & 0 deletions rules/Php80/PhpDoc/PhpDocNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
final class PhpDocNodeFinder
{
/**
* @api
*
* @template TNode as Node
* @param class-string<TNode> $nodeType
* @return TNode[]
Expand Down
2 changes: 1 addition & 1 deletion rules/Privatization/Naming/ConstantNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
) {
}

public function createFromProperty(PropertyProperty|Variable $propertyProperty): string
public function createFromProperty(PropertyProperty $propertyProperty): string
{
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($propertyProperty);
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 @@ -22,6 +22,9 @@ public function hasVisibility(Class_ | ClassMethod | Property | ClassConst | Par
return (bool) ($node->flags & $visibility);
}

/**
* @api
*/
public function makeStatic(ClassMethod | Property | ClassConst $node): void
{
$this->addVisibilityFlag($node, Visibility::STATIC);
Expand Down Expand Up @@ -132,6 +135,9 @@ public function removeReadonly(Class_ | Property | Param $node): void
$this->removeVisibilityFlag($node, Visibility::READONLY);
}

/**
* @api
*/
private function addVisibilityFlag(
Class_ | ClassMethod | Property | ClassConst | Param $node,
int $visibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ public function __construct(

public function matchTypeProvidingExpr(
Class_ $class,
ClassMethod | Function_ $functionLike,
ClassMethod $classMethod,
ObjectType $objectType
): MethodCall | PropertyFetch | Variable {
$expr = $this->typeProvidingExprFromClassResolver->resolveTypeProvidingExprFromClass(
$class,
$functionLike,
$classMethod,
$objectType
);

if ($expr !== null) {
if ($expr instanceof Variable) {
$this->addClassMethodParamForVariable($expr, $objectType, $functionLike);
$this->addClassMethodParamForVariable($expr, $objectType, $classMethod);
}

return $expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function __construct(
*/
public function resolveTypeProvidingExprFromClass(
Class_ $class,
ClassMethod | Function_ $functionLike,
ClassMethod $classMethod,
ObjectType $objectType
): ?Expr {
$className = (string) $this->nodeNameResolver->getName($class);
Expand All @@ -66,7 +65,7 @@ public function resolveTypeProvidingExprFromClass(
}

// C. param in constructor?
return $this->resolveConstructorParamProvidingType($functionLike, $objectType);
return $this->resolveConstructorParamProvidingType($classMethod, $objectType);
}

private function resolveMethodCallProvidingType(
Expand Down Expand Up @@ -113,14 +112,10 @@ private function resolvePropertyFetchProvidingType(
}

private function resolveConstructorParamProvidingType(
ClassMethod|Function_ $functionLike,
ClassMethod $classMethod,
ObjectType $objectType
): ?Variable {
if (! $functionLike instanceof ClassMethod) {
return null;
}

if (! $this->nodeNameResolver->isName($functionLike, MethodName::CONSTRUCT)) {
if (! $this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function convertConstantArrayTypeToArrayType(ConstantArrayType $constantA
}

/**
* @api
*
* Turn nested array union types to unique ones:
* e.g. int[]|string[][]|bool[][]|string[][]
* ↓
Expand Down
3 changes: 3 additions & 0 deletions src/Application/FileSystem/RemovedAndAddedFilesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function isFileRemoved(SmartFileInfo $smartFileInfo): bool
return false;
}

/**
* @api
*/
public function addAddedFile(AddedFileInterface $addedFile): void
{
$this->addedFiles[] = $addedFile;
Expand Down
27 changes: 8 additions & 19 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,19 @@ function (Node $node) use ($classLike, $propertyName): bool {
);
}

public function isPropertyToSelf(PropertyFetch | StaticPropertyFetch $expr): bool
public function isPropertyToSelf(PropertyFetch $propertyFetch): bool
{
if ($expr instanceof PropertyFetch && ! $this->nodeNameResolver->isName($expr->var, self::THIS)) {
if (! $this->nodeNameResolver->isName($propertyFetch->var, self::THIS)) {
return false;
}

if ($expr instanceof StaticPropertyFetch && ! $this->nodeNameResolver->isName(
$expr->class,
ObjectReference::SELF
)) {
return false;
}

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

foreach ($class->getProperties() as $property) {
if (! $this->nodeNameResolver->areNamesEqual($property->props[0], $expr)) {
if (! $this->nodeNameResolver->areNamesEqual($property->props[0], $propertyFetch)) {
continue;
}

Expand All @@ -171,21 +164,17 @@ public function isPropertyFetch(Node $node): bool
* Matches:
* "$this->someValue = $<variableName>;"
*/
public function isVariableAssignToThisPropertyFetch(Node $node, string $variableName): bool
public function isVariableAssignToThisPropertyFetch(Assign $assign, string $variableName): bool
{
if (! $node instanceof Assign) {
return false;
}

if (! $node->expr instanceof Variable) {
if (! $assign->expr instanceof Variable) {
return false;
}

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

return $this->isLocalPropertyFetch($node->var);
return $this->isLocalPropertyFetch($assign->var);
}

public function isFilledViaMethodCallInConstructStmts(ClassLike $classLike, string $propertyName): bool
Expand Down

0 comments on commit 84012e4

Please sign in to comment.