Skip to content

Commit 1a32570

Browse files
committed
slip
1 parent 7832a44 commit 1a32570

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+153
-57
lines changed

.github/workflows/code_analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Setup PHP
1515
uses: shivammathur/setup-php@v2
1616
with:
17-
php-version: 8.0
17+
php-version: 8.1
1818

1919
- name: Validate composer
2020
run: composer validate

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
composer.lock
77

88
# tests
9-
.phpunit.result
9+
.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"classmap": [
2727
"stubs",
28-
"tests/Feature/classes"
28+
"tests/Source"
2929
]
3030
},
3131
"extra": {

config/extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ services:
4343
tags:
4444
- phpstan.broker.methodsClassReflectionExtension
4545

46+
-
47+
class: PHPStanCakePHP2\ReturnTypeExtension\LoadComponentOnFlyMethodReturnTypeExtension
48+
tags:
49+
- phpstan.broker.dynamicMethodReturnTypeExtension
50+
4651
-
4752
class: PHPStanCakePHP2\Service\SchemaService
4853
arguments:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPStanCakePHP2\ReturnTypeExtension;
6+
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Scalar\String_;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Reflection\ReflectionProvider;
12+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
13+
use PHPStan\Type\ObjectType;
14+
use PHPStan\Type\Type;
15+
16+
final class LoadComponentOnFlyMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
17+
{
18+
private ReflectionProvider $reflectionProvider;
19+
20+
public function __construct(ReflectionProvider $reflectionProvider)
21+
{
22+
$this->reflectionProvider = $reflectionProvider;
23+
}
24+
25+
public function getClass(): string
26+
{
27+
return 'ComponentCollection';
28+
}
29+
30+
public function isMethodSupported(MethodReflection $methodReflection): bool
31+
{
32+
return $methodReflection->getName() === 'load';
33+
}
34+
35+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
36+
{
37+
$arg = $methodCall->getArgs()[0]->value;
38+
39+
if (!$arg instanceof String_) {
40+
return null;
41+
}
42+
43+
$componentName = $arg->value . 'Component';
44+
45+
if (!$this->reflectionProvider->hasClass($componentName)) {
46+
return null;
47+
}
48+
49+
if (!$this->reflectionProvider->getClass($componentName)->is('Component')) {
50+
return null;
51+
}
52+
53+
return new ObjectType($componentName);
54+
}
55+
}

tests/ClassPropertyExtension/ClassComponentPropertyExtension/ClassComponentPropertyExtensionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public static function dataFileAsserts(): Iterator
2424
{
2525
yield from self::gatherAssertTypes(__DIR__ . '/Fixture/existing_component_component.php');
2626
yield from self::gatherAssertTypes(__DIR__ . '/Fixture/invalid_component_property.php');
27+
28+
yield from self::gatherAssertTypes(__DIR__ . '/Fixture/existing_controller_component.php');
29+
yield from self::gatherAssertTypes(__DIR__ . '/Fixture/existing_controller_component_from_parent_controller.php');
30+
yield from self::gatherAssertTypes(__DIR__ . '/Fixture/existing_controller_component_with_same_method_name_as_model.php');
2731
}
2832

2933
public static function getAdditionalConfigFiles(): array

tests/ClassPropertyExtension/ClassComponentPropertyExtension/Fixture/existing_component_component.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types = 1);
44

5+
use PHPStanCakePHP2\Tests\Source\Controller\Component\BasicComponent;
56
use function PHPStan\Testing\assertType;
67

78
/** @var BasicComponent $parentComponent */

tests/Feature/data/existing_controller_component.php renamed to tests/ClassPropertyExtension/ClassComponentPropertyExtension/Fixture/existing_controller_component.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types = 1);
44

5+
use PHPStanCakePHP2\Tests\Source\Controller\BasicController;
56
use function PHPStan\Testing\assertType;
67

78
/** @var BasicController $controller */

tests/Feature/data/existing_controller_component_from_parent_controller.php renamed to tests/ClassPropertyExtension/ClassComponentPropertyExtension/Fixture/existing_controller_component_from_parent_controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types = 1);
44

5+
use PHPStanCakePHP2\Tests\Source\Controller\SameAsModelController;
56
use function PHPStan\Testing\assertType;
67

78
/** @var SameAsModelController $controller */

0 commit comments

Comments
 (0)