Skip to content

Commit c816f18

Browse files
authored
Merge pull request #36 from monojp/shortcode-atts-dynamic-function-return-type-extension
Add dynamic return type extension for shortcode_atts
2 parents 0734941 + ed83ba8 commit c816f18

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ services:
3131
class: PHPStan\WordPress\GetCommentDynamicFunctionReturnTypeExtension
3232
tags:
3333
- phpstan.broker.dynamicFunctionReturnTypeExtension
34+
-
35+
class: PHPStan\WordPress\ShortcodeAttsDynamicFunctionReturnTypeExtension
36+
tags:
37+
- phpstan.broker.dynamicFunctionReturnTypeExtension
3438
parameters:
3539
bootstrapFiles:
3640
- ../../php-stubs/wordpress-stubs/wordpress-stubs.php
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Set return type of shortcode_atts().
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace PHPStan\WordPress;
10+
11+
use PhpParser\Node\Expr\FuncCall;
12+
use PHPStan\Analyser\Scope;
13+
use PHPStan\Reflection\FunctionReflection;
14+
use PHPStan\Reflection\ParametersAcceptorSelector;
15+
use PHPStan\Type\Type;
16+
17+
final class ShortcodeAttsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
18+
{
19+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
20+
{
21+
return $functionReflection->getName() === 'shortcode_atts';
22+
}
23+
24+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
25+
{
26+
if ($functionCall->args === []) {
27+
return ParametersAcceptorSelector::selectFromArgs(
28+
$scope,
29+
$functionCall->args,
30+
$functionReflection->getVariants()
31+
)->getReturnType();
32+
}
33+
34+
return $scope->getType($functionCall->args[0]->value);
35+
}
36+
}

0 commit comments

Comments
 (0)