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

Avoid throwing a notice about strpos(): Empty needle when going through include paths #149

Merged
merged 1 commit into from
Aug 13, 2019

Conversation

dac514
Copy link
Contributor

@dac514 dac514 commented Mar 22, 2019

Kept getting this warning when doing --include="*.blade.php", among other things.

Related to #125 and #129

Kept getting this warning when doing `--include="*.blade.php"`, among other things

Related to #125 and #129
@@ -176,7 +176,7 @@ protected static function containsMatchingChildren( SplFileInfo $dir, array $mat
// Or the start of the matcher until the first wildcard matches the start of the path.
if (
( '' !== $root_relative_path && 0 === strpos( $base, $root_relative_path ) ) ||
0 === strpos( $root_relative_path, $base )
( '' !== $base && 0 === strpos( $root_relative_path, $base ) )
Copy link
Member

Choose a reason for hiding this comment

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

@swissspidy This now saves against the thrown notice, but I'm wondering whether it doesn't now exclude valid matches.

If $base is empty, should a match be possible here?

Copy link
Contributor Author

@dac514 dac514 Mar 24, 2019

Choose a reason for hiding this comment

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

The old code, without my change, in context, was:

	if (
		( '' !== $root_relative_path && 0 === strpos( $base, $root_relative_path ) ) ||
		0 === strpos( $root_relative_path, $base )
	) {
		return true;
	}
}
return false;

Written another way:

	if (
		$CONDITION_1 ||
		$CONDITION_2
	) {
		return true;
	}
}
return false;

My change only suppresses the warning:

$CONDITION_2 = strpos( 'node_modules', '' );
var_dump($CONDITION_2);
// false, aka:
$CONDITION_2 = 0 === strpos( 'node_modules', '' );
var_dump($CONDITION_2);
// false
$CONDITION_2 = '' !== '' ;
var_dump($CONDITION_2);
// false

whether it doesn't now exclude valid matches. If $base is empty, should a match be possible here?

My change doesn't change how the code works, it only suppresses the PHP Warning.

If a match is possible, and the testing suite doesn't catch it, then that's beyond the scope of my PR IMHO.

Hope this helps.

Copy link
Member

@schlessera schlessera Apr 1, 2019

Choose a reason for hiding this comment

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

Yes, it was clear how the logic works. However, the PR should ideally be about eliminating the root cause of a bug, not just obfuscating it.

In this case, the root cause is that one possible scenario was apparently not being considered by the current logic. The notice as such is valuable as it shows us we apparently forgot something. If we now just hide the notice, the possible bug might still be in there and forgotten because the symptom is being hidden.

This is why I'd like some feedback by @swissspidy, who built the original code, as I'm unsure what an empty base in this case would signify.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm I think my initial work was pretty much rewritten in #104. So maybe @herregroen knows more?

The warning implies that $path_or_file is an empty string and I wonder why this happens. If it's because of some edge case thing I don't think just breaking early would cause any troubles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this helps? I can reproduce every time by running:

wp i18n make-pot . languages/plugin.pot --include="*.blade.php" --exclude="vendor,symbionts" --headers='{"Report-Msgid-Bugs-To":"https://github.com/namespace/plugin/"}

Copy link
Member

Choose a reason for hiding this comment

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

@swissspidy I think you misread the code. The issue is not that $path_or_file is empty, but that current( explode( '*', $path_or_file ) ) returns an empty string. This happens for a simple example like *.php, as it will explode to [ '', '.php' ].

Copy link
Member

Choose a reason for hiding this comment

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

Ah, indeed! My bad.

@schlessera schlessera requested a review from swissspidy March 23, 2019 10:14
@schlessera
Copy link
Member

I'm not so sure that the logic is actually sound, as we seem to just be hiding the problem with this PR. Nevertheless, it gets rid of the notice, and it doesn't actually break anything that wouldn't already be broken in that case anyway.

So I'll go ahead and merge this for now.

@schlessera schlessera added this to the 2.2.0 milestone Aug 13, 2019
@schlessera schlessera merged commit 502185d into wp-cli:master Aug 13, 2019
@schlessera schlessera changed the title Fix: Warning: strpos(): Empty needle Avoid throwing a notice about strpos(): Empty needle when going through include paths Aug 13, 2019
@markhowellsmead
Copy link

markhowellsmead commented Oct 28, 2019

I'm still getting a warning about "empty needles" when running wp i18n make-pot . languages/sht.pot --include="*.php,*.jsx,*.js" --exclude="assets/gutenberg,assets/plugins,vendor,node_modules"with 2.2.0 in my Theme root folder. The file is generated, but it doesn't contain any of the strings from the JSX files.

@swissspidy
Copy link
Member

@markhowellsmead Could you please create a new issue for that so we can track this? Thanks!

schlessera added a commit that referenced this pull request Jan 6, 2022
Avoid throwing a notice about `strpos(): Empty needle` when going through include paths
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.

4 participants