-
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.
Showing
6 changed files
with
123 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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ARiddlestone\PHPStanCakePHP2; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertiesClassReflectionExtension; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
|
||
final class ComponentComponentsExtension implements | ||
PropertiesClassReflectionExtension | ||
{ | ||
/** | ||
* @var ReflectionProvider | ||
*/ | ||
private $reflectionProvider; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function hasProperty( | ||
ClassReflection $classReflection, | ||
string $propertyName | ||
): bool { | ||
if (! $classReflection->is('Component')) { | ||
return false; | ||
} | ||
|
||
$className = $this->getClassName($propertyName); | ||
|
||
if (! $this->reflectionProvider->hasClass($className)) { | ||
return false; | ||
} | ||
|
||
if ( | ||
! $this->reflectionProvider | ||
->getClass($className) | ||
->is('Component') | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getProperty( | ||
ClassReflection $classReflection, | ||
string $propertyName | ||
): PropertyReflection { | ||
return new PublicReadOnlyPropertyReflection( | ||
$this->getClassName($propertyName), | ||
$classReflection | ||
); | ||
} | ||
|
||
private function getClassName(string $propertyName): string | ||
{ | ||
return $propertyName . 'Component'; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace ARiddlestone\PHPStanCakePHP2\Test; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class ComponentExtensionsTest extends TypeInferenceTestCase | ||
{ | ||
/** | ||
* @return mixed[] | ||
*/ | ||
public function dataFileAsserts(): iterable | ||
{ | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/existing_component_component.php'); | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/invalid_component_property.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,3 @@ | ||
<?php | ||
|
||
class SecondComponent extends Component {} |
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 BasicComponent $parentComponent */ | ||
$childComponent = $parentComponent->Second; | ||
|
||
assertType('SecondComponent', $childComponent); |
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 BasicComponent $component */ | ||
$property = $component->stdClass; | ||
|
||
assertType('*ERROR*', $property); |