Skip to content

Commit

Permalink
Merge pull request #36 from monojp/shortcode-atts-dynamic-function-re…
Browse files Browse the repository at this point in the history
…turn-type-extension

Add dynamic return type extension for shortcode_atts
  • Loading branch information
szepeviktor authored Mar 1, 2021
2 parents 0734941 + ed83ba8 commit c816f18
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ services:
class: PHPStan\WordPress\GetCommentDynamicFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: PHPStan\WordPress\ShortcodeAttsDynamicFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
parameters:
bootstrapFiles:
- ../../php-stubs/wordpress-stubs/wordpress-stubs.php
Expand Down
36 changes: 36 additions & 0 deletions src/ShortcodeAttsDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Set return type of shortcode_atts().
*/

declare(strict_types=1);

namespace PHPStan\WordPress;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;

final class ShortcodeAttsDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'shortcode_atts';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if ($functionCall->args === []) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->args,
$functionReflection->getVariants()
)->getReturnType();
}

return $scope->getType($functionCall->args[0]->value);
}
}

0 comments on commit c816f18

Please sign in to comment.