Skip to content

Commit 979fa28

Browse files
authored
Merge branch 'main' into try/phpcs-in-feature-files
2 parents 6a3fbc8 + 9c4782b commit 979fa28

16 files changed

Lines changed: 561 additions & 10 deletions

‎.github/dependabot.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ updates:
77
open-pull-requests-limit: 10
88
labels:
99
- scope:distribution
10+
cooldown:
11+
default-days: 7
1012
- package-ecosystem: github-actions
1113
directory: "/"
1214
schedule:
1315
interval: daily
1416
open-pull-requests-limit: 10
1517
labels:
1618
- scope:distribution
19+
cooldown:
20+
default-days: 7
1721

‎.github/workflows/wp-versions-data-fetcher.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
pull-requests: write
3535
steps:
3636
- name: Checkout repository
37-
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
37+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3838
with:
3939
ref: ${{ env.ARTIFACTS_BRANCH }}
4040

‎composer.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
"slevomat/coding-standard": "^7.0 || ^8.0",
2424
"swissspidy/phpstan-no-private": "^0.2.1 || ^1.0",
2525
"szepeviktor/phpstan-wordpress": "^1.3.5 || ^2.0.3",
26-
"wp-cli/config-command": "^1 || ^2",
27-
"wp-cli/core-command": "^1 || ^2",
28-
"wp-cli/eval-command": "^1 || ^2",
29-
"wp-cli/wp-cli": "^2.13",
26+
"wp-cli/config-command": "^1 || ^2 || ^3",
27+
"wp-cli/core-command": "^1 || ^2 || ^3",
28+
"wp-cli/eval-command": "^1 || ^2 || ^3",
29+
"wp-cli/wp-cli": "^3.0",
3030
"wp-coding-standards/wpcs": "dev-develop",
31-
"yoast/phpunit-polyfills": "^1.0 || ^2.0 || ^3.0 || ^4.0"
31+
"yoast/phpunit-polyfills": "^2.0 || ^3.0 || ^4.0"
3232
},
3333
"require-dev": {
3434
"roave/security-advisories": "dev-latest"

‎extension.neon‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ 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: WP_CLI\Tests\PHPStan\WPCliGetConfigDynamicReturnTypeExtension
20+
tags:
21+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
22+
-
23+
class: SzepeViktor\PHPStan\WordPress\HookDocsVisitor
24+
tags:
25+
- phpstan.parser.richParserNodeVisitor
26+
rules:
27+
- WP_CLI\Tests\PHPStan\WPCliAddHookCallbackRule
28+
1429
parameters:
1530
dynamicConstantNames:
1631
- FOO

‎features/behat-steps.feature‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ Feature: Test that WP-CLI Behat steps work as expected
4444
Hello World
4545
"""
4646

47+
Scenario: Test "Given a file" step with quoted path containing spaces
48+
Given an empty directory
49+
And a "test with spaces.txt" file:
50+
"""
51+
Hello World with Spaces
52+
"""
53+
Then the "test with spaces.txt" file should exist
54+
And the "test with spaces.txt" file should contain:
55+
"""
56+
Hello World with Spaces
57+
"""
58+
4759
Scenario: Test "Given a cache file" step
4860
Given a WP installation
4961
And an empty cache
@@ -57,6 +69,19 @@ Feature: Test that WP-CLI Behat steps work as expected
5769
Cached content
5870
"""
5971

72+
Scenario: Test "Given a cache file" step with quoted path containing spaces
73+
Given a WP installation
74+
And an empty cache
75+
And a "test cache with spaces.txt" cache file:
76+
"""
77+
Cached Content with Spaces
78+
"""
79+
Then the "{SUITE_CACHE_DIR}/test cache with spaces.txt" file should exist
80+
And the "{SUITE_CACHE_DIR}/test cache with spaces.txt" file should contain:
81+
"""
82+
Cached Content with Spaces
83+
"""
84+
6085
Scenario: Test string replacement in file
6186
Given an empty directory
6287
And a config.txt file:

‎src/Context/FeatureContext.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ public function proc( $command, $assoc_args = [], $path = '' ): Process {
14201420
$env['BEHAT_SCENARIO_TITLE'] = $this->scenario->getTitle();
14211421
}
14221422

1423-
$env['BEHAT_STEP_LINE'] = $this->step_line;
1423+
$env['BEHAT_STEP_LINE'] = (string) $this->step_line;
14241424

14251425
$env['WP_CLI_TEST_DBTYPE'] = self::$db_type;
14261426

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ public function given_an_empty_cache(): void {
120120
*
121121
* @access public
122122
*
123-
* @Given /^an? ([^\s]+) (file|cache file):$/
123+
* @Given /^an? ("[^"]+"|[^\s]+) (file|cache file):$/
124124
*
125125
* @param string $path
126126
* @param string $type
127127
* @param PyStringNode $content
128128
*/
129129
public function given_a_specific_file( $path, $type, PyStringNode $content ): void {
130-
$path = $this->replace_variables( (string) $path );
130+
$path = trim( $this->replace_variables( (string) $path ), '"' );
131131
$content = $this->replace_variables( (string) $content ) . "\n";
132132
$full_path = 'cache file' === $type
133133
? $this->variables['SUITE_CACHE_DIR'] . "/$path"

‎src/Context/ThenStepDefinitions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public function then_stdout_stderr_should_be_a_specific_version_string( $stream,
458458
* @param string $expected Expected content.
459459
*/
460460
public function then_a_specific_file_folder_should_exist( $path, $type, $strictly, $action, $expected = null ): void {
461-
$path = $this->replace_variables( $path );
461+
$path = trim( $this->replace_variables( $path ), '"' );
462462

463463
// If it's a relative path, make it relative to the current test dir.
464464
if ( ! Path::is_absolute( $path ) ) {
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WP_CLI\Tests\PHPStan;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\StaticCall;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\Type\VerbosityLevel;
13+
14+
use function count;
15+
use function sprintf;
16+
use function strpos;
17+
18+
/**
19+
* @implements Rule<StaticCall>
20+
*/
21+
final class WPCliAddHookCallbackRule implements Rule {
22+
23+
private const KNOWN_HOOK_ARGS = [
24+
'find_command_to_run_pre' => 0,
25+
'before_wp_load' => 0,
26+
'before_wp_config_load' => 0,
27+
'after_wp_config_load' => 0,
28+
'after_wp_load' => 0,
29+
'before_ssh' => 0,
30+
'before_registering_contexts' => 1,
31+
'formatter_available_formats' => 1,
32+
'http_request_options' => 5,
33+
'before_run_command' => 3,
34+
'search_replace_unserialize_options' => 1,
35+
];
36+
37+
private const DYNAMIC_HOOK_PREFIXES = [
38+
'before_add_command:' => 1,
39+
'after_add_command:' => 0,
40+
'before_invoke:' => 1,
41+
'after_invoke:' => 1,
42+
];
43+
44+
public function getNodeType(): string {
45+
return StaticCall::class;
46+
}
47+
48+
public function processNode( Node $node, Scope $scope ): array {
49+
if ( ! $node instanceof StaticCall ) {
50+
return [];
51+
}
52+
53+
if ( ! $node->name instanceof Node\Identifier || 'add_hook' !== $node->name->name ) {
54+
return [];
55+
}
56+
57+
if ( ! $node->class instanceof Node\Name || 'WP_CLI' !== $scope->resolveName( $node->class ) ) {
58+
return [];
59+
}
60+
61+
$args = $node->getArgs();
62+
if ( count( $args ) < 2 ) {
63+
return [];
64+
}
65+
66+
$callbackType = $scope->getType( $args[1]->value );
67+
if ( ! $callbackType->isCallable()->yes() ) {
68+
if ( $callbackType->isCallable()->no() ) {
69+
return [
70+
RuleErrorBuilder::message(
71+
sprintf(
72+
'Parameter #2 $callback of WP_CLI::add_hook() expects a valid callable, %s given.',
73+
$callbackType->describe( VerbosityLevel::typeOnly() )
74+
)
75+
)->identifier( 'wpCli.addHookCallback.invalidCallback' )->build(),
76+
];
77+
}
78+
return [];
79+
}
80+
81+
$hookNameType = $scope->getType( $args[0]->value );
82+
$hookNameStrings = $hookNameType->getConstantStrings();
83+
if ( count( $hookNameStrings ) !== 1 ) {
84+
return [];
85+
}
86+
87+
$hookName = $hookNameStrings[0]->getValue();
88+
$expectedArgs = $this->getExpectedArgCountForHook( $hookName );
89+
if ( null === $expectedArgs ) {
90+
return [];
91+
}
92+
93+
$callableParametersAcceptors = $callbackType->getCallableParametersAcceptors( $scope );
94+
if ( count( $callableParametersAcceptors ) === 0 ) {
95+
return [];
96+
}
97+
98+
$parametersAcceptor = $callableParametersAcceptors[0];
99+
$requiredParams = 0;
100+
foreach ( $parametersAcceptor->getParameters() as $parameter ) {
101+
if ( ! $parameter->isOptional() ) {
102+
++$requiredParams;
103+
}
104+
}
105+
106+
if ( $requiredParams > $expectedArgs ) {
107+
return [
108+
RuleErrorBuilder::message(
109+
sprintf(
110+
'Callback for hook "%s" expects %d required %s, but only %d %s passed by WP_CLI::do_hook().',
111+
$hookName,
112+
$requiredParams,
113+
1 === $requiredParams ? 'argument' : 'arguments',
114+
$expectedArgs,
115+
1 === $expectedArgs ? 'argument is' : 'arguments are'
116+
)
117+
)->identifier( 'wpCli.addHookCallback.insufficientParameters' )->build(),
118+
];
119+
}
120+
121+
return [];
122+
}
123+
124+
private function getExpectedArgCountForHook( string $hookName ): ?int {
125+
if ( isset( self::KNOWN_HOOK_ARGS[ $hookName ] ) ) {
126+
return self::KNOWN_HOOK_ARGS[ $hookName ];
127+
}
128+
129+
foreach ( self::DYNAMIC_HOOK_PREFIXES as $prefix => $count ) {
130+
if ( 0 === strpos( $hookName, $prefix ) ) {
131+
return $count;
132+
}
133+
}
134+
135+
return null;
136+
}
137+
}
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+
}

0 commit comments

Comments
 (0)