Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support rex_article_slice->getValue() type inference #133

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/phpstan-dba.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ services:
class: redaxo\phpstan\RexCategoryGetValueDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: redaxo\phpstan\RexArticleSliceGetValueDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

55 changes: 55 additions & 0 deletions lib/RexArticleSliceGetValueDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace redaxo\phpstan;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use rex_article_slice;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;
use function count;
use function in_array;

final class RexArticleSliceGetValueDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return rex_article_slice::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array(strtolower($methodReflection->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;
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down