Skip to content

Commit f405348

Browse files
authored
PHPStan: Add dynamic return type extension for WP_CLI::get_config() (#346)
1 parent 7eaba17 commit f405348

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

‎extension.neon‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ services:
1515
class: WP_CLI\Tests\PHPStan\WPCliDoHookDynamicReturnTypeExtension
1616
tags:
1717
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
18+
-
19+
class: WP_CLI\Tests\PHPStan\WPCliGetConfigDynamicReturnTypeExtension
20+
tags:
21+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
1822
-
1923
class: SzepeViktor\PHPStan\WordPress\HookDocsVisitor
2024
tags:
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WP_CLI\Tests\PHPStan;
6+
7+
use PhpParser\Node\Expr\StaticCall;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\ArrayType;
11+
use PHPStan\Type\BooleanType;
12+
use PHPStan\Type\Constant\ConstantArrayType;
13+
use PHPStan\Type\Constant\ConstantBooleanType;
14+
use PHPStan\Type\Constant\ConstantStringType;
15+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
16+
use PHPStan\Type\IntegerType;
17+
use PHPStan\Type\NullType;
18+
use PHPStan\Type\StringType;
19+
use PHPStan\Type\Type;
20+
use PHPStan\Type\TypeCombinator;
21+
22+
use function count;
23+
use function array_values;
24+
25+
final class WPCliGetConfigDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension {
26+
27+
public function getClass(): string {
28+
return 'WP_CLI';
29+
}
30+
31+
public function isStaticMethodSupported( MethodReflection $methodReflection ): bool {
32+
return $methodReflection->getName() === 'get_config';
33+
}
34+
35+
public function getTypeFromStaticMethodCall(
36+
MethodReflection $methodReflection,
37+
StaticCall $methodCall,
38+
Scope $scope
39+
): Type {
40+
$args = $methodCall->getArgs();
41+
42+
if ( count( $args ) === 0 ) {
43+
return $this->getGlobalConfigArrayType();
44+
}
45+
46+
$keyType = $scope->getType( $args[0]->value );
47+
48+
if ( $keyType->isNull()->yes() ) {
49+
return $this->getGlobalConfigArrayType();
50+
}
51+
52+
$constantStrings = $keyType->getConstantStrings();
53+
if ( count( $constantStrings ) > 0 ) {
54+
$types = [];
55+
$configMap = $this->getConfigMap();
56+
57+
foreach ( $constantStrings as $constantString ) {
58+
$key = $constantString->getValue();
59+
if ( isset( $configMap[ $key ] ) ) {
60+
$types[] = $configMap[ $key ];
61+
} else {
62+
$types[] = new NullType();
63+
}
64+
}
65+
66+
if ( count( $types ) > 0 ) {
67+
$returnType = TypeCombinator::union( ...$types );
68+
if ( $keyType->isNull()->maybe() ) {
69+
$returnType = TypeCombinator::union( $returnType, $this->getGlobalConfigArrayType() );
70+
}
71+
return $returnType;
72+
}
73+
}
74+
75+
// Fallback for non-constant string or unknown types
76+
$fallback = TypeCombinator::addNull( $this->getFallbackValueType() );
77+
if ( $keyType->isNull()->maybe() ) {
78+
return TypeCombinator::union( $fallback, $this->getGlobalConfigArrayType() );
79+
}
80+
81+
return $fallback;
82+
}
83+
84+
/**
85+
* @return array<string, Type>
86+
*/
87+
private function getConfigMap(): array {
88+
$stringType = new StringType();
89+
$stringOrNull = TypeCombinator::addNull( $stringType );
90+
$stringList = new ArrayType( new IntegerType(), $stringType );
91+
$boolType = new BooleanType();
92+
$trueOrStringList = TypeCombinator::union( new ConstantBooleanType( true ), $stringList );
93+
$stringOrTrue = TypeCombinator::union( $stringType, new ConstantBooleanType( true ) );
94+
$stringOrFalse = TypeCombinator::union( $stringType, new ConstantBooleanType( false ) );
95+
96+
return [
97+
'path' => $stringOrNull,
98+
'ssh' => $stringOrNull,
99+
'ssh-args' => $stringList,
100+
'http' => $stringOrNull,
101+
'url' => $stringOrNull,
102+
'user' => $stringOrNull,
103+
'skip-plugins' => $trueOrStringList,
104+
'skip-themes' => $trueOrStringList,
105+
'skip-packages' => $boolType,
106+
'require' => $stringList,
107+
'exec' => $stringList,
108+
'context' => $stringType,
109+
'debug' => $stringOrTrue,
110+
'prompt' => $stringOrFalse,
111+
'quiet' => $boolType,
112+
'apache_modules' => $stringList,
113+
'assume-https' => $boolType,
114+
'color' => TypeCombinator::union( $stringType, $boolType ),
115+
'disabled_commands' => $stringList,
116+
'locale' => $stringType,
117+
'allow-root' => $boolType,
118+
'alias' => $stringType,
119+
];
120+
}
121+
122+
private function getGlobalConfigArrayType(): Type {
123+
$keyTypes = [];
124+
$valueTypes = [];
125+
126+
foreach ( $this->getConfigMap() as $key => $type ) {
127+
$keyTypes[] = new ConstantStringType( $key );
128+
$valueTypes[] = $type;
129+
}
130+
131+
return new ConstantArrayType( $keyTypes, $valueTypes );
132+
}
133+
134+
private function getFallbackValueType(): Type {
135+
$types = array_values( $this->getConfigMap() );
136+
return TypeCombinator::union( ...$types );
137+
}
138+
}

‎tests/data/get_config.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Test data for WPCliGetConfigDynamicReturnTypeExtension.
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+
// No arguments
15+
assertType( 'array{path: string|null, ssh: string|null, ssh-args: array<int, string>, http: string|null, url: string|null, user: string|null, skip-plugins: array<int, string>|true, skip-themes: array<int, string>|true, skip-packages: bool, require: array<int, string>, exec: array<int, string>, context: string, debug: string|true, prompt: string|false, quiet: bool, apache_modules: array<int, string>, assume-https: bool, color: bool|string, disabled_commands: array<int, string>, locale: string, allow-root: bool, alias: string}', WP_CLI::get_config() );
16+
assertType( 'array{path: string|null, ssh: string|null, ssh-args: array<int, string>, http: string|null, url: string|null, user: string|null, skip-plugins: array<int, string>|true, skip-themes: array<int, string>|true, skip-packages: bool, require: array<int, string>, exec: array<int, string>, context: string, debug: string|true, prompt: string|false, quiet: bool, apache_modules: array<int, string>, assume-https: bool, color: bool|string, disabled_commands: array<int, string>, locale: string, allow-root: bool, alias: string}', WP_CLI::get_config( null ) );
17+
18+
// Specific keys
19+
assertType( 'string|null', WP_CLI::get_config( 'path' ) );
20+
assertType( 'array<int, string>', WP_CLI::get_config( 'ssh-args' ) );
21+
assertType( 'bool', WP_CLI::get_config( 'skip-packages' ) );
22+
assertType( 'array<int, string>|true', WP_CLI::get_config( 'skip-plugins' ) );
23+
assertType( 'string|false', WP_CLI::get_config( 'prompt' ) );
24+
assertType( 'bool', WP_CLI::get_config( 'quiet' ) );
25+
assertType( 'bool', WP_CLI::get_config( 'assume-https' ) );
26+
27+
// Nullable and non-constant keys
28+
/** @var string|null $nullable_key */
29+
$nullable_key = null;
30+
assertType( 'array<\'alias\'|\'allow-root\'|\'apache_modules\'|\'assume-https\'|\'color\'|\'context\'|\'debug\'|\'disabled_commands\'|\'exec\'|\'http\'|\'locale\'|\'path\'|\'prompt\'|\'quiet\'|\'require\'|\'skip-packages\'|\'skip-plugins\'|\'skip-themes\'|\'ssh\'|\'ssh-args\'|\'url\'|\'user\'|int, array<int, string>|bool|string|null>|bool|string|null', WP_CLI::get_config( $nullable_key ) );
31+
32+
/** @var string $string_key */
33+
$string_key = 'path';
34+
assertType( 'array<int, string>|bool|string|null', WP_CLI::get_config( $string_key ) );
35+
36+
// Invalid key
37+
assertType( 'null', WP_CLI::get_config( 'invalid_key' ) );

‎tests/tests/PHPStan/TestDynamicReturnTypeExtension.php‎

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

2223
/**

0 commit comments

Comments
 (0)