Skip to content

Commit a2475df

Browse files
author
Robin Colombier
committed
test: mulitple type specifying extension
1 parent 72e878d commit a2475df

5 files changed

+152
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
-
3+
class: PHPStan\Tests\AssertionClassMethodTypeSpecifyingExtension
4+
arguments:
5+
nullContext: true
6+
tags:
7+
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
8+
-
9+
class: PHPStan\Tests\AssertionClassStaticMethodTypeSpecifyingExtension
10+
arguments:
11+
nullContext: true
12+
tags:
13+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
14+
-
15+
class: PHPStan\Tests\AssertionClassMethodMultipleTypeSpecifyingExtension
16+
tags:
17+
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
18+
-
19+
class: PHPStan\Tests\AssertionClassStaticMethodMultipleTypeSpecifyingExtension
20+
tags:
21+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class MultipleTypeSpecifyingExtensionTypeInferenceTest extends TypeInferenceTestCase
8+
{
9+
10+
public function dataTypeSpecifyingExtensionsTrue(): iterable
11+
{
12+
yield from $this->gatherAssertTypes(__DIR__ . '/data/multiple-type-specifying-extensions-1.php');
13+
}
14+
15+
/**
16+
* @dataProvider dataTypeSpecifyingExtensionsTrue
17+
* @param mixed ...$args
18+
*/
19+
public function testTypeSpecifyingExtensionsTrue(
20+
string $assertType,
21+
string $file,
22+
...$args,
23+
): void
24+
{
25+
$this->assertFileAsserts($assertType, $file, ...$args);
26+
}
27+
28+
public static function getAdditionalConfigFiles(): array
29+
{
30+
return [
31+
__DIR__ . '/MultipleTypeSpecifyingExtension.neon',
32+
];
33+
}
34+
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use function PHPStan\Testing\assertType;
4+
5+
/** @var string|null $foo */
6+
$foo = null;
7+
8+
/** @var int|null $bar */
9+
$bar = null;
10+
11+
(new \PHPStan\Tests\AssertionClass())->assertString($foo);
12+
\PHPStan\Tests\AssertionClass::assertInt($bar);
13+
14+
assertType('non-empty-string', $foo);
15+
assertType('int<1, max>', $bar);
16+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Tests;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifierContext;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
11+
use PHPStan\Type\MethodTypeSpecifyingExtension;
12+
13+
class AssertionClassMethodMultipleTypeSpecifyingExtension implements MethodTypeSpecifyingExtension
14+
{
15+
16+
public function getClass(): string
17+
{
18+
return AssertionClass::class;
19+
}
20+
21+
public function isMethodSupported(
22+
MethodReflection $methodReflection,
23+
MethodCall $node,
24+
TypeSpecifierContext $context,
25+
): bool
26+
{
27+
return $methodReflection->getName() === 'assertString';
28+
}
29+
30+
public function specifyTypes(
31+
MethodReflection $methodReflection,
32+
MethodCall $node,
33+
Scope $scope,
34+
TypeSpecifierContext $context,
35+
): SpecifiedTypes
36+
{
37+
return new SpecifiedTypes(['$foo' => [$node->getArgs()[0]->value, new AccessoryNonEmptyStringType()]]);
38+
}
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Tests;
4+
5+
use PhpParser\Node\Expr\StaticCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifierContext;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\IntegerRangeType;
11+
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
12+
13+
class AssertionClassStaticMethodMultipleTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension
14+
{
15+
16+
public function getClass(): string
17+
{
18+
return AssertionClass::class;
19+
}
20+
21+
public function isStaticMethodSupported(
22+
MethodReflection $staticMethodReflection,
23+
StaticCall $node,
24+
TypeSpecifierContext $context,
25+
): bool
26+
{
27+
return $staticMethodReflection->getName() === 'assertInt';
28+
}
29+
30+
public function specifyTypes(
31+
MethodReflection $staticMethodReflection,
32+
StaticCall $node,
33+
Scope $scope,
34+
TypeSpecifierContext $context,
35+
): SpecifiedTypes
36+
{
37+
return new SpecifiedTypes(['$foo' => [$node->getArgs()[0]->value, IntegerRangeType::createAllGreaterThan(0)]]);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)