From d2ec42eb70af44304bc31c9193023299caddbea5 Mon Sep 17 00:00:00 2001 From: Thomas Gnandt Date: Wed, 29 Apr 2020 19:30:19 +0200 Subject: [PATCH 1/3] better type resolution on Yii::createObject --- extension.neon | 8 +- ...DynamicStaticMethodReturnTypeExtension.php | 84 ------------------- stubs/BaseYii.stub | 16 ++++ 3 files changed, 18 insertions(+), 90 deletions(-) delete mode 100644 src/Type/YiiDynamicStaticMethodReturnTypeExtension.php create mode 100644 stubs/BaseYii.stub diff --git a/extension.neon b/extension.neon index ed1dacf..931bc4d 100644 --- a/extension.neon +++ b/extension.neon @@ -1,6 +1,8 @@ parameters: yii2: config_path: null + stubFiles: + - stubs/Yii.stub parametersSchema: yii2: structure([ @@ -35,11 +37,5 @@ services: - class: Proget\PHPStan\Yii2\Type\ActiveRecordDynamicStaticMethodReturnTypeExtension tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension] - - - class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension - tags: [phpstan.broker.dynamicMethodReturnTypeExtension] - - - class: Proget\PHPStan\Yii2\Type\YiiDynamicStaticMethodReturnTypeExtension - tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension] - Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%) diff --git a/src/Type/YiiDynamicStaticMethodReturnTypeExtension.php b/src/Type/YiiDynamicStaticMethodReturnTypeExtension.php deleted file mode 100644 index c1b8b6f..0000000 --- a/src/Type/YiiDynamicStaticMethodReturnTypeExtension.php +++ /dev/null @@ -1,84 +0,0 @@ -getName() === 'createObject'; - } - - public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type - { - $class = null; - if ($methodCall->args[0]->value instanceof String_) { - $class = $methodCall->args[0]->value->value; - } elseif ($methodCall->args[0]->value instanceof ClassConstFetch) { - if ($methodCall->args[0]->value->class instanceof Name) { - $class = $methodCall->args[0]->value->class->toString(); - } elseif ($methodCall->args[0]->value->class instanceof String_) { - $class = $methodCall->args[0]->value->class->value; - } - } elseif ($methodCall->args[0]->value instanceof Array_) { - $class = $this->extractClassFromArray($methodCall->args[0]->value); - } elseif ($methodCall->args[0]->value instanceof Closure) { - $returnType = $methodCall->args[0]->value->getReturnType(); - if ($returnType instanceof Name) { - return new ObjectType($returnType->toString()); - } - - throw new ShouldNotHappenException('Invalid $type callable argument provided to createObject method - return type is required.'); - } - - if (!$class) { - throw new ShouldNotHappenException('Invalid $type argument provided to createObject method.'); - } - - return new ObjectType($class); - } - - private function extractClassFromArray(Array_ $array): string - { - $class = null; - foreach ($array->items as $item) { - if ($item->key instanceof String_ && $item->key->value === 'class') { - if (!$item->value instanceof String_) { - throw new ShouldNotHappenException('Invalid $type array argument provided to createObject method - "class" key must be a string.'); - } - - if ($class) { - throw new ShouldNotHappenException('Invalid $type array argument provided to createObject method - duplicate "class" key.'); - } - - $class = $item->value->value; - } - } - - if (!$class) { - throw new ShouldNotHappenException('Invalid $type array argument provided to createObject method - missing "class" key.'); - } - - return $class; - } -} diff --git a/stubs/BaseYii.stub b/stubs/BaseYii.stub new file mode 100644 index 0000000..956e043 --- /dev/null +++ b/stubs/BaseYii.stub @@ -0,0 +1,16 @@ +|array{class: class-string}|callable(): T $type + * @param array $params + * + * @return T + */ + public static function createObject($type, array $params = []); +} From 44383046df09f8f3576bb914c7c7e0d0c14e07a1 Mon Sep 17 00:00:00 2001 From: Thomas Gnandt Date: Thu, 30 Apr 2020 19:00:09 +0200 Subject: [PATCH 2/3] restore accidentaly deleted extension --- extension.neon | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extension.neon b/extension.neon index 931bc4d..bb22fed 100644 --- a/extension.neon +++ b/extension.neon @@ -37,5 +37,8 @@ services: - class: Proget\PHPStan\Yii2\Type\ActiveRecordDynamicStaticMethodReturnTypeExtension tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension] + - + class: Proget\PHPStan\Yii2\Type\ContainerDynamicMethodReturnTypeExtension + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] - Proget\PHPStan\Yii2\ServiceMap(%yii2.config_path%) From d00dd8dd3e84dc1aa4bbee59e3674b1c666dbf3d Mon Sep 17 00:00:00 2001 From: Thomas Gnandt Date: Thu, 30 Apr 2020 19:10:43 +0200 Subject: [PATCH 3/3] Fix stub filename --- extension.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.neon b/extension.neon index bb22fed..d04e556 100644 --- a/extension.neon +++ b/extension.neon @@ -2,7 +2,7 @@ parameters: yii2: config_path: null stubFiles: - - stubs/Yii.stub + - stubs/BaseYii.stub parametersSchema: yii2: structure([