Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support integer-range type in min/max #627

Merged
merged 8 commits into from Aug 20, 2021
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 19, 2021

rewrite min($x, $y) as $x < $y ? $x : $y
implemented the integer-range min/max support based on the idea of ondrey in #604 (comment)

closes phpstan/phpstan#5398

this fixes the reported issue, but I can think of more complex scenarios which are not yet covered, e.g.

	/**
	 * @param int<0, 10> $range1
	 * @param int<3, 9> $range2
	 * @param int<4, 5> $range3
	 * @param int $int
	 */
	public function complexRanges($range1, $range2, $range3, $int) {
		assertType('int', min($range1, $range2, $int));
		assertType('int', max($range1, $range2, $int));

		assertType('int<0, 5>', min($range1, $range2, $range3));
		assertType('int<4, 10>', max($range1, $range2, $range3));
	}

but I think these should be covered in a separate PR, as they are rare and this PR - as is - already covers a lot of real world code

closes #604

@staabm staabm marked this pull request as ready for review August 19, 2021 18:30
}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it possible that we could delete some code that's already in the extension thanks to this logic?

Copy link
Contributor Author

@staabm staabm Aug 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code above only handles the 1-arg case and the code below handles the any-number-of-args case.

I guess we can drop it, after we push the

return $scope->getType(new Ternary(
						new Smaller($args[0]->value, $args[1]->value),
						$args[1]->value,
						$args[0]->value
					));

approach into a direction where it supports any number of args (using BooleanAnd and BooleanOr) as you suggested in #604 (comment)

but IMO this should be a separate PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool :)

@ondrejmirtes ondrejmirtes merged commit 458f5d2 into phpstan:master Aug 20, 2021
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants