Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine includes and excludes based on scores #104

Merged
merged 8 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
85 changes: 36 additions & 49 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,64 +92,51 @@ public static function fromDirectory( $dir, Translations $translations, array $o
/**
* Determines whether a file is valid based on the include option.
*
* @param SplFileInfo $file File or directory.
* @param array $include List of files and directories to include.
* @return bool
* @param SplFileInfo $file File or directory.
* @param array $matchers List of files and directories to match.
* @return int How strongly the file is matched.
*/
protected static function isIncluded( SplFileInfo $file, array $include = [] ) {
if ( empty( $include ) ) {
return true;
protected static function calculateMatchScore( SplFileInfo $file, array $matchers = [] ) {
if ( empty( $matchers ) ) {
return 0;
}

if ( in_array( $file->getBasename(), $include, true ) ) {
return true;
if ( in_array( $file->getBasename(), $matchers, true ) ) {
return 10;
}

// Check for more complex paths, e.g. /some/sub/folder.
$root_relative_path = str_replace( static::$dir, '', $file->getPathname() );
foreach ( $include as $path_or_file ) {

foreach ( $matchers as $path_or_file ) {
$pattern = preg_quote( str_replace( '*', '__wildcard__', $path_or_file ) );
$pattern = '/' . str_replace( '__wildcard__', '(.+)', $pattern );
$pattern = '(^|/)' . str_replace( '__wildcard__', '(.+)', $pattern );

$base_score = count(
array_filter(
explode( '/', $path_or_file ),
function ( $component) { return $component !== '*'; }
)
);
if ( $base_score === 0 ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
// If the matcher is simply * it gets a score above the implicit score but below 1.
$base_score = 0.2;
}


if (
false !== mb_ereg( $pattern, $root_relative_path . '$' ) &&
false !== mb_ereg( $pattern, $root_relative_path . '/' )
false === strpos( $path_or_file, '*' ) &&
false !== mb_ereg( $pattern . '$', $root_relative_path )
) {
return true;
return $base_score * 10;
}
}

return false;
}

/**
* Determines whether a file is valid based on the exclude option.
*
* @param SplFileInfo $file File or directory.
* @param array $exclude List of files and directories to skip.
* @return bool
*/
protected static function isExcluded( SplFileInfo $file, array $exclude = [] ) {
if ( empty( $exclude ) ) {
return false;
}

if ( in_array( $file->getBasename(), $exclude, true ) ) {
return true;
}

// Check for more complex paths, e.g. /some/sub/folder.
foreach ( $exclude as $path_or_file ) {
$pattern = preg_quote( str_replace( '*', '__wildcard__', $path_or_file ) );
$pattern = '/' . str_replace( '__wildcard__', '(.+)', $pattern ) . '$';

$root_relative_path = str_replace( static::$dir, '', $file->getPathname() );
if ( false !== mb_ereg( $pattern, $root_relative_path ) ) {
return true;
if ( false !== mb_ereg( $pattern . '(/|$)', $root_relative_path ) ) {
return $base_score;
}
}

return false;
return 0;
}

/**
Expand All @@ -172,16 +159,16 @@ function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions ) {
/** @var RecursiveCallbackFilterIterator $iterator */
/** @var SplFileInfo $file */

if ( static::isExcluded( $file, $exclude ) && ( empty( $include ) || ! static::isIncluded( $file, $include ) ) ) {
return false;
if ( $iterator->hasChildren() ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

if ( ! static::isIncluded( $file, $include ) && ! $iterator->hasChildren() ) {
return false;
}
// If no $include is passed everything gets the weakest possible matching score.
$inclusion_score = empty( $include ) ? 0.1 : static::calculateMatchScore( $file, $include );
$exclusion_score = static::calculateMatchScore( $file, $exclude );

if ( $iterator->hasChildren() ) {
return true;
if ( $inclusion_score === 0 || $exclusion_score > $inclusion_score ) {
return false;
}

return ( $file->isFile() && in_array( $file->getExtension(), $extensions, true ) );
Expand Down
17 changes: 11 additions & 6 deletions tests/IterableCodeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public function setUp() {
* PHP5.4 cannot set property with __DIR__ constant.
*/
self::$base = __DIR__ . '/data/';

$property = new \ReflectionProperty( 'WP_CLI\I18n\IterableCodeExtractor', 'dir' );
$property->setAccessible( true );
$property->setValue( null, self::$base );
$property->setAccessible( false );
}

public function test_can_include_files() {
$includes = [ 'foo', 'bar', 'baz/inc*.js' ];
$includes = [ 'foo-plugin', 'bar', 'baz/inc*.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] );
$expected = static::$base . 'foo-plugin/foo-plugin.php';
$this->assertContains( $expected, $result );
Expand Down Expand Up @@ -70,18 +75,18 @@ public function test_can_exclude_override_wildcard() {
}

public function test_can_exclude_override_matching_directory() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], ['excluded'], [ 'php' ] );
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], ['foo/bar/excluded/*'], [ 'php' ] );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/excluded/excluded.js';
$this->assertContains( $expected_1, $result );
$this->assertNotContains( $expected_2, $result );
}

public function test_can_not_exclude_partially_directory() {
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], ['exc'], [ 'js' ] );
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'exc' ], [ 'js' ] );
$expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php';
$expected_2 = static::$base . 'foo/bar/excluded/ignored.js';
//$this->assertContains( $expected_1, $result );
$this->assertNotContains( $expected_1, $result );
$this->assertContains( $expected_2, $result );
}

Expand All @@ -99,8 +104,8 @@ public function test_can_exclude_files() {

public function test_can_override_exclude_by_include() {
// Overrides include option
$includes = [ 'excluded' ];
$excludes = [ 'excluded/ignored.js' ];
$includes = [ 'excluded/ignored.js' ];
$excludes = [ 'excluded/*.js' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'foo/bar/excluded/ignored.js';
$this->assertContains( $expected, $result );
Expand Down