Skip to content

Commit c3ad834

Browse files
authored
Added component components (#6)
Closes #2
1 parent 774739e commit c3ad834

6 files changed

Lines changed: 123 additions & 0 deletions

File tree

extension.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ parameters:
88
stubFiles:
99
- stubs/Utility.php
1010
services:
11+
- class: ARiddlestone\PHPStanCakePHP2\ComponentComponentsExtension
12+
tags:
13+
- phpstan.broker.propertiesClassReflectionExtension
1114
- class: ARiddlestone\PHPStanCakePHP2\ControllerComponentsExtension
1215
tags:
1316
- phpstan.broker.propertiesClassReflectionExtension
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ARiddlestone\PHPStanCakePHP2;
6+
7+
use PHPStan\Reflection\ClassReflection;
8+
use PHPStan\Reflection\PropertiesClassReflectionExtension;
9+
use PHPStan\Reflection\PropertyReflection;
10+
use PHPStan\Reflection\ReflectionProvider;
11+
12+
final class ComponentComponentsExtension implements
13+
PropertiesClassReflectionExtension
14+
{
15+
/**
16+
* @var ReflectionProvider
17+
*/
18+
private $reflectionProvider;
19+
20+
public function __construct(ReflectionProvider $reflectionProvider)
21+
{
22+
$this->reflectionProvider = $reflectionProvider;
23+
}
24+
25+
public function hasProperty(
26+
ClassReflection $classReflection,
27+
string $propertyName
28+
): bool {
29+
if (! $classReflection->is('Component')) {
30+
return false;
31+
}
32+
33+
$className = $this->getClassName($propertyName);
34+
35+
if (! $this->reflectionProvider->hasClass($className)) {
36+
return false;
37+
}
38+
39+
if (
40+
! $this->reflectionProvider
41+
->getClass($className)
42+
->is('Component')
43+
) {
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
public function getProperty(
51+
ClassReflection $classReflection,
52+
string $propertyName
53+
): PropertyReflection {
54+
return new PublicReadOnlyPropertyReflection(
55+
$this->getClassName($propertyName),
56+
$classReflection
57+
);
58+
}
59+
60+
private function getClassName(string $propertyName): string
61+
{
62+
return $propertyName . 'Component';
63+
}
64+
}

tests/ComponentExtensionsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ARiddlestone\PHPStanCakePHP2\Test;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class ComponentExtensionsTest extends TypeInferenceTestCase
8+
{
9+
/**
10+
* @return mixed[]
11+
*/
12+
public function dataFileAsserts(): iterable
13+
{
14+
yield from $this->gatherAssertTypes(__DIR__ . '/data/existing_component_component.php');
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/invalid_component_property.php');
16+
}
17+
18+
/**
19+
* @dataProvider dataFileAsserts
20+
* @param mixed $args
21+
*/
22+
public function testControllerExtensions(string $assertType, string $file, ...$args): void
23+
{
24+
$this->assertFileAsserts($assertType, $file, ...$args);
25+
}
26+
27+
public static function getAdditionalConfigFiles(): array
28+
{
29+
return [
30+
__DIR__ . '/data/phpstan.neon',
31+
];
32+
}
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
class SecondComponent extends Component {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @var BasicComponent $parentComponent */
8+
$childComponent = $parentComponent->Second;
9+
10+
assertType('SecondComponent', $childComponent);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @var BasicComponent $component */
8+
$property = $component->stdClass;
9+
10+
assertType('*ERROR*', $property);

0 commit comments

Comments
 (0)