Skip to content

Commit 9d72214

Browse files
swissspidyfelixarntzernilambar
authored
Merge pull request #612 from WordPress/fix/early-check-retrieval
Co-authored-by: felixarntz <flixos90@git.wordpress.org> Co-authored-by: swissspidy <swissspidy@git.wordpress.org> Co-authored-by: ernilambar <rabmalin@git.wordpress.org>
2 parents 75be00e + c532f10 commit 9d72214

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

includes/Checker/Abstract_Check_Runner.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace WordPress\Plugin_Check\Checker;
99

1010
use Exception;
11+
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;
1112
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
1213
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
13-
use WP_CLI;
1414

1515
/**
1616
* Abstract Check Runner class.
@@ -294,17 +294,31 @@ final public function set_categories( $categories ) {
294294
final public function prepare() {
295295
$cleanup_functions = array();
296296

297-
try {
298-
if ( $this->has_runtime_check( $this->get_checks_to_run() ) ) {
299-
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
300-
$cleanup_functions[] = $preparation->prepare();
301-
}
302-
} catch ( Exception $error ) {
303-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
304-
WP_CLI::error( $error->getMessage() );
305-
} else {
306-
throw $error;
297+
if ( $this->initialized_early ) {
298+
/*
299+
* When initialized early, plugins are not loaded yet when this method is called.
300+
* Therefore it could be that check slugs provided refer to addon checks that are not loaded yet.
301+
* In that case, the only reliable option is to assume that it refers to an addon check and that the addon
302+
* check is a runtime check. We don't know, but better to have the runtime preparations initialize
303+
* unnecessarily rather than not having them when needed.
304+
*
305+
* The actual checks to run are retrieved later (once plugins are loaded), so if one of the provided slugs
306+
* is actually invalid, the exception will still be thrown at that point.
307+
*/
308+
try {
309+
$checks = $this->get_checks_to_run();
310+
$initialize_runtime = $this->has_runtime_check( $checks );
311+
} catch ( Invalid_Check_Slug_Exception $e ) {
312+
$initialize_runtime = true;
307313
}
314+
} else {
315+
// When not initialized early, all checks are loaded, so we can simply see if there are runtime checks.
316+
$initialize_runtime = $this->has_runtime_check( $this->get_checks_to_run() );
317+
}
318+
319+
if ( $initialize_runtime ) {
320+
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
321+
$cleanup_functions[] = $preparation->prepare();
308322
}
309323

310324
if ( $this->delete_plugin_folder ) {

includes/Checker/Check_Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
use ArrayAccess;
1111
use Countable;
12-
use Exception;
1312
use IteratorAggregate;
13+
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;
1414

1515
/**
1616
* Check Collection interface.
@@ -82,7 +82,7 @@ public function exclude( array $check_slugs ): Check_Collection;
8282
* @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned.
8383
* @return Check_Collection The unchanged check collection.
8484
*
85-
* @throws Exception Thrown when any of the given check slugs is not present in the collection.
85+
* @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection.
8686
*/
8787
public function require( array $check_slugs ): Check_Collection;
8888
}

includes/Checker/Default_Check_Collection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace WordPress\Plugin_Check\Checker;
99

1010
use ArrayIterator;
11-
use Exception;
1211
use Traversable;
12+
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;
1313

1414
/**
1515
* Default Check Collection class.
@@ -150,12 +150,12 @@ static function ( Check $check, $slug ) use ( $check_slugs ) {
150150
* @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned.
151151
* @return Check_Collection The unchanged check collection.
152152
*
153-
* @throws Exception Thrown when any of the given check slugs is not present in the collection.
153+
* @throws Invalid_Check_Slug_Exception Thrown when any of the given check slugs is not present in the collection.
154154
*/
155155
public function require( array $check_slugs ): Check_Collection {
156156
foreach ( $check_slugs as $slug ) {
157157
if ( ! isset( $this->checks[ $slug ] ) ) {
158-
throw new Exception(
158+
throw new Invalid_Check_Slug_Exception(
159159
sprintf(
160160
/* translators: %s: The Check slug. */
161161
__( 'Check with the slug "%s" does not exist.', 'plugin-check' ),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Class WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception
4+
*
5+
* @package plugin-check
6+
*/
7+
8+
namespace WordPress\Plugin_Check\Checker\Exception;
9+
10+
use InvalidArgumentException;
11+
12+
/**
13+
* Class for an exception thrown when an invalid check slug is provided.
14+
*
15+
* @since n.e.x.t
16+
*/
17+
class Invalid_Check_Slug_Exception extends InvalidArgumentException {
18+
19+
}

tests/behat/features/plugin-check.feature

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,18 @@ Feature: Test that the WP-CLI command works.
514514
WordPress.WP.EnqueuedResourceParameters.NotInFooter,WARNING
515515
"""
516516

517-
# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
517+
# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.
518518
# And STDOUT should contain:
519519
# """
520520
# ExampleRuntimeCheck.ForbiddenScript,WARNING
521+
# """
522+
523+
# This doesn't currently work.
524+
# Run one runtime check from PCP and one from pcp-addon.
525+
# When I run the WP-CLI command `plugin check foo-sample --checks=non_blocking_scripts,example_runtime --fields=code,type --format=csv --require=./wp-content/plugins/plugin-check/cli.php`
526+
# Then STDOUT should contain:
527+
# """
528+
# ExampleRuntimeCheck.ForbiddenScript,WARNING
521529
# """
522530

523531
# This doesn't currently work, because we are not actually loading any other plugins, including pcp-addon.

0 commit comments

Comments
 (0)