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
48 changes: 42 additions & 6 deletions src/Type/ErrorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
namespace PHPStan\Type;

use PHPStan\Turbo\ShadowedByTurboExtension;
use function sprintf;

/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/ErrorType.cpp')]
class ErrorType extends MixedType
{

/** @api */
public function __construct(private ?string $reason = null)
/**
* @api
* @param Type|null $witnessType What some path is known to produce, see MixedType::__construct().
*/
public function __construct(private ?string $reason = null, ?Type $witnessType = null)
{
parent::__construct();
parent::__construct(witnessType: $witnessType);
}

public function getReason(): ?string
Expand All @@ -25,10 +29,24 @@ public function describe(VerbosityLevel $level): string
return $level->handle(
fn (): string => parent::describe($level),
fn (): string => parent::describe($level),
static fn (): string => '*ERROR*',
fn (): string => '*ERROR*' . $this->describeWitnessType($level),
);
}

private function describeWitnessType(VerbosityLevel $level): string
{
$witnessType = $this->getWitnessType();
if ($witnessType === null) {
return '';
}

if ($witnessType instanceof UnionType) {
return sprintf('+(%s)', $witnessType->describe($level));
}

return sprintf('+%s', $witnessType->describe($level));
}

public function getIterableKeyType(): Type
{
return new ErrorType();
Expand All @@ -41,12 +59,30 @@ public function getIterableValueType(): Type

public function subtract(Type $type): Type
{
return new self();
$witnessType = $this->getWitnessType();

return $this->recreate(false, null, $witnessType !== null ? TypeCombinator::remove($witnessType, $type) : null);
}

public function equals(Type $type): bool
{
return $type instanceof self;
if (!$type instanceof self) {
return false;
}

$witnessType = $this->getWitnessType();
$otherWitnessType = $type->getWitnessType();
if ($witnessType === null || $otherWitnessType === null) {
return $witnessType === $otherWitnessType;
}

return $witnessType->equals($otherWitnessType);
}

/** An error has nothing subtracted and is not an explicit mixed. */
protected function recreate(bool $isExplicitMixed, ?Type $subtractedType, ?Type $witnessType): MixedType
{
return new self($this->reason, $witnessType);
}

public function toNativePhpTypeNode(NativeTypePosition $position)
Expand Down
14 changes: 10 additions & 4 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ public function subtract(Type $type): Type
$type = TypeCombinator::union($this->subtractedType, $type);
}

return new self($this->isExplicitMixed, $type, $witnessType);
return $this->recreate($this->isExplicitMixed, $type, $witnessType);
}

public function getTypeWithoutSubtractedType(): Type
Expand All @@ -912,7 +912,7 @@ public function getTypeWithoutSubtractedType(): Type

public function changeSubtractedType(?Type $subtractedType): Type
{
return new self($this->isExplicitMixed, $subtractedType, $this->witnessType);
return $this->recreate($this->isExplicitMixed, $subtractedType, $this->witnessType);
}

/**
Expand All @@ -938,7 +938,13 @@ public function getWitnessType(): ?Type

public function changeWitnessType(?Type $witnessType): self
{
return new self($this->isExplicitMixed, $this->subtractedType, $witnessType);
return $this->recreate($this->isExplicitMixed, $this->subtractedType, $witnessType);
}

/** A copy of this type with these parts, which a subclass that is a mixed of its own kind keeps its kind in. */
protected function recreate(bool $isExplicitMixed, ?Type $subtractedType, ?Type $witnessType): self
{
return new self($isExplicitMixed, $subtractedType, $witnessType);
}

public function getSubtractedType(): ?Type
Expand All @@ -957,7 +963,7 @@ public function traverse(callable $cb): Type
return $this;
}

return new self($this->isExplicitMixed, $this->subtractedType, $witnessType);
return $this->recreate($this->isExplicitMixed, $this->subtractedType, $witnessType);
}

public function traverseSimultaneously(Type $right, callable $cb): Type
Expand Down
12 changes: 9 additions & 3 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,19 @@ public static function union(Type ...$types): Type
public static function joinValues(Type ...$types): Type
{
$union = self::union(...$types);
if (get_class($union) !== MixedType::class || count($types) < 2) {
if (!$union instanceof MixedType || !self::holdsWitnessType($union) || count($types) < 2) {
return $union;
}

return self::witnessAbsorbedTypes($union, ...$types);
}

/** A plain mixed or error keeps a witness type; a template or another mixed of its own kind does not. */
private static function holdsWitnessType(Type $type): bool
{
return get_class($type) === MixedType::class || get_class($type) === ErrorType::class;
}

/**
* A mixed that absorbed other members keeps them as its witness type, so a
* path known to produce a value of the wrong type is still reported where
Expand All @@ -344,7 +350,7 @@ public static function witnessAbsorbedTypes(MixedType $union, Type ...$types): M
foreach ($types as $type) {
$members = $type instanceof UnionType && !$type instanceof TemplateType ? $type->getTypes() : [$type];
foreach ($members as $member) {
if (get_class($member) === MixedType::class) {
if ($member instanceof MixedType && self::holdsWitnessType($member)) {
$witnessType = $member->getWitnessType();
if ($witnessType !== null) {
$witnessTypes[] = $witnessType;
Expand All @@ -364,7 +370,7 @@ public static function witnessAbsorbedTypes(MixedType $union, Type ...$types): M
$witnessType = $witnessTypes === [] ? null : TypeTraverser::map(
self::union(...$witnessTypes),
static function (Type $type, callable $traverse): Type {
if (get_class($type) === MixedType::class && $type->getWitnessType() !== null) {
if ($type instanceof MixedType && self::holdsWitnessType($type) && $type->getWitnessType() !== null) {
return $type->changeWitnessType(null);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/error-type-witness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ErrorTypeWitness;

use function PHPStan\Testing\assertType;

function merged(): void
{
$a = undefinedFunction();
if (rand(0, 1)) {
$a = 1;
}

assertType('*ERROR*+1', $a);
echo strlen($a);

if (!is_int($a)) {
assertType('*ERROR*', $a);
}
}

function ternary(bool $c): void
{
$b = $c ? undefinedFunction() : [1];
assertType('*ERROR*+array{1}', $b);
echo strlen($b);

$d = $c ? undefinedFunction() : 'foo';
assertType("*ERROR*+'foo'", $d);
echo strlen($d);
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3958,6 +3958,21 @@ public function testBug11281(): void
]);
}

public function testErrorTypeWitness(): void
{
$this->checkMixedWitnessTypes = true;
$this->analyse([__DIR__ . '/../../Analyser/nsrt/error-type-witness.php'], [
[
'Parameter #1 $string of function strlen expects string, int given.',
15,
],
[
'Parameter #1 $string of function strlen expects string, array<int, int> given.',
26,
],
]);
}

public function testRecursiveTypes(): void
{
$this->analyse([__DIR__ . '/data/recursive-types.php'], [
Expand Down
Loading
Loading