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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ parameters:

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
count: 1
count: 2
path: src/Type/Constant/ConstantArrayType.php

-
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use PHPStan\Type\ConstantType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\IntegerRangeType;
Expand Down Expand Up @@ -296,7 +295,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
{
if ($type instanceof MixedType && !$type instanceof TemplateMixedType) {
if ($type instanceof CompoundType && !$type instanceof IntersectionType) {
return $type->isAcceptedWithReasonBy($this, $strictTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,7 @@ public function testBug7211(): void

public function testBug5474(): void
{
$this->analyse([__DIR__ . '/../Comparison/data/bug-5474.php'], [
[
'Parameter #1 $data of function Bug5474\testData expects array{test: int}, *NEVER* given.',
26,
],
]);
$this->analyse([__DIR__ . '/../Comparison/data/bug-5474.php'], []);
Copy link
Contributor Author

@tscni tscni Aug 17, 2024

Choose a reason for hiding this comment

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

This would be the consequence of the change. (See phpstan/phpstan#5474, https://phpstan.org/r/4ba9826f-8be3-4782-97ea-127ba76a9950)

At least to me that seems acceptable, as never is after all the bottom type. And the same issue doesn't appear for other types that already accept never anyway.
This rather seems like a now undesired artifact from before notIdentical.alwaysFalse or similar.

}

public function testBug6261(): void
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function testRule(): void
]);
}

public function testBug11517(): void
{
$this->analyse([__DIR__ . '/data/bug-11517.php'], []);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Generators/data/bug-11517.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug11517;

class HelloWorld
{
/**
* @return iterable<array-key, array{string}>
*/
public function bug(): iterable
{
yield from [];
}

/**
* @return iterable<array-key, object{a: string}>
*/
public function fine(): iterable
{
yield from [];
}

/**
* @return iterable<array-key, string>
*/
public function finetoo(): iterable
{
yield from [];
}
}
61 changes: 61 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
use Closure;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\CallableType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -345,6 +349,63 @@ public function dataAccepts(): iterable
]),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([], []),
new NeverType(),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([new ConstantIntegerType(1)], [new ConstantIntegerType(2)]),
new NeverType(),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([new ConstantStringType('test')], [new MixedType()]),
new IntersectionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new ConstantStringType('test')),
]),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([new ConstantStringType('test')], [new StringType()]),
new IntersectionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetValueType(new ConstantStringType('test'), new StringType()),
]),
TrinaryLogic::createYes(),
];

yield [
new ConstantArrayType([new ConstantStringType('test')], [new MixedType()]),
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetType(new ConstantStringType('test')),
]),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([new ConstantStringType('test')], [new StringType()]),
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new HasOffsetValueType(new ConstantStringType('test'), new StringType()),
]),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([new ConstantStringType('test')], [new MixedType()]),
new IntersectionType([
new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new MixedType())]),
new HasOffsetType(new ConstantStringType('test')),
]),
TrinaryLogic::createMaybe(),
];
}

/**
Expand Down
Loading