-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LaravelContainerMakeTypeExtension to fill Container->make() retur…
…n type (#6)
- Loading branch information
1 parent
ff3e2d5
commit 6cba521
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/TypeExtension/MethodCall/LaravelContainerMakeTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanExtensions\TypeExtension\MethodCall; | ||
|
||
use Illuminate\Container\Container; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use Symplify\PHPStanExtensions\TypeResolver\ClassConstFetchReturnTypeResolver; | ||
|
||
/** | ||
* Helps to check Container->make() return type | ||
*/ | ||
final class LaravelContainerMakeTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function __construct( | ||
private readonly ClassConstFetchReturnTypeResolver $classConstFetchReturnTypeResolver | ||
) { | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return Container::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return in_array($methodReflection->getName(), ['make', 'get'], true); | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
return $this->classConstFetchReturnTypeResolver->resolve($methodReflection, $methodCall); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...sion/MethodCall/LaravelContainerMakeTypeExtension/ContainerGetReturnTypeExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanExtensions\Tests\TypeExtension\MethodCall\LaravelContainerMakeTypeExtension; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
final class ContainerGetReturnTypeExtensionTest extends TypeInferenceTestCase | ||
{ | ||
public function testAsserts(): void | ||
{ | ||
foreach ($this->gatherAssertTypes(__DIR__ . '/data/fixture.php') as [$assertType, $file, $expectedType, $actualType, $line]) { | ||
$this->assertFileAsserts($assertType, $file, ...[$expectedType, $actualType, $line]); | ||
} | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [__DIR__ . '/config/config.neon']; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/TypeExtension/MethodCall/LaravelContainerMakeTypeExtension/Source/ExternalService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanExtensions\Tests\TypeExtension\MethodCall\LaravelContainerMakeTypeExtension\Source; | ||
|
||
final class ExternalService | ||
{ | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/TypeExtension/MethodCall/LaravelContainerMakeTypeExtension/config/config.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
- Symplify\PHPStanExtensions\TypeResolver\ClassConstFetchReturnTypeResolver | ||
|
||
- | ||
class: Symplify\PHPStanExtensions\TypeExtension\MethodCall\LaravelContainerMakeTypeExtension | ||
tags: [phpstan.broker.dynamicMethodReturnTypeExtension] |
19 changes: 19 additions & 0 deletions
19
tests/TypeExtension/MethodCall/LaravelContainerMakeTypeExtension/data/fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Container\Container; | ||
use Symplify\PHPStanExtensions\Tests\TypeExtension\MethodCall\ContainerGetReturnTypeExtension\Source\ExternalService; | ||
use function PHPStan\Testing\assertType; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(Container $container): void | ||
{ | ||
$services = $container->make(ExternalService::class); | ||
assertType(ExternalService::class, $services); | ||
|
||
$services = $container->get(ExternalService::class); | ||
assertType(ExternalService::class, $services); | ||
} | ||
} |