Skip to content
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
8 changes: 6 additions & 2 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,19 @@ public function describe(VerbosityLevel $level): string
function () use ($level): string {
$description = 'mixed';
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}

return $description;
},
function () use ($level): string {
$description = 'mixed';
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}

if ($this->isExplicitMixed) {
Expand Down
8 changes: 6 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ public function describe(VerbosityLevel $level): string
$preciseWithSubtracted = function () use ($level): string {
$description = $this->className;
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}

return $description;
Expand Down Expand Up @@ -538,7 +540,9 @@ private function describeCache(): string
}

if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe(VerbosityLevel::cache()));
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe(VerbosityLevel::cache()))
: sprintf('~%s', $this->subtractedType->describe(VerbosityLevel::cache()));
}

$reflection = $this->classReflection;
Expand Down
4 changes: 3 additions & 1 deletion src/Type/ObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ public function describe(VerbosityLevel $level): string
function () use ($level): string {
$description = 'object';
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}

return $description;
Expand Down
24 changes: 12 additions & 12 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TypeSpecifierTest extends PHPStanTestCase
{

private const FALSEY_TYPE_DESCRIPTION = '0|0.0|\'\'|\'0\'|array{}|false|null';
private const TRUTHY_TYPE_DESCRIPTION = 'mixed~' . self::FALSEY_TYPE_DESCRIPTION;
private const TRUTHY_TYPE_DESCRIPTION = 'mixed~(' . self::FALSEY_TYPE_DESCRIPTION . ')';
private const SURE_NOT_FALSEY = '~' . self::FALSEY_TYPE_DESCRIPTION;
private const SURE_NOT_TRUTHY = '~' . self::TRUTHY_TYPE_DESCRIPTION;

Expand Down Expand Up @@ -819,10 +819,10 @@ public function dataCondition(): iterable
new LNumber(3),
),
[
'$n' => 'mixed~int<3, max>|true',
'$n' => 'mixed~(int<3, max>|true)',
],
[
'$n' => 'mixed~0.0|int<min, 2>|false|null',
'$n' => 'mixed~(0.0|int<min, 2>|false|null)',
],
],
[
Expand All @@ -831,10 +831,10 @@ public function dataCondition(): iterable
new LNumber(PHP_INT_MIN),
),
[
'$n' => 'mixed~int<' . PHP_INT_MIN . ', max>|true',
'$n' => 'mixed~(int<' . PHP_INT_MIN . ', max>|true)',
],
[
'$n' => 'mixed~0.0|false|null',
'$n' => 'mixed~(0.0|false|null)',
],
],
[
Expand All @@ -843,7 +843,7 @@ public function dataCondition(): iterable
new LNumber(PHP_INT_MAX),
),
[
'$n' => 'mixed~0.0|bool|int<min, ' . PHP_INT_MAX . '>|null',
'$n' => 'mixed~(0.0|bool|int<min, ' . PHP_INT_MAX . '>|null)',
],
[
'$n' => 'mixed',
Expand All @@ -858,7 +858,7 @@ public function dataCondition(): iterable
'$n' => 'mixed~int<' . (PHP_INT_MIN + 1) . ', max>',
],
[
'$n' => 'mixed~0.0|bool|int<min, ' . PHP_INT_MIN . '>|null',
'$n' => 'mixed~(0.0|bool|int<min, ' . PHP_INT_MIN . '>|null)',
],
],
[
Expand All @@ -867,10 +867,10 @@ public function dataCondition(): iterable
new LNumber(PHP_INT_MAX),
),
[
'$n' => 'mixed~0.0|int<min, ' . (PHP_INT_MAX - 1) . '>|false|null',
'$n' => 'mixed~(0.0|int<min, ' . (PHP_INT_MAX - 1) . '>|false|null)',
],
[
'$n' => 'mixed~int<' . PHP_INT_MAX . ', max>|true',
'$n' => 'mixed~(int<' . PHP_INT_MAX . ', max>|true)',
],
],
[
Expand All @@ -885,10 +885,10 @@ public function dataCondition(): iterable
),
),
[
'$n' => 'mixed~0.0|int<min, 2>|int<6, max>|false|null',
'$n' => 'mixed~(0.0|int<min, 2>|int<6, max>|false|null)',
],
[
'$n' => 'mixed~int<3, 5>|true',
'$n' => 'mixed~(int<3, 5>|true)',
],
],
[
Expand Down Expand Up @@ -1250,7 +1250,7 @@ public function dataCondition(): iterable
),
[
'$foo' => 'non-empty-array',
'count($foo)' => 'mixed~0.0|int<min, 1>|false|null',
'count($foo)' => 'mixed~(0.0|int<min, 1>|false|null)',
],
[],
],
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/assert-empty.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ function ($var) {

function ($var) {
assertNotEmpty($var);
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $var);
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $var);
};
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-10189.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function file_managed_file_save_upload(): array {
}
assertType('non-empty-array|Bug10189\SomeInterface', $files);
$files = array_filter($files);
assertType("array<mixed~0|0.0|''|'0'|array{}|false|null>", $files);
assertType("array<mixed~(0|0.0|''|'0'|array{}|false|null)>", $files);

