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

Add UnneededRegularExpression #25

Merged
merged 1 commit into from
Jan 2, 2018
Merged

Add UnneededRegularExpression #25

merged 1 commit into from
Jan 2, 2018

Conversation

wata727
Copy link
Owner

@wata727 wata727 commented Jan 1, 2018

If you only want to test if a variable contains a specific character, you should use strpos() instead of preg_match().

Before

<?php

if (preg_match("/abc/", $var)) { // UnneededRegularExpression: Using `strpos()` instead of `preg_match()` will be faster.
    something($var);
}

After

<?php

if (strpos($var, "abc") !== false) {
    something($var);
}

This tool finds a call to preg_match() that uses unneeded regular expressions.


1. Not using `matches` variables.
2. Not using a pattern including pattern modifiers.
3. Not using a pattern including meta charactors.
Copy link
Owner Author

Choose a reason for hiding this comment

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

"charactors" is a misspelling of "characters"
link

Good catch 👍

@wata727 wata727 merged commit bc00e6d into master Jan 2, 2018
@wata727 wata727 deleted the unneeded_regexp branch January 2, 2018 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant