-
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 support to loading components on the fly (#31)
- Loading branch information
Showing
4 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ARiddlestone\PHPStanCakePHP2; | ||
|
||
use Component; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
class LoadComponentOnFlyMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
private ReflectionProvider $reflectionProvider; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return \ComponentCollection::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'load'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
$arg = $methodCall->getArgs()[0]->value; | ||
|
||
if (!$arg instanceof String_) { | ||
return null; | ||
} | ||
|
||
$componentName = $arg->value . 'Component'; | ||
|
||
if (!$this->reflectionProvider->hasClass($componentName)) { | ||
return null; | ||
} | ||
|
||
if (!$this->reflectionProvider->getClass($componentName)->is(Component::class)) { | ||
return null; | ||
} | ||
|
||
return new ObjectType($componentName); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Feature/LoadComponentOnFlyMethodReturnTypeExtensionTest.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ARiddlestone\PHPStanCakePHP2\Test\Feature; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class LoadComponentOnFlyMethodReturnTypeExtensionTest extends TypeInferenceTestCase | ||
{ | ||
/** | ||
* @return mixed[] | ||
*/ | ||
public function dataFileAsserts(): iterable | ||
{ | ||
yield from self::gatherAssertTypes(__DIR__ . '/data/loading_component_loaded_on_fly.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
* @param mixed $args | ||
*/ | ||
public function testControllerExtensions(string $assertType, string $file, ...$args): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/data/phpstan.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** @var BasicController $controller */ | ||
$component = $controller->Components->load('Basic'); | ||
|
||
assertType('BasicComponent', $component); |