Skip to content

Conversation

@thrijith
Copy link
Member

@thrijith thrijith commented Apr 18, 2019

Add a PHPCS ruleset using the new WPCliCS standard.

Fixes #104

Related wp-cli/wp-cli#5179

Update .distignore and .gitignore with phpcs/phpunit config files
Update wp-cli-tests to 2.1
@thrijith thrijith requested a review from a team as a code owner April 18, 2019 11:34
@schlessera schlessera changed the title Implement CS checking based on the WPCliCS ruleset Implement CS checking based on the WP_CLI_CS ruleset Apr 19, 2019
Copy link
Member

@schlessera schlessera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, put the PHPCS in the preceding line instead of after the statement. Horizontal scrolling is not something a lot of people enjoy... ;)

Update namespace function calls and array syntax
@schlessera schlessera added this to the 2.0.3 milestone Apr 19, 2019
@schlessera
Copy link
Member

Ok, the file is now being loaded twice, so you need to wrap a function_exists() check around the wpcli_assert_image_editor_support() function.

Silly, but there's no easy way I can think of to avoid that.

@schlessera
Copy link
Member

Hmm, maybe it is worth investigating why it is being loaded twice after all...

@schlessera
Copy link
Member

So wrapping the function in function_exists(), but it is just cloaking the fact that we're actually adding the command twice:

Image 2019-04-19 at 7 40 18 PM

This is of course just the case when doing development on the command itself, but nevertheless, this bothers me.

Two solutions I can think of:

  • Define a new pattern to make sure the command bootstrap file is only processed once (will probably need to be define based).
  • Bail early if you try to add_command() a command that was already added (will need to investigate what this means for overrides).

@schlessera
Copy link
Member

Here's what I came up with so far.

This solves two problems:

  • the file can be parsed multiple times without throwing an error (because I turned the regular function into a closure)
  • the file can be executed multiple times without adding the command more than once (because of the define).
<?php

if ( defined( 'WP_CLI_MEDIA_COMMAND_LOADED' ) || ! class_exists( 'WP_CLI' ) ) {
	return;
}

define( 'WP_CLI_MEDIA_COMMAND_LOADED', true );

$wpcli_media_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $wpcli_media_autoloader ) ) {
	require_once $wpcli_media_autoloader;
}

/**
 * Check for Imagick and GD extensions availability.
 *
 * @throws \WP_CLI\ExitException
 */
$wpcli_media_assert_image_editor_support = function () {
	if ( ! wp_image_editor_supports() ) {
		WP_CLI::error(
			'No support for generating images found. '
			. 'Please install the Imagick or GD PHP extensions.'
		);
	}
};

WP_CLI::add_command(
	'media',
	'Media_Command',
	[ 'before_invoke' => $wpcli_media_assert_image_editor_support ]
);

How do you feel about that?

@schlessera
Copy link
Member

I can't really make up my mind just yet on a new pattern, so let's ignore the double-loading for this release.

To fix the broken tests, just turn the regular function into a closure:

/**
 * Check for Imagick and GD extensions availability.
 *
 * @throws \WP_CLI\ExitException
 */
$wpcli_media_assert_image_editor_support = function () {
	if ( ! wp_image_editor_supports() ) {
		WP_CLI::error(
			'No support for generating images found. '
			. 'Please install the Imagick or GD PHP extensions.'
		);
	}
};

WP_CLI::add_command(
	'media',
	'Media_Command',
	[ 'before_invoke' => $wpcli_media_assert_image_editor_support ]
);

@thrijith
Copy link
Member Author

I was away so couldn't reply faster, this seems fine #106 (comment).

so let's ignore the double-loading for this release.

Didn't get this part.

So, let's go with the last option then? The invoking function was a closure already, we now have better readability.

@schlessera schlessera merged commit 885b5e6 into wp-cli:master Apr 19, 2019
@thrijith thrijith deleted the feature/use-phpcs branch April 20, 2019 01:25
danielbachhuber pushed a commit that referenced this pull request Nov 18, 2022
Implement CS checking based on the `WP_CLI_CS` ruleset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adopt and enforce new WP_CLI_CS standard

2 participants