From 1e597733e9afd5ed97d198e82e0671a024e4510f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 21 Sep 2022 21:24:07 +0200 Subject: [PATCH] support rex_article_slice->getValue() type inference --- config/phpstan-dba.neon | 4 ++ ...liceGetValueDynamicReturnTypeExtension.php | 55 +++++++++++++++++++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 4 files changed, 61 insertions(+) create mode 100644 lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php diff --git a/config/phpstan-dba.neon b/config/phpstan-dba.neon index dbbd37631..0e9cfcb08 100644 --- a/config/phpstan-dba.neon +++ b/config/phpstan-dba.neon @@ -59,4 +59,8 @@ services: class: redaxo\phpstan\RexCategoryGetValueDynamicReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension + - + class: redaxo\phpstan\RexArticleSliceGetValueDynamicReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php b/lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php new file mode 100644 index 000000000..246dcc38e --- /dev/null +++ b/lib/RexArticleSliceGetValueDynamicReturnTypeExtension.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_slice', 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 c9b56a3a3..16b35f561 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -78,6 +78,7 @@ '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\\RexArticleSliceGetValueDynamicReturnTypeExtension' => $baseDir . '/lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexCategoryGetValueDynamicReturnTypeExtension' => $baseDir . '/lib/RexCategoryGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexClassDynamicReturnTypeExtension' => $baseDir . '/lib/RexClassDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexFunctionsDynamicReturnTypeExtension' => $baseDir . '/lib/RexFunctionsDynamicReturnTypeExtension.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 3266579a5..c94ea5d59 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -250,6 +250,7 @@ class ComposerStaticInit9cf8af24a7a084f114b4553be2a1ff9f '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\\RexArticleSliceGetValueDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexCategoryGetValueDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexCategoryGetValueDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexClassDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexClassDynamicReturnTypeExtension.php', 'redaxo\\phpstan\\RexFunctionsDynamicReturnTypeExtension' => __DIR__ . '/../..' . '/lib/RexFunctionsDynamicReturnTypeExtension.php',