Skip to content

Commit

Permalink
Add extensions for yii request object
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Mar 8, 2018
1 parent 7d5b192 commit 547581c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## What does it do?

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

## Installation

Expand Down
19 changes: 10 additions & 9 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
services:
-
class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: Proget\PHPStan\Yii2\Reflection\RequestMethodsClassReflectionExtension
tags: [phpstan.broker.methodsClassReflectionExtension]
-
class: Proget\PHPStan\Yii2\Reflection\RequestPropertiesClassReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]

- Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%)
- Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%)
parameters:
ignoreErrors:
- '#Access to an undefined property yii\\db\\ActiveRecord#'
- '#Call to an undefined method yii\\db\\ActiveRecord#'
- '#Call to an undefined method yii\\console\\Request#'
- '#Access to an undefined property yii\\console\\Request#'
- '#Call to an undefined method yii\\console\\Response#'
- '#Access to an undefined property yii\\console\\Response#'
- '#Method yii\\db\\ActiveQuery\:\:with\(\) invoked with#'
45 changes: 45 additions & 0 deletions src/Reflection/RequestMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);


namespace Proget\PHPStan\Yii2\Reflection;


use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;

final class RequestMethodsClassReflectionExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
{
/**
* @var Broker
*/
private $broker;

public function setBroker(Broker $broker)
{
$this->broker = $broker;
}

public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if($classReflection->getName()!=='yii\console\Request') {
return false;
}

$webRequest = $this->broker->getClass('yii\web\Request');

return $webRequest->hasMethod($methodName);
}

public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
$webRequest = $this->broker->getClass('yii\web\Request');

return $webRequest->getNativeMethod($methodName);
}

}
54 changes: 54 additions & 0 deletions src/Reflection/RequestPropertiesClassReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);


namespace Proget\PHPStan\Yii2\Reflection;


use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;

final class RequestPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension, BrokerAwareExtension
{
/**
* @var Broker
*/
private $broker;

public function setBroker(Broker $broker)
{
$this->broker = $broker;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if($classReflection->getName()!=='yii\console\Request') {
return false;
}

$webRequest = $this->broker->getClass('yii\web\Request');

return $webRequest->hasProperty($propertyName);
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$webRequest = $this->broker->getClass('yii\web\Request');

$printer = new Standard();

return $webRequest->getProperty($propertyName, new Scope(
$this->broker,
$printer,
new TypeSpecifier($printer),
(string) $classReflection->getFileName()
));
}

}

0 comments on commit 547581c

Please sign in to comment.