From 547581cf83e2526fab1074792b11362e098b6772 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Fri, 9 Mar 2018 00:16:30 +0100 Subject: [PATCH] Add extensions for yii request object --- README.md | 3 +- extension.neon | 19 +++---- ...RequestMethodsClassReflectionExtension.php | 45 ++++++++++++++++ ...uestPropertiesClassReflectionExtension.php | 54 +++++++++++++++++++ 4 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 src/Reflection/RequestMethodsClassReflectionExtension.php create mode 100644 src/Reflection/RequestPropertiesClassReflectionExtension.php diff --git a/README.md b/README.md index 26ad41c..4e1a632 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/extension.neon b/extension.neon index 6f8b161..98d90c6 100644 --- a/extension.neon +++ b/extension.neon @@ -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#' diff --git a/src/Reflection/RequestMethodsClassReflectionExtension.php b/src/Reflection/RequestMethodsClassReflectionExtension.php new file mode 100644 index 0000000..101d722 --- /dev/null +++ b/src/Reflection/RequestMethodsClassReflectionExtension.php @@ -0,0 +1,45 @@ +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); + } + +} \ No newline at end of file diff --git a/src/Reflection/RequestPropertiesClassReflectionExtension.php b/src/Reflection/RequestPropertiesClassReflectionExtension.php new file mode 100644 index 0000000..1c737c7 --- /dev/null +++ b/src/Reflection/RequestPropertiesClassReflectionExtension.php @@ -0,0 +1,54 @@ +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() + )); + } + +} \ No newline at end of file