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

More precise mixed-type subtraction #3420

Merged
merged 2 commits into from
Sep 22, 2024
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 8, 2024

realized this nit while looking more into division related types

@staabm staabm force-pushed the mixsub branch 2 times, most recently from a26a7a0 to 1f9a3bf Compare September 8, 2024 14:54
@staabm
Copy link
Contributor Author

staabm commented Sep 9, 2024

after a break and thinking about your feedback, I came to a new implementation idea :).

@staabm
Copy link
Contributor Author

staabm commented Sep 12, 2024

just reduced this PR to mixed->toString() and will to the toInteger part in a separate PR

$accessories[] = new AccessoryNonFalsyStringType();
}
return new IntersectionType(
$accessories,
Copy link
Member

Choose a reason for hiding this comment

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

I think I'm missing floats here. See https://3v4l.org/7kgpL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe I missed your point, but float 0.0 is substracted in line 544

Copy link
Member

Choose a reason for hiding this comment

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

I was caught off guard a bit because this method (https://github.com/phpstan/phpstan-src/pull/3434/files#diff-23a89368c86f55bda583e138270384edec06835b889677bb17fc84e581e36dfcR494) mentions whole FloatType and this one mentions just constant 0.0, is there a difference on purpose or does it even matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the difference is, that there is a bunch of float numbers which cast to int 0, but only a single one which casts to string '0'

https://3v4l.org/Q1cva

Copy link
Member

Choose a reason for hiding this comment

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

Oh right :)

@ondrejmirtes ondrejmirtes merged commit b40a1f6 into phpstan:1.12.x Sep 22, 2024
360 of 376 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the mixsub branch September 22, 2024 19:04
@@ -526,6 +528,32 @@ public function toFloat(): Type

public function toString(): Type
{
if ($this->subtractedType !== null) {
$castsToEmptyString = new UnionType([
Copy link
Contributor

Choose a reason for hiding this comment

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

In the same way there AllowedArrayKeysTypes::getType I dunno if it would be interesting to introduce a class/static methods for

  • castsToZero
  • castsToEmptyString
  • castsToZeroString
  • ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you see a use-case where we need the same implementation somewhere else?

Copy link
Contributor

Choose a reason for hiding this comment

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

No indeed, there is all the following union which is part similar but always different so far

NullType::getSmallerOrEqualType

		return new UnionType([
			new NullType(),
			new ConstantBooleanType(false),
			new ConstantIntegerType(0),
			new ConstantFloatType(0.0),
			new ConstantStringType(''),
			new ConstantArrayType([], []),
		]);

NullType::getGreaterType

return new MixedType(false, new UnionType([
			new NullType(),
			new ConstantBooleanType(false),
			new ConstantIntegerType(0),
			new ConstantFloatType(0.0),
			new ConstantStringType(''),
			new ConstantArrayType([], []),
		]));
$castsToZero = new UnionType([
			new NullType(),
			new ConstantBooleanType(false),
			new ConstantIntegerType(0),
			new ConstantArrayType([], []),
			new StringType(),
			new FloatType(), // every 0.x float casts to int(0)
		]);
$castsToEmptyString = new UnionType([
				new NullType(),
				new ConstantBooleanType(false),
				new ConstantStringType(''),
			]);
$castsToZeroString = new UnionType([
					new ConstantFloatType(0.0),
					new ConstantStringType('0'),
					new ConstantIntegerType(0),
				]);

StaticTypeFactory::falsey

$falsey = new UnionType([
				new NullType(),
				new ConstantBooleanType(false),
				new ConstantIntegerType(0),
				new ConstantFloatType(0.0),
				new ConstantStringType(''),
				new ConstantStringType('0'),
				new ConstantArrayType([], []),
			]);

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

Successfully merging this pull request may close these issues.

None yet

3 participants