Skip to content

Commit d339d2d

Browse files
authored
PHPStan: Add dynamic return type extension for WP_CLI::do_hook() (#344)
1 parent e307fc6 commit d339d2d

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎extension.neon‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ services:
1111
class: WP_CLI\Tests\PHPStan\WPCliRuncommandDynamicReturnTypeExtension
1212
tags:
1313
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
14+
-
15+
class: WP_CLI\Tests\PHPStan\WPCliDoHookDynamicReturnTypeExtension
16+
tags:
17+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
18+
-
19+
class: SzepeViktor\PHPStan\WordPress\HookDocsVisitor
20+
tags:
21+
- phpstan.parser.richParserNodeVisitor
1422
parameters:
1523
dynamicConstantNames:
1624
- FOO
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WP_CLI\Tests\PHPStan;
6+
7+
use PhpParser\Comment\Doc;
8+
use PhpParser\Node\Expr\StaticCall;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
12+
use PHPStan\Type\FileTypeMapper;
13+
use PHPStan\Type\NullType;
14+
use PHPStan\Type\Type;
15+
16+
use function count;
17+
18+
final class WPCliDoHookDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension {
19+
20+
/**
21+
* @var FileTypeMapper
22+
*/
23+
private $fileTypeMapper;
24+
25+
public function __construct( FileTypeMapper $fileTypeMapper ) {
26+
$this->fileTypeMapper = $fileTypeMapper;
27+
}
28+
29+
public function getClass(): string {
30+
return 'WP_CLI';
31+
}
32+
33+
public function isStaticMethodSupported( MethodReflection $methodReflection ): bool {
34+
return $methodReflection->getName() === 'do_hook';
35+
}
36+
37+
public function getTypeFromStaticMethodCall(
38+
MethodReflection $methodReflection,
39+
StaticCall $methodCall,
40+
Scope $scope
41+
): Type {
42+
$args = $methodCall->getArgs();
43+
44+
if ( count( $args ) < 2 ) {
45+
return new NullType();
46+
}
47+
48+
$docComment = $methodCall->getAttribute( 'latestDocComment' );
49+
if ( $docComment instanceof Doc ) {
50+
$classReflection = $scope->getClassReflection();
51+
$traitReflection = $scope->getTraitReflection();
52+
53+
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
54+
$scope->getFile(),
55+
( $scope->isInClass() && null !== $classReflection ) ? $classReflection->getName() : null,
56+
( $scope->isInTrait() && null !== $traitReflection ) ? $traitReflection->getName() : null,
57+
$scope->getFunctionName(),
58+
$docComment->getText()
59+
);
60+
61+
$params = $resolvedPhpDoc->getParamTags();
62+
foreach ( $params as $paramTag ) {
63+
return $paramTag->getType();
64+
}
65+
}
66+
67+
return $scope->getType( $args[1]->value );
68+
}
69+
}

‎tests/data/do_hook.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Test data for WPCliDoHookDynamicReturnTypeExtension.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace WP_CLI\Tests\Tests\PHPStan;
10+
11+
use WP_CLI;
12+
use function PHPStan\Testing\assertType;
13+
14+
// Action without arguments returns null.
15+
$action_result = WP_CLI::do_hook( 'custom_action' );
16+
assertType( 'null', $action_result );
17+
18+
// Filter with scalar argument returns type of argument.
19+
$string_result = WP_CLI::do_hook( 'custom_string_filter', 'default_val' );
20+
assertType( "'default_val'", $string_result );
21+
22+
// Filter with array argument returns exact array type.
23+
$array_data = [
24+
'a' => 1,
25+
'b' => 2,
26+
];
27+
$array_result = WP_CLI::do_hook( 'custom_array_filter', $array_data );
28+
assertType( 'array{a: 1, b: 2}', $array_result );
29+
30+
/** @var array<string, int> $typed_array */
31+
$typed_array = [ 'count' => 5 ];
32+
$typed_result = WP_CLI::do_hook( 'custom_typed_filter', $typed_array );
33+
assertType( 'array<string, int>', $typed_result );
34+
35+
/**
36+
* Filter available options.
37+
*
38+
* @param array<string, bool> $options Filtered options.
39+
*/
40+
$docblock_result = WP_CLI::do_hook( 'custom_docblock_filter', $typed_array );
41+
assertType( 'array<string, bool>', $docblock_result );
42+
43+
/**
44+
* Filter available formats.
45+
*
46+
* @param string[] $formats Array of format names.
47+
*/
48+
$formats_result = WP_CLI::do_hook( 'formatter_available_formats', [ 'table', 'json' ] );
49+
assertType( 'array<string>', $formats_result );

‎tests/tests/PHPStan/TestDynamicReturnTypeExtension.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static function dataFileAsserts(): iterable {
1616
yield from self::gatherAssertTypes( dirname( __DIR__, 2 ) . '/data/parse_url.php' );
1717
yield from self::gatherAssertTypes( dirname( __DIR__, 2 ) . '/data/get_flag_value.php' );
1818
yield from self::gatherAssertTypes( dirname( __DIR__, 2 ) . '/data/runcommand.php' );
19+
yield from self::gatherAssertTypes( dirname( __DIR__, 2 ) . '/data/do_hook.php' );
1920
}
2021

2122
/**

0 commit comments

Comments
 (0)