Skip to content

Commit a3baceb

Browse files
committed
Add dynamic return type extension for shortcode_atts
1 parent 0734941 commit a3baceb

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
$argsCount = count($functionCall->args);
27+
if ($argsCount === 0) {
28+
return ParametersAcceptorSelector::selectFromArgs(
29+
$scope,
30+
$functionCall->args,
31+
$functionReflection->getVariants()
32+
)->getReturnType();
33+
}
34+
35+
return $scope->getType($functionCall->args[0]->value);
36+
}
37+
}

0 commit comments

Comments
 (0)