return empty($files) ? [] : [1,2];
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-3991.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function email($config = null)
assertType('array{}|null', $config);
$config = new \stdClass();
} elseif (! (is_array($config) || $config instanceof \stdClass)) {
assertNativeType('mixed~0|0.0|\'\'|\'0\'|array{}|stdClass|false|null', $config);
assertNativeType('mixed~(0|0.0|\'\'|\'0\'|array{}|stdClass|false|null)', $config);
assertType('*NEVER*', $config);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-3993.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function doFoo($arguments)
return;
}

assertType('mixed~array{}|null', $arguments);
assertType('mixed~(array{}|null)', $arguments);

array_shift($arguments);

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-4117.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function broken(int $key)
$item = $this->items[$key] ?? null;
assertType('T of mixed~null (class Bug4117Types\GenericList, argument)|null', $item);
if ($item) {
assertType("T of mixed~0|0.0|''|'0'|array{}|false|null (class Bug4117Types\GenericList, argument)", $item);
assertType("T of mixed~(0|0.0|''|'0'|array{}|false|null) (class Bug4117Types\GenericList, argument)", $item);
} else {
assertType("(array{}&T of mixed~null (class Bug4117Types\GenericList, argument))|(0.0&T of mixed~null (class Bug4117Types\GenericList, argument))|(0&T of mixed~null (class Bug4117Types\GenericList, argument))|(''&T of mixed~null (class Bug4117Types\GenericList, argument))|('0'&T of mixed~null (class Bug4117Types\GenericList, argument))|(T of mixed~null (class Bug4117Types\GenericList, argument)&false)|null", $item);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-7176.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function test(Suit $x): string {
assertType('Bug7176Types\Suit::Spades', $x);
return 'DOES NOT WORK';
}
assertType('Bug7176Types\Suit~Bug7176Types\Suit::Clubs|Bug7176Types\Suit::Spades', $x);
assertType('Bug7176Types\Suit~(Bug7176Types\Suit::Clubs|Bug7176Types\Suit::Spades)', $x);

return match ($x) {
Suit::Hearts => 'a',
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-pr-339.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
assertType('mixed', $a);
assertType('mixed', $c);
if ($a) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $a);
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $a);
assertType('mixed', $c);
assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

if ($c) {
assertType('mixed', $a);
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $c);
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $c);
assertVariableCertainty(TrinaryLogic::createYes(), $c);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/callsite-cast-narrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function sayHello($mixed, int $int, string $string, $numericString, $nonE
if (ctype_digit((string) $mixed)) {
assertType('int<0, max>|numeric-string|true', $mixed);
} else {
assertType('mixed~int<0, max>|numeric-string|true', $mixed);
assertType('mixed~(int<0, max>|numeric-string|true)', $mixed);
}
assertType('mixed', $mixed);

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/composer-array-bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function doFoo(): void
unset($this->config['authors']);
assertType("array<mixed~'authors', mixed>", $this->config);
} else {
assertType("array&hasOffsetValue('authors', mixed~0|0.0|''|'0'|array{}|false|null)", $this->config);
assertType("array&hasOffsetValue('authors', mixed~(0|0.0|''|'0'|array{}|false|null))", $this->config);
}

assertType('array', $this->config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Foo
public function doFoo()
{
if (!empty($this->config['authors'])) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $this->config['authors']);
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $this->config['authors']);
foreach ($this->config['authors'] as $key => $author) {
assertType("mixed", $this->config['authors']);
if (!is_array($author)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/ctype-digit.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function foo(mixed $foo): void
if (is_int($foo) && ctype_digit($foo)) {
assertType('int<48, 57>|int<256, max>', $foo);
} else {
assertType('mixed~int<48, 57>|int<256, max>', $foo);
assertType('mixed~(int<48, 57>|int<256, max>)', $foo);
}

if (ctype_digit($foo)) {
assertType('int<48, 57>|int<256, max>|numeric-string', $foo);
return;
}

assertType('mixed~int<48, 57>|int<256, max>', $foo); // not all numeric strings are covered by ctype_digit
assertType('mixed~(int<48, 57>|int<256, max>)', $foo); // not all numeric strings are covered by ctype_digit
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/more-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function doFoo(
assertType('literal-string&non-empty-string', $nonEmptyLiteralString);
assertType('float|int<min, -1>|int<1, max>|non-falsy-string|true', $nonEmptyScalar);
assertType("0|0.0|''|'0'|false", $emptyScalar);
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $nonEmptyMixed);
assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $nonEmptyMixed);
}

}
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/this-subtractable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function doFoo()
assertType('$this(ThisSubtractable\Foo)', $this);

