Skip to content

Commit

Permalink
Test mixed type support
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 8, 2020
1 parent f1af853 commit a9043e0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/phpstan.neon
Expand Up @@ -18,6 +18,7 @@ parameters:
- %rootDir%/tests/notAutoloaded/*
- %rootDir%/tests/PHPStan/Generics/functions.php
- %rootDir%/tests/PHPStan/Reflection/UnionTypesTest.php
- %rootDir%/tests/PHPStan/Reflection/MixedTypeTest.php
ignoreErrors:
- '#^Dynamic call to static method PHPUnit\\Framework\\\S+\(\)\.$#'
- '#should be contravariant with parameter \$node \(PhpParser\\Node\) of method PHPStan\\Rules\\Rule<PhpParser\\Node>::processNode\(\)$#'
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -10000,6 +10000,15 @@ public function dataNativeUnionTypes(): array
return $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/unionTypes.php');
}

public function dataNativeMixedType(): array
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
return [];
}

return $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/mixedType.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Reflection/MixedTypeTest.php
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

use NativeMixedType\Foo;
use PhpParser\Node\Name;
use PHPStan\Testing\TestCase;
use PHPStan\Type\MixedType;

class MixedTypeTest extends TestCase
{

public function testMixedType(): void
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$reflectionProvider = $this->createBroker();
$class = $reflectionProvider->getClass(Foo::class);
$propertyType = $class->getNativeProperty('fooProp')->getNativeType();
$this->assertInstanceOf(MixedType::class, $propertyType);
$this->assertTrue($propertyType->isExplicitMixed());

$method = $class->getNativeMethod('doFoo');
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants());
$methodReturnType = $methodVariant->getReturnType();
$this->assertInstanceOf(MixedType::class, $methodReturnType);
$this->assertTrue($methodReturnType->isExplicitMixed());

$methodParameterType = $methodVariant->getParameters()[0]->getType();
$this->assertInstanceOf(MixedType::class, $methodParameterType);
$this->assertTrue($methodParameterType->isExplicitMixed());

$function = $reflectionProvider->getFunction(new Name('NativeMixedType\doFoo'), null);
$functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants());
$functionReturnType = $functionVariant->getReturnType();
$this->assertInstanceOf(MixedType::class, $functionReturnType);
$this->assertTrue($functionReturnType->isExplicitMixed());

$functionParameterType = $functionVariant->getParameters()[0]->getType();
$this->assertInstanceOf(MixedType::class, $functionParameterType);
$this->assertTrue($functionParameterType->isExplicitMixed());
}

}
42 changes: 42 additions & 0 deletions tests/PHPStan/Reflection/data/mixedType.php
@@ -0,0 +1,42 @@
<?php // lint >= 8.0

namespace NativeMixedType;

use function PHPStan\Analyser\assertType;

class Foo
{

public mixed $fooProp;

public function doFoo(mixed $foo): mixed
{
assertType('mixed', $foo);
assertType('mixed', $this->fooProp);
}

}

class Bar
{

}

function doFoo(mixed $foo): mixed
{
assertType('mixed', $foo);
}

function (Foo $foo): void {
assertType('mixed', $foo->fooProp);
assertType('mixed', $foo->doFoo(1));
assertType('mixed', doFoo(1));
};

function (): void {
$f = function (mixed $foo): mixed {
assertType('mixed', $foo);
};

assertType('mixed', $f(1));
};

0 comments on commit a9043e0

Please sign in to comment.