Skip to content

Commit 860372f

Browse files
zonuexeondrejmirtes
authored andcommitted
Micro-optimize with in_array()
1 parent 3c22ef5 commit 860372f

File tree

9 files changed

+18
-25
lines changed

9 files changed

+18
-25
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ private function specifyTypesForConstantStringBinaryExpression(
974974
if ($constantType->getValue() === 'boolean') {
975975
$type = new BooleanType();
976976
}
977-
if ($constantType->getValue() === 'resource' || $constantType->getValue() === 'resource (closed)') {
977+
if (in_array($constantType->getValue(), ['resource', 'resource (closed)'], true)) {
978978
$type = new ResourceType();
979979
}
980980
if ($constantType->getValue() === 'integer') {

src/PhpDoc/TypeNodeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static function (string $variance): TemplateTypeVariance {
619619
$typeNode->variances,
620620
);
621621

622-
if ($mainTypeName === 'array' || $mainTypeName === 'non-empty-array') {
622+
if (in_array($mainTypeName, ['array', 'non-empty-array'], true)) {
623623
if (count($genericTypes) === 1) { // array<ValueType>
624624
$arrayType = new ArrayType(new BenevolentUnionType([new IntegerType(), new StringType()]), $genericTypes[0]);
625625
} elseif (count($genericTypes) === 2) { // array<KeyType, ValueType>
@@ -637,7 +637,7 @@ static function (string $variance): TemplateTypeVariance {
637637
}
638638

639639
return $arrayType;
640-
} elseif ($mainTypeName === 'list' || $mainTypeName === 'non-empty-list') {
640+
} elseif (in_array($mainTypeName, ['list', 'non-empty-list'], true)) {
641641
if (count($genericTypes) === 1) { // list<ValueType>
642642
$listType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $genericTypes[0]));
643643
if ($mainTypeName === 'non-empty-list') {

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function array_key_exists;
1212
use function count;
1313
use function ltrim;
14+
use function in_array;
1415
use function php_strip_whitespace;
1516
use function preg_match_all;
1617
use function preg_replace;
@@ -201,7 +202,7 @@ private function findSymbols(string $file): array
201202
$name = $matches['name'][$i];
202203

203204
// skip anon classes extending/implementing
204-
if ($name === 'extends' || $name === 'implements') {
205+
if (in_array($name, ['extends', 'implements'], true)) {
205206
continue;
206207
}
207208

src/Reflection/Php/PhpMethodFromParserNodeReflection.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\Type;
2121
use PHPStan\Type\TypeCombinator;
2222
use PHPStan\Type\VoidType;
23+
use function in_array;
2324
use function strtolower;
2425

2526
/**
@@ -57,13 +58,7 @@ public function __construct(
5758
)
5859
{
5960
$name = strtolower($classMethod->name->name);
60-
if (
61-
$name === '__construct'
62-
|| $name === '__destruct'
63-
|| $name === '__unset'
64-
|| $name === '__wakeup'
65-
|| $name === '__clone'
66-
) {
61+
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
6762
$realReturnType = new VoidType();
6863
}
6964
if ($name === '__tostring') {

src/Reflection/Php/PhpMethodReflection.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use function array_map;
3838
use function explode;
3939
use function filemtime;
40+
use function in_array;
4041
use function is_bool;
4142
use function sprintf;
4243
use function strtolower;
@@ -319,13 +320,7 @@ private function getReturnType(): Type
319320
$name = strtolower($this->getName());
320321
$returnType = $this->reflection->getReturnType();
321322
if ($returnType === null) {
322-
if (
323-
$name === '__construct'
324-
|| $name === '__destruct'
325-
|| $name === '__unset'
326-
|| $name === '__wakeup'
327-
|| $name === '__clone'
328-
) {
323+
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
329324
return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType);
330325
}
331326
if ($name === '__tostring') {

src/Rules/Classes/EnumSanityRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Serializable;
1414
use function array_key_exists;
1515
use function count;
16+
use function in_array;
1617
use function implode;
1718
use function sprintf;
1819

@@ -88,7 +89,7 @@ public function processNode(Node $node, Scope $scope): array
8889
continue;
8990
}
9091

91-
if ($lowercasedMethodName !== 'from' && $lowercasedMethodName !== 'tryfrom') {
92+
if (!in_array($lowercasedMethodName, ['from', 'tryfrom'], true)) {
9293
continue;
9394
}
9495

@@ -101,8 +102,7 @@ public function processNode(Node $node, Scope $scope): array
101102

102103
if (
103104
$enumNode->scalarType !== null
104-
&& $enumNode->scalarType->name !== 'int'
105-
&& $enumNode->scalarType->name !== 'string'
105+
&& !in_array($enumNode->scalarType->name, ['int', 'string'], true)
106106
) {
107107
$errors[] = RuleErrorBuilder::message(sprintf(
108108
'Backed enum %s can have only "int" or "string" type.',
@@ -124,7 +124,7 @@ public function processNode(Node $node, Scope $scope): array
124124
}
125125
$caseName = $stmt->name->name;
126126

127-
if (($stmt->expr instanceof Node\Scalar\LNumber || $stmt->expr instanceof Node\Scalar\String_)) {
127+
if ($stmt->expr instanceof Node\Scalar\LNumber || $stmt->expr instanceof Node\Scalar\String_) {
128128
if ($enumNode->scalarType === null) {
129129
$errors[] = RuleErrorBuilder::message(sprintf(
130130
'Enum %s is not backed, but case %s has value %s.',

src/Type/Constant/ConstantStringType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use PHPStan\Type\TypeCombinator;
4242
use PHPStan\Type\VerbosityLevel;
4343
use function addcslashes;
44+
use function in_array;
4445
use function is_float;
4546
use function is_int;
4647
use function is_numeric;
@@ -325,7 +326,7 @@ public function isNonEmptyString(): TrinaryLogic
325326

326327
public function isNonFalsyString(): TrinaryLogic
327328
{
328-
return TrinaryLogic::createFromBoolean($this->getValue() !== '' && $this->getValue() !== '0');
329+
return TrinaryLogic::createFromBoolean(!in_array($this->getValue(), ['', '0'], true));
329330
}
330331

331332
public function isLiteralString(): TrinaryLogic

src/Type/Php/DsMapDynamicReturnTypeExtension.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\TypeWithClassName;
1212
use function count;
13+
use function in_array;
1314

1415
final class DsMapDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1516
{
@@ -21,7 +22,7 @@ public function getClass(): string
2122

2223
public function isMethodSupported(MethodReflection $methodReflection): bool
2324
{
24-
return $methodReflection->getName() === 'get' || $methodReflection->getName() === 'remove';
25+
return in_array($methodReflection->getName(), ['get', 'remove'], true);
2526
}
2627

2728
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type

src/Type/Php/MbSubstituteCharacterDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
105105
if ($argType instanceof ConstantStringType) {
106106
$value = strtolower($argType->getValue());
107107

108-
if ($value === 'none' || $value === 'long' || $value === 'entity') {
108+
if (in_array($value, ['none', 'long', 'entity'], true)) {
109109
return new ConstantBooleanType(true);
110110
}
111111

0 commit comments

Comments
 (0)