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
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
use PHPStan\Type\Accessory\OversizedArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
Expand Down Expand Up @@ -504,7 +506,7 @@ public function toInteger(): Type
new ConstantIntegerType(0),
new ConstantArrayType([], []),
new StringType(),
new FloatType(),
new FloatType(), // every 0.x float casts to int(0)
]);
if (
$this->subtractedType !== null
Expand All @@ -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([], []),
			]);

new NullType(),
new ConstantBooleanType(false),
new ConstantStringType(''),
]);
if ($this->subtractedType->isSuperTypeOf($castsToEmptyString)->yes()) {
$accessories = [
new StringType(),
new AccessoryNonEmptyStringType(),
];

$castsToZeroString = new UnionType([
new ConstantFloatType(0.0),
new ConstantStringType('0'),
new ConstantIntegerType(0),
]);
if ($this->subtractedType->isSuperTypeOf($castsToZeroString)->yes()) {
$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 :)

);
}
}

return new StringType();
}

Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,40 @@ function multiplesPrintfFormats(string $s) {
assertType('non-empty-string', sprintf($nonEmpty, $s));
assertType('non-falsy-string', sprintf($nonFalsy, $s));
}

function subtract($m) {
if ($m) {
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $m);
assertType('non-falsy-string', (string) $m);
}
if ($m != '') {
assertType("mixed", $m);
assertType('string', (string) $m);
}
if ($m !== '') {
assertType("mixed~''", $m);
assertType('string', (string) $m);
}
if (!is_string($m)) {
assertType("mixed~string", $m);
assertType('string', (string) $m);
}

if ($m !== true) {
assertType("mixed~true", $m);
assertType('string', (string) $m);
}
if ($m !== false) {
assertType("mixed~false", $m);
assertType('string', (string) $m);
}
if ($m !== false && $m !== '' && $m !== null) {
assertType("mixed~(''|false|null)", $m);
assertType('non-empty-string', (string) $m);
}
if (!is_bool($m) && $m !== '' && $m !== null) {
assertType("mixed~(''|bool|null)", $m);
assertType('non-empty-string', (string) $m);
}
}
}
Loading