if (!$this instanceof Bar && !$this instanceof Baz) {
assertType('$this(ThisSubtractable\Foo~ThisSubtractable\Bar|ThisSubtractable\Baz)', $this);
assertType('$this(ThisSubtractable\Foo~(ThisSubtractable\Bar|ThisSubtractable\Baz))', $this);
} else {
assertType('($this(ThisSubtractable\Foo)&ThisSubtractable\Bar)|($this(ThisSubtractable\Foo)&ThisSubtractable\Baz)', $this);
}
Expand All @@ -26,7 +26,7 @@ public function doBar()
assertType('static(ThisSubtractable\Foo)', $s);

if (!$s instanceof Bar && !$s instanceof Baz) {
assertType('static(ThisSubtractable\Foo~ThisSubtractable\Bar|ThisSubtractable\Baz)', $s);
assertType('static(ThisSubtractable\Foo~(ThisSubtractable\Bar|ThisSubtractable\Baz))', $s);
} else {
assertType('(static(ThisSubtractable\Foo)&ThisSubtractable\Bar)|(static(ThisSubtractable\Foo)&ThisSubtractable\Baz)', $s);
}
Expand All @@ -52,7 +52,7 @@ public function doBazz(self $s)
assertType('ThisSubtractable\Foo', $s);

if (!$s instanceof Bar && !$s instanceof Baz) {
assertType('ThisSubtractable\Foo~ThisSubtractable\Bar|ThisSubtractable\Baz', $s);
assertType('ThisSubtractable\Foo~(ThisSubtractable\Bar|ThisSubtractable\Baz)', $s);
} else {
assertType('ThisSubtractable\Bar|ThisSubtractable\Baz', $s);
}
Expand All @@ -70,12 +70,12 @@ public function doBazzz(self $s)
assertType('ThisSubtractable\Foo&hasMethod(test123)', $s);

if (!$s instanceof Bar && !$s instanceof Baz) {
assertType('ThisSubtractable\Foo~ThisSubtractable\Bar|ThisSubtractable\Baz&hasMethod(test123)', $s);
assertType('ThisSubtractable\Foo~(ThisSubtractable\Bar|ThisSubtractable\Baz)&hasMethod(test123)', $s);
} else {
assertType('(ThisSubtractable\Bar&hasMethod(test123))|(ThisSubtractable\Baz&hasMethod(test123))', $s);
}

assertType('(ThisSubtractable\Bar&hasMethod(test123))|(ThisSubtractable\Baz&hasMethod(test123))|(ThisSubtractable\Foo~ThisSubtractable\Bar|ThisSubtractable\Baz&hasMethod(test123))', $s);
assertType('(ThisSubtractable\Bar&hasMethod(test123))|(ThisSubtractable\Baz&hasMethod(test123))|(ThisSubtractable\Foo~(ThisSubtractable\Bar|ThisSubtractable\Baz)&hasMethod(test123))', $s);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/throw-points/try-catch.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function (): void {
assertVariableCertainty(TrinaryLogic::createMaybe(), $baz);
assertType('1|2', $baz);
} catch (\Throwable $e) {
assertType('Throwable~InvalidArgumentException|RuntimeException', $e);
assertType('Throwable~(InvalidArgumentException|RuntimeException)', $e);
assertVariableCertainty(TrinaryLogic::createNo(), $foo);
assertVariableCertainty(TrinaryLogic::createYes(), $bar);
assertVariableCertainty(TrinaryLogic::createNo(), $baz);
Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ public function dataUnion(): iterable
StaticTypeFactory::truthy(),
],
MixedType::class,
'mixed~0|0.0|\'\'|\'0\'|array{}|false|null=implicit',
'mixed~(0|0.0|\'\'|\'0\'|array{}|false|null)=implicit',
],
[
[
Expand Down Expand Up @@ -3325,7 +3325,7 @@ public function dataIntersect(): iterable
new MixedType(false, new IntegerType()),
],
MixedType::class,
'mixed~int|string=implicit',
'mixed~(int|string)=implicit',
],
[
[
Expand Down Expand Up @@ -4494,7 +4494,7 @@ public function dataRemove(): array
StaticTypeFactory::truthy(),
StaticTypeFactory::falsey(),
MixedType::class,
'mixed~0|0.0|\'\'|\'0\'|array{}|false|null',
'mixed~(0|0.0|\'\'|\'0\'|array{}|false|null)',
],
[
StaticTypeFactory::falsey(),
Expand Down Expand Up @@ -4670,7 +4670,7 @@ public function dataRemove(): array
new MixedType(false, new IntegerType()),
new StringType(),
MixedType::class,
'mixed~int|string',
'mixed~(int|string)',
],
[
new MixedType(false),
Expand Down Expand Up @@ -4706,7 +4706,7 @@ public function dataRemove(): array
new ObjectType('Exception', new ObjectType('InvalidArgumentException')),
new ObjectType('LengthException'),
ObjectType::class,
'Exception~InvalidArgumentException|LengthException',
'Exception~(InvalidArgumentException|LengthException)',
],
[
new ObjectType('Exception'),
Expand Down
Loading