-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support rex_article->getValue() type inference (#131)
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters