Skip to content

Commit

Permalink
Merge branch refs/heads/1.12.x into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot committed Sep 22, 2024
2 parents baa3d1e + b40a1f6 commit 45c4648
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
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([
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,
);
}
}

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);
}
}
}

0 comments on commit 45c4648

Please sign in to comment.