Skip to content

Commit 547581c

Browse files
committed
Add extensions for yii request object
1 parent 7d5b192 commit 547581c

File tree

4 files changed

+111
-10
lines changed

4 files changed

+111
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## What does it do?
44

55
* Provides correct return type for `Yii::$container->get('service_id')` method,
6-
* Ignore common problems with active record and request/response objects.
6+
* Provides correct methods and properties for `Yii::$app->request`
7+
* Ignore common problems with response objects (to be removed).
78

89
## Installation
910

extension.neon

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
services:
2-
-
3-
class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension
4-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
2+
-
3+
class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension
4+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
5+
-
6+
class: Proget\PHPStan\Yii2\Reflection\RequestMethodsClassReflectionExtension
7+
tags: [phpstan.broker.methodsClassReflectionExtension]
8+
-
9+
class: Proget\PHPStan\Yii2\Reflection\RequestPropertiesClassReflectionExtension
10+
tags: [phpstan.broker.propertiesClassReflectionExtension]
511

6-
- Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%)
12+
- Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%)
713
parameters:
814
ignoreErrors:
9-
- '#Access to an undefined property yii\\db\\ActiveRecord#'
10-
- '#Call to an undefined method yii\\db\\ActiveRecord#'
11-
- '#Call to an undefined method yii\\console\\Request#'
12-
- '#Access to an undefined property yii\\console\\Request#'
1315
- '#Call to an undefined method yii\\console\\Response#'
1416
- '#Access to an undefined property yii\\console\\Response#'
15-
- '#Method yii\\db\\ActiveQuery\:\:with\(\) invoked with#'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
5+
namespace Proget\PHPStan\Yii2\Reflection;
6+
7+
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Broker\Broker;
10+
use PHPStan\Reflection\BrokerAwareExtension;
11+
use PHPStan\Reflection\ClassReflection;
12+
use PHPStan\Reflection\MethodReflection;
13+
use PHPStan\Reflection\MethodsClassReflectionExtension;
14+
15+
final class RequestMethodsClassReflectionExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
16+
{
17+
/**
18+
* @var Broker
19+
*/
20+
private $broker;
21+
22+
public function setBroker(Broker $broker)
23+
{
24+
$this->broker = $broker;
25+
}
26+
27+
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
28+
{
29+
if($classReflection->getName()!=='yii\console\Request') {
30+
return false;
31+
}
32+
33+
$webRequest = $this->broker->getClass('yii\web\Request');
34+
35+
return $webRequest->hasMethod($methodName);
36+
}
37+
38+
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
39+
{
40+
$webRequest = $this->broker->getClass('yii\web\Request');
41+
42+
return $webRequest->getNativeMethod($methodName);
43+
}
44+
45+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
5+
namespace Proget\PHPStan\Yii2\Reflection;
6+
7+
8+
use PhpParser\PrettyPrinter\Standard;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Analyser\TypeSpecifier;
11+
use PHPStan\Broker\Broker;
12+
use PHPStan\Reflection\BrokerAwareExtension;
13+
use PHPStan\Reflection\ClassReflection;
14+
use PHPStan\Reflection\PropertiesClassReflectionExtension;
15+
use PHPStan\Reflection\PropertyReflection;
16+
17+
final class RequestPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension, BrokerAwareExtension
18+
{
19+
/**
20+
* @var Broker
21+
*/
22+
private $broker;
23+
24+
public function setBroker(Broker $broker)
25+
{
26+
$this->broker = $broker;
27+
}
28+
29+
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
30+
{
31+
if($classReflection->getName()!=='yii\console\Request') {
32+
return false;
33+
}
34+
35+
$webRequest = $this->broker->getClass('yii\web\Request');
36+
37+
return $webRequest->hasProperty($propertyName);
38+
}
39+
40+
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
41+
{
42+
$webRequest = $this->broker->getClass('yii\web\Request');
43+
44+
$printer = new Standard();
45+
46+
return $webRequest->getProperty($propertyName, new Scope(
47+
$this->broker,
48+
$printer,
49+
new TypeSpecifier($printer),
50+
(string) $classReflection->getFileName()
51+
));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)