Skip to content

Commit 9deb573

Browse files
committed
Merge branch 'main' into copilot/add-block-related-commands
2 parents 4ce213f + 6617538 commit 9deb573

File tree

5 files changed

+156
-5
lines changed

5 files changed

+156
-5
lines changed

AGENTS.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Instructions
2+
3+
This package is part of WP-CLI, the official command line interface for WordPress. For a detailed explanation of the project structure and development workflow, please refer to the main @README.md file.
4+
5+
## Best Practices for Code Contributions
6+
7+
When contributing to this package, please adhere to the following guidelines:
8+
9+
* **Follow Existing Conventions:** Before writing any code, analyze the existing codebase in this package to understand the coding style, naming conventions, and architectural patterns.
10+
* **Focus on the Package's Scope:** All changes should be relevant to the functionality of the package.
11+
* **Write Tests:** All new features and bug fixes must be accompanied by acceptance tests using Behat. You can find the existing tests in the `features/` directory. There may be PHPUnit unit tests as well in the `tests/` directory.
12+
* **Update Documentation:** If your changes affect the user-facing functionality, please update the relevant inline code documentation.
13+
14+
### Building and running
15+
16+
Before submitting any changes, it is crucial to validate them by running the full suite of static code analysis and tests. To run the full suite of checks, execute the following command: `composer test`.
17+
18+
This single command ensures that your changes meet all the quality gates of the project. While you can run the individual steps separately, it is highly recommended to use this single command to ensure a comprehensive validation.
19+
20+
### Useful Composer Commands
21+
22+
The project uses Composer to manage dependencies and run scripts. The following commands are available:
23+
24+
* `composer install`: Install dependencies.
25+
* `composer test`: Run the full test suite, including linting, code style checks, static analysis, and unit/behavior tests.
26+
* `composer lint`: Check for syntax errors.
27+
* `composer phpcs`: Check for code style violations.
28+
* `composer phpcbf`: Automatically fix code style violations.
29+
* `composer phpstan`: Run static analysis.
30+
* `composer phpunit`: Run unit tests.
31+
* `composer behat`: Run behavior-driven tests.
32+
33+
### Coding Style
34+
35+
The project follows the `WP_CLI_CS` coding standard, which is enforced by PHP_CodeSniffer. The configuration can be found in `phpcs.xml.dist`. Before submitting any code, please run `composer phpcs` to check for violations and `composer phpcbf` to automatically fix them.
36+
37+
## Documentation
38+
39+
The `README.md` file might be generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). In that case, changes need to be made against the corresponding part of the codebase.
40+
41+
### Inline Documentation
42+
43+
Only write high-value comments if at all. Avoid talking to the user through comments.
44+
45+
## Testing
46+
47+
The project has a comprehensive test suite that includes unit tests, behavior-driven tests, and static analysis.
48+
49+
* **Unit tests** are written with PHPUnit and can be found in the `tests/` directory. The configuration is in `phpunit.xml.dist`.
50+
* **Behavior-driven tests** are written with Behat and can be found in the `features/` directory. The configuration is in `behat.yml`.
51+
* **Static analysis** is performed with PHPStan.
52+
53+
All tests are run on GitHub Actions for every pull request.
54+
55+
When writing tests, aim to follow existing patterns. Key conventions include:
56+
57+
* When adding tests, first examine existing tests to understand and conform to established conventions.
58+
* For unit tests, extend the base `WP_CLI\Tests\TestCase` test class.
59+
* For Behat tests, only WP-CLI commands installed in `composer.json` can be run.
60+
61+
### Behat Steps
62+
63+
WP-CLI makes use of a Behat-based testing framework and provides a set of custom step definitions to write feature tests.
64+
65+
> **Note:** If you are expecting an error output in a test, you need to use `When I try ...` instead of `When I run ...` .
66+
67+
#### Given
68+
69+
* `Given an empty directory` - Creates an empty directory.
70+
* `Given /^an? (empty|non-existent) ([^\s]+) directory$/` - Creates or deletes a specific directory.
71+
* `Given an empty cache` - Clears the WP-CLI cache directory.
72+
* `Given /^an? ([^\s]+) (file|cache file):$/` - Creates a file with the given contents.
73+
* `Given /^"([^"]+)" replaced with "([^"]+)" in the ([^\s]+) file$/` - Search and replace a string in a file using regex.
74+
* `Given /^that HTTP requests to (.*?) will respond with:$/` - Mock HTTP requests to a given URL.
75+
* `Given WP files` - Download WordPress files without installing.
76+
* `Given wp-config.php` - Create a wp-config.php file using `wp config create`.
77+
* `Given a database` - Creates an empty database.
78+
* `Given a WP install(ation)` - Installs WordPress.
79+
* `Given a WP install(ation) in :subdir` - Installs WordPress in a given directory.
80+
* `Given a WP install(ation) with Composer` - Installs WordPress with Composer.
81+
* `Given a WP install(ation) with Composer and a custom vendor directory :vendor_directory` - Installs WordPress with Composer and a custom vendor directory.
82+
* `Given /^a WP multisite (subdirectory|subdomain)?\s?(install|installation)$/` - Installs WordPress Multisite.
83+
* `Given these installed and active plugins:` - Installs and activates one or more plugins.
84+
* `Given a custom wp-content directory` - Configure a custom `wp-content` directory.
85+
* `Given download:` - Download multiple files into the given destinations.
86+
* `Given /^save (STDOUT|STDERR) ([\'].+[^\'])?\s?as \{(\w+)\}$/` - Store STDOUT or STDERR contents in a variable.
87+
* `Given /^a new Phar with (?:the same version|version "([^"]+)")$/` - Build a new WP-CLI Phar file with a given version.
88+
* `Given /^a downloaded Phar with (?:the same version|version "([^"]+)")$/` - Download a specific WP-CLI Phar version from GitHub.
89+
* `Given /^save the (.+) file ([\'].+[^\'])? as \{(\w+)\}$/` - Stores the contents of the given file in a variable.
90+
* `Given a misconfigured WP_CONTENT_DIR constant directory` - Modify wp-config.php to set `WP_CONTENT_DIR` to an empty string.
91+
* `Given a dependency on current wp-cli` - Add `wp-cli/wp-cli` as a Composer dependency.
92+
* `Given a PHP built-in web server` - Start a PHP built-in web server in the current directory.
93+
* `Given a PHP built-in web server to serve :subdir` - Start a PHP built-in web server in the given subdirectory.
94+
95+
#### When
96+
97+
* ``When /^I launch in the background `([^`]+)`$/`` - Launch a given command in the background.
98+
* ``When /^I (run|try) `([^`]+)`$/`` - Run or try a given command.
99+
* ``When /^I (run|try) `([^`]+)` from '([^\s]+)'$/`` - Run or try a given command in a subdirectory.
100+
* `When /^I (run|try) the previous command again$/` - Run or try the previous command again.
101+
102+
#### Then
103+
104+
* `Then /^the return code should( not)? be (\d+)$/` - Expect a specific exit code of the previous command.
105+
* `Then /^(STDOUT|STDERR) should( strictly)? (be|contain|not contain):$/` - Check the contents of STDOUT or STDERR.
106+
* `Then /^(STDOUT|STDERR) should be a number$/` - Expect STDOUT or STDERR to be a numeric value.
107+
* `Then /^(STDOUT|STDERR) should not be a number$/` - Expect STDOUT or STDERR to not be a numeric value.
108+
* `Then /^STDOUT should be a table containing rows:$/` - Expect STDOUT to be a table containing the given rows.
109+
* `Then /^STDOUT should end with a table containing rows:$/` - Expect STDOUT to end with a table containing the given rows.
110+
* `Then /^STDOUT should be JSON containing:$/` - Expect valid JSON output in STDOUT.
111+
* `Then /^STDOUT should be a JSON array containing:$/` - Expect valid JSON array output in STDOUT.
112+
* `Then /^STDOUT should be CSV containing:$/` - Expect STDOUT to be CSV containing certain values.
113+
* `Then /^STDOUT should be YAML containing:$/` - Expect STDOUT to be YAML containing certain content.
114+
* `Then /^(STDOUT|STDERR) should be empty$/` - Expect STDOUT or STDERR to be empty.
115+
* `Then /^(STDOUT|STDERR) should not be empty$/` - Expect STDOUT or STDERR not to be empty.
116+
* `Then /^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|<>) ([+\w.{}-]+)$/` - Expect STDOUT or STDERR to be a version string comparing to the given version.
117+
* `Then /^the (.+) (file|directory) should( strictly)? (exist|not exist|be:|contain:|not contain):$/` - Expect a certain file or directory to (not) exist or (not) contain certain contents.
118+
* `Then /^the contents of the (.+) file should( not)? match (((\/.*\/)|(#.#))([a-z]+)?)$/` - Match file contents against a regex.
119+
* `Then /^(STDOUT|STDERR) should( not)? match (((\/.*\/)|(#.#))([a-z]+)?)$/` - Match STDOUT or STDERR against a regex.
120+
* `Then /^an email should (be sent|not be sent)$/` - Expect an email to be sent (or not).
121+
* `Then the HTTP status code should be :code` - Expect the HTTP status code for visiting `http://localhost:8080`.

src/Comment_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function __construct() {
6363
* # Create comment.
6464
* $ wp comment create --comment_post_ID=15 --comment_content="hello blog" --comment_author="wp-cli"
6565
* Success: Created comment 932.
66+
*
67+
* @param string[] $args Positional arguments. Unused.
68+
* @param array<string, mixed> $assoc_args Associative arguments.
6669
*/
6770
public function create( $args, $assoc_args ) {
6871
$assoc_args = wp_slash( $assoc_args );
@@ -110,6 +113,9 @@ function ( $params ) {
110113
* # Update comment.
111114
* $ wp comment update 123 --comment_author='That Guy'
112115
* Success: Updated comment 123.
116+
*
117+
* @param string[] $args Positional arguments. Comment IDs to update.
118+
* @param array<string, mixed> $assoc_args Associative arguments.
113119
*/
114120
public function update( $args, $assoc_args ) {
115121
$assoc_args = wp_slash( $assoc_args );

src/Term_Command.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ public function list_( $args, $assoc_args ) {
142142
/**
143143
* @var \WP_Term[] $terms
144144
*/
145-
// phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Required for backward compatibility.
146-
$terms = get_terms( $args, $assoc_args );
145+
$terms = get_terms(
146+
array_merge(
147+
$assoc_args,
148+
[
149+
'taxonomy' => $args,
150+
]
151+
)
152+
);
147153
}
148154

149155
$terms = array_map(
@@ -211,7 +217,11 @@ public function create( $args, $assoc_args ) {
211217

212218
$assoc_args = wp_slash( $assoc_args );
213219
$term = wp_slash( $term );
214-
$result = wp_insert_term( $term, $taxonomy, $assoc_args );
220+
221+
/**
222+
* @var string $term
223+
*/
224+
$result = wp_insert_term( $term, $taxonomy, $assoc_args );
215225

216226
if ( is_wp_error( $result ) ) {
217227
WP_CLI::error( $result->get_error_message() );
@@ -643,8 +653,13 @@ public function recount( $args ) {
643653
* @var \WP_Term[] $terms
644654
*/
645655

646-
// phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Required for backward compatibility.
647-
$terms = get_terms( $taxonomy, [ 'hide_empty' => false ] );
656+
$terms = get_terms(
657+
[
658+
'taxonomy' => $taxonomy,
659+
'hide_empty' => false,
660+
]
661+
);
662+
648663
$term_taxonomy_ids = wp_list_pluck( $terms, 'term_taxonomy_id' );
649664

650665
wp_update_term_count( $term_taxonomy_ids, $taxonomy );

src/User_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ public function create( $args, $assoc_args ) {
553553
* # Update user
554554
* $ wp user update 123 --display_name=Mary --user_pass=marypass
555555
* Success: Updated user 123.
556+
*
557+
* @param string[] $args Positional arguments. Users to update.
558+
* @param array $assoc_args Associative arguments.
556559
*/
557560
public function update( $args, $assoc_args ) {
558561
if ( isset( $assoc_args['user_login'] ) ) {

src/WP_CLI/CommandWithMeta.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ public function update( $args, $assoc_args ) {
314314

315315
$object_id = $this->check_object_id( $object_id );
316316

317+
/**
318+
* @var array|string $meta_value
319+
*/
317320
$meta_value = sanitize_meta( $meta_key, $meta_value, $this->meta_type );
318321
$old_value = sanitize_meta( $meta_key, $this->get_metadata( $object_id, $meta_key, true ), $this->meta_type );
319322

@@ -454,6 +457,9 @@ function ( $key ) {
454457
WP_CLI::error( $exception->getMessage() );
455458
}
456459

460+
/**
461+
* @var array|string $patched_meta_value
462+
*/
457463
$patched_meta_value = sanitize_meta( $meta_key, $traverser->value(), $this->meta_type );
458464

459465
if ( $patched_meta_value === $old_meta_value ) {

0 commit comments

Comments
 (0)