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

TextStrings: add new methods for working with interpolated variables #321

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 6, 2022

This introduces three new methods to the PHPCSUtils\Utils\TextStrings class.

Methods:

  • getEmbeds() - to retrieve embedded variables and expressions from an arbitrary string, typically the contents of a T_DOUBLE_QUOTED_STRING or T_HEREDOC. Returns an array of the variables/expression recognized as strings with the offset within the string at which the embed was found as the keys.
  • stripEmbeds() - strip embedded variables/expression from an arbitrary string, typically the contents of a T_DOUBLE_QUOTED_STRING or T_HEREDOC. Returns the adjusted string.
  • getStripEmbeds() - the actual "worker" method which does both of the above in one go.
    This method is public for those rare cases where both the embeds as well as the stripped text is needed.

These methods support all types of variable/expression embedding as supported by PHP. Please report edge cases if you run into one.

These methods are much improved versions of the methods which have previously been battle-tested in various forms in the PHPCompatibility and the WordPressCS standards.

Includes extensive, dedicated unit tests.

This introduces three new methods to the PHPCSUtils\Utils\TextStrings class.

Methods:
* `getEmbeds()` - to retrieve embedded variables and expressions from an arbitrary string, typically the contents of a `T_DOUBLE_QUOTED_STRING` or `T_HEREDOC`. Returns an array of the variables/expression recognized as strings with the offset within the string at which the embed was found as the keys.
* `stripEmbeds()` - strip embedded variables/expression from an arbitrary string, typically the contents of a `T_DOUBLE_QUOTED_STRING` or `T_HEREDOC`. Returns the adjusted string.
* `getStripEmbeds()` - the actual "worker" method which does both of the above in one go.
    This method is `public` for those rare cases where both the embeds as well as the stripped text is needed.

These methods support all types of variable/expression embedding as supported by PHP. Please report edge cases if you run into one.

These methods are much improved versions of the methods which have previously been battle-tested in various forms in the PHPCompatibility and the WordPressCS standards.

Includes extensive, dedicated unit tests.
@jrfnl
Copy link
Member Author

jrfnl commented Jun 7, 2022

Note: the code coverage going down is a bug in Coveralls reporting. The build on the develop branch after the merge should confirm that code coverage has actually remained the same or even gone up.

@jrfnl jrfnl merged commit 692c248 into develop Jun 7, 2022
@jrfnl jrfnl deleted the textstrings/new-methods-handling-embedded-vars-and-expressions branch June 7, 2022 09:18
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.

None yet

1 participant