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
20 changes: 4 additions & 16 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\SubstractableTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use function get_class;
use function sprintf;
Expand All @@ -47,6 +48,7 @@ class MixedType implements CompoundType, SubtractableType
use NonGenericTypeTrait;
use UndecidedComparisonCompoundTypeTrait;
use NonGeneralizableTypeTrait;
use SubstractableTypeTrait;

private ?Type $subtractedType;

Expand Down Expand Up @@ -471,23 +473,9 @@ public function describe(VerbosityLevel $level): string
return $level->handle(
static fn (): string => 'mixed',
static fn (): string => 'mixed',
fn (): string => 'mixed' . $this->describeSubtractedType($this->subtractedType, $level),
function () use ($level): string {
$description = 'mixed';
if ($this->subtractedType !== null) {
$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 .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}
$description = 'mixed' . $this->describeSubtractedType($this->subtractedType, $level);

if ($this->isExplicitMixed) {
$description .= '=explicit';
Expand Down
19 changes: 4 additions & 15 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use PHPStan\Type\Traits\NonArrayTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\SubstractableTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use Stringable;
use Throwable;
Expand All @@ -68,6 +69,7 @@ class ObjectType implements TypeWithClassName, SubtractableType
use NonGenericTypeTrait;
use UndecidedComparisonTypeTrait;
use NonGeneralizableTypeTrait;
use SubstractableTypeTrait;

private const EXTRA_OFFSET_CLASSES = [
'DOMNamedNodeMap', // Only read and existence
Expand Down Expand Up @@ -504,16 +506,7 @@ public function describe(VerbosityLevel $level): string
return $reflectionProvider->getClassName($this->className);
};

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

return $description;
};
$preciseWithSubtracted = fn (): string => $this->className . $this->describeSubtractedType($this->subtractedType, $level);

return $level->handle(
$preciseNameCallback,
Expand Down Expand Up @@ -559,11 +552,7 @@ private function describeCache(): string
$description .= '<' . implode(', ', $typeDescriptions) . '>';
}

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

$reflection = $this->classReflection;
if ($reflection !== null) {
Expand Down
14 changes: 3 additions & 11 deletions src/Type/ObjectWithoutClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\ObjectTypeTrait;
use PHPStan\Type\Traits\SubstractableTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use function sprintf;

/** @api */
class ObjectWithoutClassType implements SubtractableType
Expand All @@ -18,6 +18,7 @@ class ObjectWithoutClassType implements SubtractableType
use NonGenericTypeTrait;
use UndecidedComparisonTypeTrait;
use NonGeneralizableTypeTrait;
use SubstractableTypeTrait;

private ?Type $subtractedType;

Expand Down Expand Up @@ -120,16 +121,7 @@ public function describe(VerbosityLevel $level): string
return $level->handle(
static fn (): string => 'object',
static fn (): string => 'object',
function () use ($level): string {
$description = 'object';
if ($this->subtractedType !== null) {
$description .= $this->subtractedType instanceof UnionType
? sprintf('~(%s)', $this->subtractedType->describe($level))
: sprintf('~%s', $this->subtractedType->describe($level));
}

return $description;
},
fn (): string => 'object' . $this->describeSubtractedType($this->subtractedType, $level),
);
}

Expand Down
30 changes: 30 additions & 0 deletions src/Type/Traits/SubstractableTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Traits;

use PHPStan\Type\SubtractableType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

trait SubstractableTypeTrait
{

public function describeSubtractedType(?Type $subtractedType, VerbosityLevel $level): string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dunno if it's worth to add this method to the interface (I didn't) but since the logic was used 5 times I thought it could been useful to have a specific method (for later updates ?)

{
if ($subtractedType === null) {
return '';
}

if (
$subtractedType instanceof UnionType
|| ($subtractedType instanceof SubtractableType && $subtractedType->getSubtractedType() !== null)
) {
return sprintf('~(%s)', $subtractedType->describe($level));
}

return sprintf('~%s', $subtractedType->describe($level));
}

}
12 changes: 12 additions & 0 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,18 @@ private static function unionWithSubtractedType(
return $type;
}

if ($subtractedType instanceof SubtractableType) {
$withoutSubtracted = $subtractedType->getTypeWithoutSubtractedType();
if ($withoutSubtracted->isSuperTypeOf($type)->yes()) {
$subtractedSubtractedType = $subtractedType->getSubtractedType();
if ($subtractedSubtractedType === null) {
return new NeverType();
}

return self::intersect($type, $subtractedSubtractedType);
}
}

if ($type instanceof SubtractableType) {
$subtractedType = $type->getSubtractedType() === null
? $subtractedType
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/subtracted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Substracted;


use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param mixed $date
* @param bool $foo
*/
public function sayHello($date, $foo): void
{
if(is_object($date)){

} else {
assertType('mixed~object', $date);

if ($foo) {
$date = new \stdClass();
}
assertType('mixed~(object~stdClass)', $date);

if (is_object($date)) {
assertType('stdClass', $date);
}
}
}
}
10 changes: 9 additions & 1 deletion tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ public static function dataUnion(): iterable
new ObjectType('InvalidArgumentException'),
],
MixedType::class,
'mixed~Exception~InvalidArgumentException=implicit',
'mixed~(Exception~InvalidArgumentException)=implicit',
],
[
[
Expand Down Expand Up @@ -4143,6 +4143,14 @@ public static function dataIntersect(): iterable
ObjectWithoutClassType::class,
'object',
],
[
[
new MixedType(subtractedType: new ObjectWithoutClassType(new ObjectType('stdClass'))),
new ObjectWithoutClassType(),
],
ObjectType::class,
'stdClass',
],
];

if (PHP_VERSION_ID < 80100) {
Expand Down
Loading