Skip to content

Commit

Permalink
Micro-optimize with in_array()
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe authored and ondrejmirtes committed Sep 12, 2023
1 parent 3c22ef5 commit 860372f
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ private function specifyTypesForConstantStringBinaryExpression(
if ($constantType->getValue() === 'boolean') {
$type = new BooleanType();
}
if ($constantType->getValue() === 'resource' || $constantType->getValue() === 'resource (closed)') {
if (in_array($constantType->getValue(), ['resource', 'resource (closed)'], true)) {
$type = new ResourceType();
}
if ($constantType->getValue() === 'integer') {
Expand Down
4 changes: 2 additions & 2 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ static function (string $variance): TemplateTypeVariance {
$typeNode->variances,
);

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

return $arrayType;
} elseif ($mainTypeName === 'list' || $mainTypeName === 'non-empty-list') {
} elseif (in_array($mainTypeName, ['list', 'non-empty-list'], true)) {
if (count($genericTypes) === 1) { // list<ValueType>
$listType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $genericTypes[0]));
if ($mainTypeName === 'non-empty-list') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function array_key_exists;
use function count;
use function ltrim;
use function in_array;
use function php_strip_whitespace;
use function preg_match_all;
use function preg_replace;
Expand Down Expand Up @@ -201,7 +202,7 @@ private function findSymbols(string $file): array
$name = $matches['name'][$i];

// skip anon classes extending/implementing
if ($name === 'extends' || $name === 'implements') {
if (in_array($name, ['extends', 'implements'], true)) {
continue;
}

Expand Down
9 changes: 2 additions & 7 deletions src/Reflection/Php/PhpMethodFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VoidType;
use function in_array;
use function strtolower;

/**
Expand Down Expand Up @@ -57,13 +58,7 @@ public function __construct(
)
{
$name = strtolower($classMethod->name->name);
if (
$name === '__construct'
|| $name === '__destruct'
|| $name === '__unset'
|| $name === '__wakeup'
|| $name === '__clone'
) {
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
$realReturnType = new VoidType();
}
if ($name === '__tostring') {
Expand Down
9 changes: 2 additions & 7 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use function array_map;
use function explode;
use function filemtime;
use function in_array;
use function is_bool;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -319,13 +320,7 @@ private function getReturnType(): Type
$name = strtolower($this->getName());
$returnType = $this->reflection->getReturnType();
if ($returnType === null) {
if (
$name === '__construct'
|| $name === '__destruct'
|| $name === '__unset'
|| $name === '__wakeup'
|| $name === '__clone'
) {
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType);
}
if ($name === '__tostring') {
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/Classes/EnumSanityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Serializable;
use function array_key_exists;
use function count;
use function in_array;
use function implode;
use function sprintf;

Expand Down Expand Up @@ -88,7 +89,7 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ($lowercasedMethodName !== 'from' && $lowercasedMethodName !== 'tryfrom') {
if (!in_array($lowercasedMethodName, ['from', 'tryfrom'], true)) {
continue;
}

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

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

if (($stmt->expr instanceof Node\Scalar\LNumber || $stmt->expr instanceof Node\Scalar\String_)) {
if ($stmt->expr instanceof Node\Scalar\LNumber || $stmt->expr instanceof Node\Scalar\String_) {
if ($enumNode->scalarType === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Enum %s is not backed, but case %s has value %s.',
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;
use function addcslashes;
use function in_array;
use function is_float;
use function is_int;
use function is_numeric;
Expand Down Expand Up @@ -325,7 +326,7 @@ public function isNonEmptyString(): TrinaryLogic

public function isNonFalsyString(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->getValue() !== '' && $this->getValue() !== '0');
return TrinaryLogic::createFromBoolean(!in_array($this->getValue(), ['', '0'], true));
}

public function isLiteralString(): TrinaryLogic
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Php/DsMapDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use function count;
use function in_array;

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

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'get' || $methodReflection->getName() === 'remove';
return in_array($methodReflection->getName(), ['get', 'remove'], true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if ($argType instanceof ConstantStringType) {
$value = strtolower($argType->getValue());

if ($value === 'none' || $value === 'long' || $value === 'entity') {
if (in_array($value, ['none', 'long', 'entity'], true)) {
return new ConstantBooleanType(true);
}

Expand Down

0 comments on commit 860372f

Please sign in to comment.