Skip to content

Commit

Permalink
support rex_article->getValue() type inference (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Sep 21, 2022
1 parent 0af003b commit 840b10b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/phpstan-dba.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ services:
class: redaxo\phpstan\RexMediaGetValueDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: redaxo\phpstan\RexArticleGetValueDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

55 changes: 55 additions & 0 deletions lib/RexArticleGetValueDynamicReturnTypeExtension.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;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;
use function count;
use function in_array;

final class RexArticleGetValueDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return rex_article::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', 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 @@ -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',
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 @@ -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',
Expand Down

0 comments on commit 840b10b

Please sign in to comment.