Skip to content

Commit a9043e0

Browse files
committed
Test mixed type support
1 parent f1af853 commit a9043e0

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

build/phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ parameters:
1818
- %rootDir%/tests/notAutoloaded/*
1919
- %rootDir%/tests/PHPStan/Generics/functions.php
2020
- %rootDir%/tests/PHPStan/Reflection/UnionTypesTest.php
21+
- %rootDir%/tests/PHPStan/Reflection/MixedTypeTest.php
2122
ignoreErrors:
2223
- '#^Dynamic call to static method PHPUnit\\Framework\\\S+\(\)\.$#'
2324
- '#should be contravariant with parameter \$node \(PhpParser\\Node\) of method PHPStan\\Rules\\Rule<PhpParser\\Node>::processNode\(\)$#'

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10000,6 +10000,15 @@ public function dataNativeUnionTypes(): array
1000010000
return $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/unionTypes.php');
1000110001
}
1000210002

10003+
public function dataNativeMixedType(): array
10004+
{
10005+
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
10006+
return [];
10007+
}
10008+
10009+
return $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/mixedType.php');
10010+
}
10011+
1000310012
/**
1000410013
* @dataProvider dataBug2574
1000510014
* @dataProvider dataBug2577
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
use NativeMixedType\Foo;
6+
use PhpParser\Node\Name;
7+
use PHPStan\Testing\TestCase;
8+
use PHPStan\Type\MixedType;
9+
10+
class MixedTypeTest extends TestCase
11+
{
12+
13+
public function testMixedType(): void
14+
{
15+
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
16+
$this->markTestSkipped('Test requires PHP 8.0');
17+
}
18+
19+
$reflectionProvider = $this->createBroker();
20+
$class = $reflectionProvider->getClass(Foo::class);
21+
$propertyType = $class->getNativeProperty('fooProp')->getNativeType();
22+
$this->assertInstanceOf(MixedType::class, $propertyType);
23+
$this->assertTrue($propertyType->isExplicitMixed());
24+
25+
$method = $class->getNativeMethod('doFoo');
26+
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
27+
$methodReturnType = $methodVariant->getReturnType();
28+
$this->assertInstanceOf(MixedType::class, $methodReturnType);
29+
$this->assertTrue($methodReturnType->isExplicitMixed());
30+
31+
$methodParameterType = $methodVariant->getParameters()[0]->getType();
32+
$this->assertInstanceOf(MixedType::class, $methodParameterType);
33+
$this->assertTrue($methodParameterType->isExplicitMixed());
34+
35+
$function = $reflectionProvider->getFunction(new Name('NativeMixedType\doFoo'), null);
36+
$functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants());
37+
$functionReturnType = $functionVariant->getReturnType();
38+
$this->assertInstanceOf(MixedType::class, $functionReturnType);
39+
$this->assertTrue($functionReturnType->isExplicitMixed());
40+
41+
$functionParameterType = $functionVariant->getParameters()[0]->getType();
42+
$this->assertInstanceOf(MixedType::class, $functionParameterType);
43+
$this->assertTrue($functionParameterType->isExplicitMixed());
44+
}
45+
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php // lint >= 8.0
2+
3+
namespace NativeMixedType;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
public mixed $fooProp;
11+
12+
public function doFoo(mixed $foo): mixed
13+
{
14+
assertType('mixed', $foo);
15+
assertType('mixed', $this->fooProp);
16+
}
17+
18+
}
19+
20+
class Bar
21+
{
22+
23+
}
24+
25+
function doFoo(mixed $foo): mixed
26+
{
27+
assertType('mixed', $foo);
28+
}
29+
30+
function (Foo $foo): void {
31+
assertType('mixed', $foo->fooProp);
32+
assertType('mixed', $foo->doFoo(1));
33+
assertType('mixed', doFoo(1));
34+
};
35+
36+
function (): void {
37+
$f = function (mixed $foo): mixed {
38+
assertType('mixed', $foo);
39+
};
40+
41+
assertType('mixed', $f(1));
42+
};

0 commit comments

Comments
 (0)