Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ public function specifyTypesInCondition(

if (
$expr->left instanceof FuncCall
&& count($expr->left->getArgs()) >= 1
&& $expr->left->name instanceof Name
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
&& count($expr->left->getArgs()) >= 1
&& (
!$expr->right instanceof FuncCall
|| !$expr->right->name instanceof Name
Expand All @@ -261,9 +261,9 @@ public function specifyTypesInCondition(
if (
!$context->null()
&& $expr->right instanceof FuncCall
&& count($expr->right->getArgs()) >= 1
&& $expr->right->name instanceof Name
&& in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true)
&& count($expr->right->getArgs()) >= 1
&& $leftType->isInteger()->yes()
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
Expand Down Expand Up @@ -330,9 +330,9 @@ public function specifyTypesInCondition(
if (
!$context->null()
&& $expr->right instanceof FuncCall
&& count($expr->right->getArgs()) >= 3
&& $expr->right->name instanceof Name
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
&& count($expr->right->getArgs()) >= 3
&& (
IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($leftType)->yes()
|| ($expr instanceof Expr\BinaryOp\Smaller && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes())
Expand All @@ -347,9 +347,9 @@ public function specifyTypesInCondition(
if (
!$context->null()
&& $expr->right instanceof FuncCall
&& count($expr->right->getArgs()) === 1
&& $expr->right->name instanceof Name
&& in_array(strtolower((string) $expr->right->name), ['strlen', 'mb_strlen'], true)
&& count($expr->right->getArgs()) === 1
&& $leftType->isInteger()->yes()
) {
if (
Expand Down Expand Up @@ -479,8 +479,9 @@ public function specifyTypesInCondition(

$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
$normalizedExpr = $expr;
if (count($expr->getArgs()) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
$args = $expr->getArgs();
if (count($args) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants());
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
}

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

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

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

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

$normalizedExpr = $expr;
if (count($expr->getArgs()) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
$args = $expr->getArgs();
if (count($args) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
}

Expand All @@ -545,7 +547,7 @@ public function specifyTypesInCondition(
}
}

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

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

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

$normalizedExpr = $expr;
if (count($expr->getArgs()) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
$args = $expr->getArgs();
if (count($args) > 0) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $args, $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants());
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
}

Expand All @@ -603,7 +606,7 @@ public function specifyTypesInCondition(
}
}

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

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

$asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes(
$type,
Expand Down Expand Up @@ -705,9 +708,10 @@ public function specifyTypesInCondition(
&& count($expr->expr->getArgs()) >= 1
) {
$numArg = null;
$arrayArg = $expr->expr->getArgs()[0]->value;
if (count($expr->expr->getArgs()) > 1) {
$numArg = $expr->expr->getArgs()[1]->value;
$args = $expr->expr->getArgs();
$arrayArg = $args[0]->value;
if (count($args) > 1) {
$numArg = $args[1]->value;
}
$one = new ConstantIntegerType(1);
$arrayType = $scope->getType($arrayArg);
Expand Down Expand Up @@ -754,10 +758,10 @@ public function specifyTypesInCondition(
$expr->expr instanceof Expr\BinaryOp\Minus
&& $expr->expr->left instanceof FuncCall
&& $expr->expr->left->name instanceof Name
&& in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true)
&& count($expr->expr->left->getArgs()) >= 1
&& $expr->expr->right instanceof Node\Scalar\Int_
&& $expr->expr->right->value === 1
&& in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true)
&& count($expr->expr->left->getArgs()) >= 1
) {
$arrayArg = $expr->expr->left->getArgs()[0]->value;
$arrayType = $scope->getType($arrayArg);
Expand Down
Loading