-
Notifications
You must be signed in to change notification settings - Fork 175
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
[libraries/Utility] Add startsWith, endsWith string functions to Utility (with example) #2965
[libraries/Utility] Add startsWith, endsWith string functions to Utility (with example) #2965
Conversation
static function endsWith($haystack, $needle) | ||
{ | ||
return substr($haystack, -strlen($needle)) === $needle; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these would make more sense in a new Strings class, rather than adding more stuff to the already overloaded Utility class.
@johnsaigle @driusan I think the current way these are implemented is problematic for a couple reasons:
I wouldn't be opposed to create a new String.class.inc and have those there (they will be easier to find) the same PR should have functions such as contains(needle,haystack) and postion(needle,haystack) But i would insist on these being used multiple places in the code before the PR is merged |
@ridz1208 I am in favour of a Strings class to make things simpler. I added these not so much because of new developers but because of PHP being pretty inconsistent. In fact I added them because of #2953. Misuse of Given that a lot of code is copy-pasted as people learn how to e.g. write modules I think having some little utility functions like this help improve code readability and can help you think more clearly while writing -- and the alternative is keeping track of every little detail of PHP weirdness which is pretty tough. |
@johnsaigle I don't make the distinction between new/old and jr/sr developers... everyone makes mistakes, yes including dave !! But Dave's code got reviewed and merged so there is at least one other developer who also failed to catch the error. The issue in my opinion is not PHP being inconsistent (even if that does not help) but improper management of PRs which is an issue we have been tackling lately and I think we made significand improvements. Tha said, I'm not THAT opposed to these chnages, it is an improvement... will keep this PR open for further development |
add starts with and endswith to Utility add example phpcs
Now that I look this over ain, I don't know that this is actually fixing the problem. Someone unfamiliar with the code would still need to read this over to see if it's |
Create syntax sugar for
substr
to prevent developer errors with string comparisonAdd startsWith string comparison function to Utility
Add endsWith string comparison function to Utility
see [Help Editor] Fix incorrect substr syntax #2966 for an example of this error