Skip to content

Commit

Permalink
Utilize Type->isArray() (#3065)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <m.staab@complex-it.de>
  • Loading branch information
staabm and clxmstaab committed Nov 15, 2022
1 parent e78e472 commit 04257d3
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private function shouldSkip(Foreach_ $foreach, Scope $scope): bool
}

$type = $scope->getType($foreach->expr);
return ! $type instanceof ArrayType;
return ! $type->isArray()->yes();
}

private function shouldSkipAsPartOfOtherLoop(Foreach_ $foreach): bool
Expand Down
2 changes: 1 addition & 1 deletion rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp
private function resolveCount(bool $isNegated, FuncCall $funcCall): Identical | Greater | null
{
$countedType = $this->getType($funcCall->args[0]->value);
if ($countedType instanceof ArrayType) {
if ($countedType->isArray()->yes()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function refactor(Node $node): ?Node
}

$emptyExprType = $this->getType($negagedExpr->expr);
if (! $emptyExprType instanceof ArrayType) {
if (! $emptyExprType->isArray()->yes()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private function processMarkTruthyNegation(BooleanNot $booleanNot): ?Identical

private function isArray(Expr $expr): bool
{
return $this->getType($expr) instanceof ArrayType;
return $this->getType($expr)->isArray()->yes();
}

private function processIdenticalOrNotIdentical(Node $node, FuncCall $funcCall, Expr $expr): ?Expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function isNullableNonScalarType(Expr $expr): bool

// is array?
foreach ($staticType->getTypes() as $subType) {
if ($subType instanceof ArrayType) {
if ($subType->isArray()->yes()) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/TypeAnalyzer/IterableTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class IterableTypeAnalyzer
{
public function detect(Type $type): bool
{
if ($type instanceof ArrayType) {
if ($type->isArray()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
if (($if->cond instanceof Variable || $this->propertyFetchAnalyzer->isPropertyFetch($if->cond))
&& $this->nodeComparator->areNodesEqual($if->cond, $foreachExpr)
) {
return $scope->getType($if->cond) instanceof ArrayType;
return $scope->getType($if->cond)->isArray()->yes();
}

if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotIdenticalEmptyArray($if, $foreachExpr)) {
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/UselessIfCondBeforeForeachDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isMatchingNotEmpty(If_ $if, Expr $foreachExpr, Scope $scope): bo

// is array though?
$arrayType = $scope->getType($empty->expr);
if (! $arrayType instanceof ArrayType) {
if (! $arrayType->isArray()->yes()) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions rules/Php80/NodeManipulator/TokenManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function refactorArrayToken(array $nodes, Variable $singleTokenVariable):
}

$tokenStaticType = $this->nodeTypeResolver->getType($node->var);
if (! $tokenStaticType instanceof ArrayType) {
if (! $tokenStaticType->isArray()->yes()) {
return null;
}

Expand All @@ -88,7 +88,7 @@ public function refactorNonArrayToken(array $nodes, Variable $singleTokenVariabl
}

$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
if ($tokenStaticType instanceof ArrayType) {
if ($tokenStaticType->isArray()->yes()) {
return null;
}

Expand All @@ -104,7 +104,7 @@ public function refactorNonArrayToken(array $nodes, Variable $singleTokenVariabl
}

$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
if ($tokenStaticType instanceof ArrayType) {
if ($tokenStaticType->isArray()->yes()) {
return null;
}

Expand Down Expand Up @@ -237,7 +237,7 @@ private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Variable
}

$tokenStaticType = $this->nodeTypeResolver->getType($possibleTokenArray->var);
if (! $tokenStaticType instanceof ArrayType) {
if (! $tokenStaticType->isArray()->yes()) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Php80/Rector/FunctionLike/UnionTypesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private function filterOutDuplicatedArrayTypes(UnionType $unionType): UnionType
$originalTypeCount = count($unionType->getTypes());

foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ArrayType) {
if ($unionedType->isArray()->yes()) {
if ($hasArrayType) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions rules/Strict/NodeFactory/ExactCompareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function createIdenticalFalsyCompare(
return new Identical($expr, $this->nodeFactory->createFalse());
}

if ($exprType instanceof ArrayType) {
if ($exprType->isArray()->yes()) {
return new Identical($expr, new Array_([]));
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public function createNotIdenticalFalsyCompare(
return new NotIdentical($expr, new LNumber(0));
}

if ($exprType instanceof ArrayType) {
if ($exprType->isArray()->yes()) {
return new NotIdentical($expr, new Array_([]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function decoratePropertyUnionType(
private function isDocBlockRequired(UnionType $unionType): bool
{
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ArrayType) {
if ($unionedType->isArray()->yes()) {
$describedArray = $unionedType->describe(VerbosityLevel::value());
if ($describedArray !== 'array') {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function refactor(Node $node): ?Node

$returnType = $this->nodeTypeResolver->getType($onlyReturn->expr);

if (! $returnType instanceof ArrayType) {
if (! $returnType->isArray()->yes()) {
return null;
}

Expand Down Expand Up @@ -221,7 +221,7 @@ private function shouldAddReturnArrayDocType(Type $exprType): bool
return ! $exprType->getItemType() instanceof NeverType;
}

return $exprType instanceof ArrayType;
return $exprType->isArray()->yes();
}

private function narrowConstantArrayType(Type $type): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function isStaticTypeIterable(Type $type): bool

private function isArrayIterableOrIteratorType(Type $type): bool
{
if ($type instanceof ArrayType) {
if ($type->isArray()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function isIterableType(Type $type): bool
return true;
}

if ($type instanceof ArrayType) {
if ($type->isArray()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function inferProperty(Property $property): ?Type
$classMethod
);
// let PhpDoc solve that later for more precise type
if ($returnType instanceof ArrayType) {
if ($returnType->isArray()->yes()) {
return new MixedType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function resolveFromParamType(Param $param, ClassMethod $classMethod, st
$types = [];

// it's an array - annotation → make type more precise, if possible
if ($type instanceof ArrayType || $param->variadic) {
if ($type->isArray()->yes() || $param->variadic) {
$types[] = $this->getResolveParamStaticTypeAsPHPStanType($classMethod, $propertyName);
} else {
$types[] = $type;
Expand Down
2 changes: 1 addition & 1 deletion rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private function createArrayTypeFromNonConstantValueTypes(array $nonConstantValu
private function collectNestedArrayTypeFromUnionType(UnionType $unionType, int $arrayNesting): void
{
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ArrayType) {
if ($unionedType->isArray()->yes()) {
++$arrayNesting;
$this->normalizeArrayOfUnionToUnionArray($unionedType, $arrayNesting);
} else {
Expand Down

0 comments on commit 04257d3

Please sign in to comment.