Skip to content

Commit 0bf4e1e

Browse files
staabmondrejmirtes
authored andcommitted
TypeSpecifier: cheap checks first
1 parent 93b9c34 commit 0bf4e1e

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ public function specifyTypesInCondition(
233233

234234
if (
235235
$expr->left instanceof FuncCall
236-
&& count($expr->left->getArgs()) >= 1
237236
&& $expr->left->name instanceof Name
238237
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
238+
&& count($expr->left->getArgs()) >= 1
239239
&& (
240240
!$expr->right instanceof FuncCall
241241
|| !$expr->right->name instanceof Name
@@ -261,9 +261,9 @@ public function specifyTypesInCondition(
261261
if (
262262
!$context->null()
263263
&& $expr->right instanceof FuncCall
264-
&& count($expr->right->getArgs()) >= 1
265264
&& $expr->right->name instanceof Name
266265
&& in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true)
266+
&& count($expr->right->getArgs()) >= 1
267267
&& $leftType->isInteger()->yes()
268268
) {
269269
$argType = $scope->getType($expr->right->getArgs()[0]->value);
@@ -330,9 +330,9 @@ public function specifyTypesInCondition(
330330
if (
331331
!$context->null()
332332
&& $expr->right instanceof FuncCall
333-
&& count($expr->right->getArgs()) >= 3
334333
&& $expr->right->name instanceof Name
335334
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
335+
&& count($expr->right->getArgs()) >= 3
336336
&& (
337337
IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($leftType)->yes()
338338
|| ($expr instanceof Expr\BinaryOp\Smaller && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes())
@@ -347,9 +347,9 @@ public function specifyTypesInCondition(
347347
if (
348348
!$context->null()
349349
&& $expr->right instanceof FuncCall
350-
&& count($expr->right->getArgs()) === 1
351350
&& $expr->right->name instanceof Name
352351
&& in_array(strtolower((string) $expr->right->name), ['strlen', 'mb_strlen'], true)
352+
&& count($expr->right->getArgs()) === 1
353353
&& $leftType->isInteger()->yes()
354354
) {
355355
if (
@@ -479,8 +479,9 @@ public function specifyTypesInCondition(
479479

480480
$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
481481
$normalizedExpr = $expr;
482-
if (count($expr->getArgs()) > 0) {
483-
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
482+
$args = $expr->getArgs();
483+
if (count($args) > 0) {
484+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
484485
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
485486
}
486487

@@ -492,7 +493,7 @@ public function specifyTypesInCondition(
492493
return $extension->specifyTypes($functionReflection, $normalizedExpr, $scope, $context);
493494
}
494495

495-
if (count($expr->getArgs()) > 0) {
496+
if (count($args) > 0) {
496497
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
497498
if ($specifiedTypes !== null) {
498499
return $specifiedTypes;
@@ -501,7 +502,7 @@ public function specifyTypesInCondition(
501502

502503
$assertions = $functionReflection->getAsserts();
503504
if ($assertions->getAll() !== []) {
504-
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
505+
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $args, $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
505506

506507
$asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes(
507508
$type,
@@ -525,8 +526,9 @@ public function specifyTypesInCondition(
525526
$parametersAcceptor = null;
526527

527528
$normalizedExpr = $expr;
528-
if (count($expr->getArgs()) > 0) {
529-
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
529+
$args = $expr->getArgs();
530+
if (count($args) > 0) {
531+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
530532
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
531533
}
532534

@@ -545,7 +547,7 @@ public function specifyTypesInCondition(
545547
}
546548
}
547549

548-
if (count($expr->getArgs()) > 0) {
550+
if (count($args) > 0) {
549551
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
550552
if ($specifiedTypes !== null) {
551553
return $specifiedTypes;
@@ -554,7 +556,7 @@ public function specifyTypesInCondition(
554556

555557
$assertions = $methodReflection->getAsserts();
556558
if ($assertions->getAll() !== []) {
557-
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
559+
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
558560

559561
$asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes(
560562
$type,
@@ -583,8 +585,9 @@ public function specifyTypesInCondition(
583585
$parametersAcceptor = null;
584586

585587
$normalizedExpr = $expr;
586-
if (count($expr->getArgs()) > 0) {
587-
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
588+
$args = $expr->getArgs();
589+
if (count($args) > 0) {
590+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
588591
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
589592
}
590593

@@ -603,7 +606,7 @@ public function specifyTypesInCondition(
603606
}
604607
}
605608

606-
if (count($expr->getArgs()) > 0) {
609+
if (count($args) > 0) {
607610
$specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope);
608611
if ($specifiedTypes !== null) {
609612
return $specifiedTypes;
@@ -612,7 +615,7 @@ public function specifyTypesInCondition(
612615

613616
$assertions = $staticMethodReflection->getAsserts();
614617
if ($assertions->getAll() !== []) {
615-
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
618+
$parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $args, $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
616619

617620
$asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes(
618621
$type,
@@ -705,9 +708,10 @@ public function specifyTypesInCondition(
705708
&& count($expr->expr->getArgs()) >= 1
706709
) {
707710
$numArg = null;
708-
$arrayArg = $expr->expr->getArgs()[0]->value;
709-
if (count($expr->expr->getArgs()) > 1) {
710-
$numArg = $expr->expr->getArgs()[1]->value;
711+
$args = $expr->expr->getArgs();
712+
$arrayArg = $args[0]->value;
713+
if (count($args) > 1) {
714+
$numArg = $args[1]->value;
711715
}
712716
$one = new ConstantIntegerType(1);
713717
$arrayType = $scope->getType($arrayArg);
@@ -754,10 +758,10 @@ public function specifyTypesInCondition(
754758
$expr->expr instanceof Expr\BinaryOp\Minus
755759
&& $expr->expr->left instanceof FuncCall
756760
&& $expr->expr->left->name instanceof Name
757-
&& in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true)
758-
&& count($expr->expr->left->getArgs()) >= 1
759761
&& $expr->expr->right instanceof Node\Scalar\Int_
760762
&& $expr->expr->right->value === 1
763+
&& in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true)
764+
&& count($expr->expr->left->getArgs()) >= 1
761765
) {
762766
$arrayArg = $expr->expr->left->getArgs()[0]->value;
763767
$arrayType = $scope->getType($arrayArg);

0 commit comments

Comments
 (0)