Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.travis.yml
behat.yml
circle.yml
phpcs.xml.dist
phpunit.xml.dist
bin/
features/
utils/
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ vendor/
*.tar.gz
composer.lock
*.log
phpunit.xml
phpcs.xml
.phpcs.xml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"behat/behat": "~2.5",
"wp-cli/wp-cli-tests": "^2"
"wp-cli/wp-cli-tests": "^2.1"
},
"config": {
"platform": {
Expand Down
6 changes: 3 additions & 3 deletions find-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
return;
}

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

WP_CLI::add_command( 'find', 'Find_Command' );
Expand Down
98 changes: 48 additions & 50 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI">
<description>Custom ruleset for WP-CLI</description>
<ruleset name="WP-CLI-find">
<description>Custom ruleset for WP-CLI find-command</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
#############################################################################
-->

<!-- What to scan. -->
<file>.</file>

<!-- For help in understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<!-- For help in using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Show progress. -->
<arg value="p"/>

<!-- What to scan -->
<file>.</file>
<!-- Ignoring Files and Folders:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>*/.git/*</exclude-pattern>
<exclude-pattern>*/ci/*</exclude-pattern>
<exclude-pattern>*/features/*</exclude-pattern>
<exclude-pattern>*/packages/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/utils/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- How to scan -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="colors"/> <!-- Show results with colors -->
<arg name="extensions" value="php"/> <!-- Limit to PHP files -->

<!-- Rules: Check PHP version compatibility - see
https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP">
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
</rule>
<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- For help in understanding this testVersion:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>
<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>

<!-- Ignore php_uname mode issue, we're conditionally providing a callback -->
<rule ref="PHPCompatibility.PHP.NewFunctionParameters.php_uname_modeFound">
<exclude-pattern>*/php/commands/src/CLI_Command.php</exclude-pattern>
</rule>
<!--
#############################################################################
USE THE WP_CLI_CS RULESET
#############################################################################
-->

<!-- Rules: WordPress Coding Standards - see
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<rule ref="WordPress-Core">
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
</rule>
<rule ref="WP_CLI_CS"/>

<!-- For help in understanding these custom sniff properties:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<rule ref="WordPress.Files.FileName">
<!--
#############################################################################
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
#############################################################################
-->

<!-- For help understanding the `testVersion` configuration setting:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="strict_class_file_names" value="false"/>
<property name="prefixes" type="array">
<element value="WP_CLI\Find"/><!-- Namespaces. -->
<element value="wpcli_find"/><!-- Global variables and such. -->
</property>
</properties>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
</rule>

<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
<exclude-pattern>*/src/Find_Command\.php$</exclude-pattern>
</rule>

</ruleset>
27 changes: 14 additions & 13 deletions src/Find_Command.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use WP_CLI\Utils;
use WP_CLI\Formatter;

class Find_Command {

Expand All @@ -9,7 +10,7 @@ class Find_Command {
*
* @var array
*/
private $ignored_paths = array(
private $ignored_paths = [
// System directories
'/__MACOSX/',
// Webserver directories
Expand Down Expand Up @@ -53,7 +54,7 @@ class Find_Command {
// Already in a WordPress install
'/wp-admin/',
'/wp-content/',
);
];

/**
* Beginning of the recursion path.
Expand Down Expand Up @@ -102,14 +103,14 @@ class Find_Command {
*
* @var array
*/
private $resolved_aliases = array();
private $resolved_aliases = [];

/**
* Found WordPress installations.
*
* @var array
*/
private $found_wp = array();
private $found_wp = [];

/**
* Find WordPress installations on the filesystem.
Expand Down Expand Up @@ -207,7 +208,7 @@ public function __invoke( $args, $assoc_args ) {
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
}

$fields = array( 'version_path', 'version', 'depth', 'alias' );
$fields = [ 'version_path', 'version', 'depth', 'alias' ];
if ( ! empty( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
}
Expand All @@ -216,7 +217,7 @@ public function __invoke( $args, $assoc_args ) {
$this->log( "Searching for WordPress installations in '{$path}'" );
$this->recurse_directory( $this->base_path );
$this->log( "Finished search for WordPress installations in '{$path}'" );
$formatter = new \WP_CLI\Formatter( $assoc_args, $fields );
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( $this->found_wp );
}

Expand All @@ -233,7 +234,7 @@ private function recurse_directory( $path ) {
// Don't recurse directories that probably don't have a WordPress installation.
if ( ! $this->skip_ignored_paths ) {
// Assume base path doesn't need comparison
$compared_path = preg_replace( '#^' . preg_quote( $this->base_path ) . '#', '', $path );
$compared_path = preg_replace( '#^' . preg_quote( $this->base_path, '/' ) . '#', '', $path );
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Uh.. oh... this is wrong... the delimiter should be passed as the second parameter and in this case, the delimiter is #.

Copy link
Copy Markdown
Member Author

@thrijith thrijith Jul 5, 2020

Choose a reason for hiding this comment

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

@schlessera, should I send a PR with # as the delimiter for preg_quote? I don't recall why I added /, not sure which is correct here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jrfnl Ah, good catch, thanks for that!

@thrijith Yes, please.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@thrijith Did you maybe intend to concatenate to add trailing slash here... ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Either way, preg_quote() should have a # as the second argument in this case.

// Ignore all hidden system directories
$bits = explode( '/', trim( $compared_path, '/' ) );
$current_dir = array_pop( $bits );
Expand All @@ -258,7 +259,7 @@ private function recurse_directory( $path ) {
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
$wp_path = rtrim( $wp_path, '/' ) . '/';

$this->found_wp[ $version_path ] = array(
$this->found_wp[ $version_path ] = [
'version_path' => $version_path,
'version' => self::get_wp_version( $version_path ),
'wp_path' => rtrim( $wp_path, '/' ) . '/',
Expand All @@ -267,25 +268,25 @@ private function recurse_directory( $path ) {
'db_host' => '',
'db_name' => '',
'db_user' => '',
);
];

$config_path = self::get_wp_config_path( $wp_path );
if ( $config_path ) {
try {
$transformer = new WPConfigTransformer( $config_path );
foreach ( array( 'db_host', 'db_name', 'db_user' ) as $constant ) {
foreach ( [ 'db_host', 'db_name', 'db_user' ] as $constant ) {
$value = $transformer->get_value( 'constant', strtoupper( $constant ) );
// Clean up strings.
$first = substr( $value, 0, 1 );
$last = substr( $value, -1 );
$both = array_unique( array( $first, $last ) );
if ( in_array( $both, array( array( '"' ), array( "'" ) ), true ) ) {
$both = array_unique( [ $first, $last ] );
if ( in_array( $both, [ [ '"' ], [ "'" ] ], true ) ) {
$value = substr( $value, 1, -1 );
}
$this->found_wp[ $version_path ][ $constant ] = $value;
}
} catch ( Exception $exception ) {
$this->log( "Could not process the 'wp-config.php' transformation: " . $exception->getMessage() );
$this->log( "Could not process the 'wp-config.php' transformation: {$exception->getMessage()}" );
}
}
$this->log( "Found WordPress installation at '{$version_path}'" );
Expand Down