From 840b10b4f60aef32e8143c2eb13526157862e579 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 21 Sep 2022 21:16:44 +0200 Subject: [PATCH] support rex_article->getValue() type inference (#131) --- config/phpstan-dba.neon | 5 ++ ...icleGetValueDynamicReturnTypeExtension.php | 55 +++++++++++++++++++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 4 files changed, 62 insertions(+) create mode 100644 lib/RexArticleGetValueDynamicReturnTypeExtension.php diff --git a/config/phpstan-dba.neon b/config/phpstan-dba.neon index 2af6fdd54..e74ce9768 100644 --- a/config/phpstan-dba.neon +++ b/config/phpstan-dba.neon @@ -51,3 +51,8 @@ services: class: redaxo\phpstan\RexMediaGetValueDynamicReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension + - + class: redaxo\phpstan\RexArticleGetValueDynamicReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension + diff --git a/lib/RexArticleGetValueDynamicReturnTypeExtension.php b/lib/RexArticleGetValueDynamicReturnTypeExtension.php new file mode 100644 index 000000000..1b75fbb37 --- /dev/null +++ b/lib/RexArticleGetValueDynamicReturnTypeExtension.php @@ -0,0 +1,55 @@ +getName()), ['getvalue'], true); + } + + public function getTypeFromMethodCall( + MethodReflection $methodReflection, + MethodCall $methodCall, + Scope $scope + ): ?Type { + $args = $methodCall->getArgs(); + if (1 < count($args)) { + return null; + } + + $nameType = $scope->getType($args[0]->value); + if (!$nameType instanceof ConstantStringType) { + return null; + } + + $queryReflection = new QueryReflection(); + $resultType = $queryReflection->getResultType('SELECT * FROM rex_article', QueryReflector::FETCH_TYPE_ASSOC); + if ($resultType instanceof ConstantArrayType && $resultType->hasOffsetValueType($nameType)->yes()) { + return $resultType->getOffsetValueType($nameType); + } + + return null; + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index ac4158f90..939baf29c 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -77,6 +77,7 @@ 'Stringable' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Stringable.php', 'UnhandledMatchError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php', 'ValueError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/ValueError.php', + 'redaxo\\phpstan\\RexArticleGetValueDynamicReturnTypeExtension' => $baseDir . '/lib/RexArticleGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexClassDynamicReturnTypeExtension' => $baseDir . '/lib/RexClassDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexFunctionsDynamicReturnTypeExtension' => $baseDir . '/lib/RexFunctionsDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexMediaGetValueDynamicReturnTypeExtension' => $baseDir . '/lib/RexMediaGetValueDynamicReturnTypeExtension.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index fd203844e..af4b9b455 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -249,6 +249,7 @@ class ComposerStaticInit9cf8af24a7a084f114b4553be2a1ff9f 'Stringable' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/Stringable.php', 'UnhandledMatchError' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php', 'ValueError' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/ValueError.php', + 'redaxo\\phpstan\\RexArticleGetValueDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexArticleGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexClassDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexClassDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexFunctionsDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexFunctionsDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexMediaGetValueDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexMediaGetValueDynamicReturnTypeExtension.php',