Skip to content

Commit 6044c89

Browse files
committed
Simplify by replacing chained $var === const with in_array()
1 parent 56ec764 commit 6044c89

7 files changed

+20
-15
lines changed

src/Fixable/PhpPrinterIndentationDetectorVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\NodeVisitor;
99
use PhpParser\NodeVisitorAbstract;
1010
use function count;
11+
use function in_array;
1112
use function is_array;
1213
use function preg_match;
1314
use function preg_match_all;
@@ -47,7 +48,7 @@ public function enterNode(Node $node): ?int
4748
$text = $this->origTokens->getTokenCode($node->getStartTokenPos(), $firstStmt->getStartTokenPos(), 0);
4849

4950
$c = preg_match_all('~\n([\\x09\\x20]*)~', $text, $matches, PREG_SET_ORDER);
50-
if ($c === 0 || $c === false) {
51+
if (in_array($c, [0, false], true)) {
5152
return null;
5253
}
5354

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ public function getDivType(Expr $left, Expr $right, callable $getTypeCallback):
864864

865865
$rightScalarValues = $rightType->toNumber()->getConstantScalarValues();
866866
foreach ($rightScalarValues as $scalarValue) {
867-
if ($scalarValue === 0 || $scalarValue === 0.0) {
867+
if (in_array($scalarValue, [0, 0.0], true)) {
868868
return new ErrorType();
869869
}
870870
}
@@ -938,7 +938,7 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
938938
$rightScalarValues = $rightType->toNumber()->getConstantScalarValues();
939939
foreach ($rightScalarValues as $scalarValue) {
940940

941-
if ($scalarValue === 0 || $scalarValue === 0.0) {
941+
if (in_array($scalarValue, [0, 0.0], true)) {
942942
return new ErrorType();
943943
}
944944
}

src/Rules/Keywords/ContinueBreakInLoopRule.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use function array_reverse;
13+
use function in_array;
1314
use function sprintf;
1415

1516
/**
@@ -52,13 +53,13 @@ public function processNode(Node $node, Scope $scope): array
5253
->build(),
5354
];
5455
}
55-
if (
56-
$parentStmtType === Stmt\For_::class
57-
|| $parentStmtType === Stmt\Foreach_::class
58-
|| $parentStmtType === Stmt\Do_::class
59-
|| $parentStmtType === Stmt\While_::class
60-
|| $parentStmtType === Stmt\Switch_::class
61-
) {
56+
if (in_array($parentStmtType, [
57+
Stmt\For_::class,
58+
Stmt\Foreach_::class,
59+
Stmt\Do_::class,
60+
Stmt\While_::class,
61+
Stmt\Switch_::class,
62+
], true)) {
6263
$value--;
6364
}
6465
if ($value === 0) {

src/Rules/UnusedFunctionParametersCheck.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_combine;
1313
use function array_map;
1414
use function array_merge;
15+
use function in_array;
1516
use function is_array;
1617
use function is_string;
1718
use function sprintf;
@@ -78,7 +79,7 @@ private function getUsedVariables(Scope $scope, $node): array
7879
if ($node instanceof Node) {
7980
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) {
8081
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
81-
if ($functionName === 'func_get_args' || $functionName === 'get_defined_vars') {
82+
if (in_array($functionName, ['func_get_args', 'get_defined_vars'], true)) {
8283
return $scope->getDefinedVariables();
8384
}
8485
}

src/Type/Php/DsMapDynamicMethodThrowTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\Type;
1111
use PHPStan\Type\VoidType;
1212
use function count;
13+
use function in_array;
1314

1415
#[AutowiredService]
1516
final class DsMapDynamicMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
@@ -18,7 +19,7 @@ final class DsMapDynamicMethodThrowTypeExtension implements DynamicMethodThrowTy
1819
public function isMethodSupported(MethodReflection $methodReflection): bool
1920
{
2021
return $methodReflection->getDeclaringClass()->getName() === 'Ds\Map'
21-
&& ($methodReflection->getName() === 'get' || $methodReflection->getName() === 'remove');
22+
&& in_array($methodReflection->getName(), ['get', 'remove'], true);
2223
}
2324

2425
public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type

src/Type/Php/StrWordCountFunctionDynamicReturnTypeExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Type;
1717
use PHPStan\Type\UnionType;
1818
use function count;
19+
use function in_array;
1920

2021
#[AutowiredService]
2122
final class StrWordCountFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -35,14 +36,14 @@ public function getTypeFromFunctionCall(
3536
$argsCount = count($functionCall->getArgs());
3637
if ($argsCount === 1) {
3738
return new IntegerType();
38-
} elseif ($argsCount === 2 || $argsCount === 3) {
39+
} elseif (in_array($argsCount, [2, 3], true)) {
3940
$formatType = $scope->getType($functionCall->getArgs()[1]->value);
4041
if ($formatType instanceof ConstantIntegerType) {
4142
$val = $formatType->getValue();
4243
if ($val === 0) {
4344
// return word count
4445
return new IntegerType();
45-
} elseif ($val === 1 || $val === 2) {
46+
} elseif (in_array($val, [1, 2], true)) {
4647
// return [word] or [offset => word]
4748
return new ArrayType(new IntegerType(), new StringType());
4849
}

src/Type/Regex/RegexGroupParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ private function getLiteralValue(TreeNode $node, ?array &$onlyLiterals, bool $ap
711711
}
712712
}
713713

714-
if ($token === 'anchor' || $token === 'match_point_reset') {
714+
if (in_array($token, ['anchor', 'match_point_reset'], true)) {
715715
return '';
716716
}
717717

0 commit comments

Comments
 (0)