Skip to content

Commit 9f12f0f

Browse files
committed
Fixed level 9 issues
1 parent 28179f2 commit 9f12f0f

File tree

8 files changed

+225
-5
lines changed

8 files changed

+225
-5
lines changed

src/Type/StrictMixedType.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,31 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
3232

3333
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
3434
{
35-
return TrinaryLogic::createFromBoolean(
36-
$acceptingType instanceof MixedType && !$acceptingType instanceof TemplateMixedType,
37-
);
35+
if ($acceptingType instanceof self) {
36+
return TrinaryLogic::createYes();
37+
}
38+
if ($acceptingType instanceof MixedType && !$acceptingType instanceof TemplateMixedType) {
39+
return TrinaryLogic::createYes();
40+
}
41+
42+
return TrinaryLogic::createMaybe();
3843
}
3944

4045
public function isSuperTypeOf(Type $type): TrinaryLogic
4146
{
42-
return TrinaryLogic::createFromBoolean($type instanceof self);
47+
return TrinaryLogic::createYes();
4348
}
4449

4550
public function isSubTypeOf(Type $otherType): TrinaryLogic
4651
{
47-
return TrinaryLogic::createFromBoolean($otherType instanceof self);
52+
if ($otherType instanceof self) {
53+
return TrinaryLogic::createYes();
54+
}
55+
if ($otherType instanceof MixedType && !$otherType instanceof TemplateMixedType) {
56+
return TrinaryLogic::createYes();
57+
}
58+
59+
return TrinaryLogic::createMaybe();
4860
}
4961

5062
public function equals(Type $type): bool

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,4 +2332,40 @@ public function testReadOnlyPropertyPassedByReference(): void
23322332
]);
23332333
}
23342334

2335+
public function testBug6055(): void
2336+
{
2337+
$this->checkThisOnly = false;
2338+
$this->checkNullables = true;
2339+
$this->checkUnionTypes = true;
2340+
$this->checkExplicitMixed = true;
2341+
$this->analyse([__DIR__ . '/data/bug-6055.php'], []);
2342+
}
2343+
2344+
public function testBug6081(): void
2345+
{
2346+
$this->checkThisOnly = false;
2347+
$this->checkNullables = true;
2348+
$this->checkUnionTypes = true;
2349+
$this->checkExplicitMixed = true;
2350+
$this->analyse([__DIR__ . '/data/bug-6081.php'], []);
2351+
}
2352+
2353+
public function testBug6236(): void
2354+
{
2355+
$this->checkThisOnly = false;
2356+
$this->checkNullables = true;
2357+
$this->checkUnionTypes = true;
2358+
$this->checkExplicitMixed = true;
2359+
$this->analyse([__DIR__ . '/data/bug-6236.php'], []);
2360+
}
2361+
2362+
public function testBug6118(): void
2363+
{
2364+
$this->checkThisOnly = false;
2365+
$this->checkNullables = true;
2366+
$this->checkUnionTypes = true;
2367+
$this->checkExplicitMixed = true;
2368+
$this->analyse([__DIR__ . '/data/bug-6118.php'], []);
2369+
}
2370+
23352371
}

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,10 @@ public function testBug4165(): void
579579
$this->analyse([__DIR__ . '/data/bug-4165.php'], []);
580580
}
581581

582+
public function testBug6053(): void
583+
{
584+
$this->checkExplicitMixed = true;
585+
$this->analyse([__DIR__ . '/data/bug-6053.php'], []);
586+
}
587+
582588
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug6053;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param ?mixed $items
9+
*
10+
* @return ?array<mixed>
11+
*/
12+
public function processItems($items): ?array
13+
{
14+
if ($items === null || !is_array($items)) {
15+
return null;
16+
}
17+
18+
if ($items === []) {
19+
return [];
20+
}
21+
22+
$result = [];
23+
foreach ($items as $item) {
24+
$result[] = 'something';
25+
}
26+
27+
return $result;
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug6055;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(): void
8+
{
9+
$this->check(null);
10+
11+
$this->check(array_merge(
12+
['key1' => true],
13+
['key2' => 'value']
14+
));
15+
}
16+
17+
/**
18+
* @param ?array<mixed> $items
19+
*/
20+
private function check(?array $items): void
21+
{
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bug6081;
4+
5+
class HelloWorld
6+
{
7+
/** @param mixed[]|string $thing */
8+
public function something($thing) : void{}
9+
10+
/** @param int[] $array */
11+
public function sayHello(array $array): void
12+
{
13+
$this->something($array);
14+
if(count($array) !== 0){
15+
$this->something($array);
16+
}
17+
}
18+
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Bug6118;
4+
5+
6+
/**
7+
* @template-covariant T of mixed
8+
*/
9+
class Element {
10+
/** @var T */
11+
public mixed $value;
12+
13+
/**
14+
* @param Element<mixed> $element
15+
*/
16+
function getValue(Element $element) : void {
17+
}
18+
19+
/**
20+
* @param Element<int> $element
21+
*/
22+
function takesValue(Element $element) : void {
23+
$this->getValue($element);
24+
}
25+
26+
27+
/**
28+
* @param Element<int|null> $element
29+
*/
30+
function getValue2(Element $element) : void {
31+
}
32+
33+
/**
34+
* @param Element<int> $element
35+
*/
36+
function takesValue2(Element $element) : void {
37+
getValue2($element);
38+
}
39+
}
40+
41+
/**
42+
* @template-covariant T of string|int|array<mixed>
43+
*/
44+
interface Example {
45+
/**
46+
* @return T|null
47+
*/
48+
public function normalize(): string|int|array|null;
49+
}
50+
51+
/**
52+
* @implements Example<string|int|array<mixed>>
53+
*/
54+
class Foo implements Example {
55+
public function normalize(): int
56+
{
57+
return 0;
58+
}
59+
/**
60+
* @param Example<int|string|array<mixed>> $example
61+
*/
62+
function foo(Example $example): void {
63+
}
64+
65+
function bar(): void {
66+
$this->foo(new Foo());
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bug6236;
4+
5+
6+
class HelloWorld
7+
{
8+
/**
9+
* @param \Traversable<mixed, \DateTime> $t
10+
*/
11+
public static function sayHello(\Traversable $t): void
12+
{
13+
}
14+
15+
/**
16+
* @param \SplObjectStorage<\DateTime, \DateTime> $foo
17+
*/
18+
public function doFoo($foo)
19+
{
20+
$this->sayHello(new \ArrayIterator([new \DateTime()]));
21+
22+
$this->sayHello(new \ArrayIterator(['a' => new \DateTime()]));
23+
24+
$this->sayHello($foo);
25+
}
26+
}
27+

0 commit comments

Comments
 (0)