refactor: add wrapper around preg_match
#2319
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
Description
The default behavior of
preg_match
is to silent any parsing error.Therefore, I created a this wrapper is here to make sure that it properly throws an exception in case of any
preg_match
issue.The origin of this PR comes from an issue we had at work. We were trying to delete a block in a .docx file (75Kb) and nothing was happening, despite the fact that the method issued correctly.
We made an introspection of the
document.xml
and discovered that the methodTemplateProcessor::deleteBlock
doesn't work, it seems that the regex is not valid (there are already a couple of issues about that: #2051, #2221, #2309, #2157), we are now usingTemplateProcessor::cloneblock
instead.Then, we narrowed down the issue to the
preg_match
function and succeeded to reproduce the issue with a simple xml document.This is were I discovered the function
preg_last_error
and noticed that the issue was related to thepcre.backtrack_limit
PHP directive. Once we updated that directive from1000000
to2000000
it worked.We lost less or more half a day on this issue, and this PR should help some people to see clearer when something goes wrong.
Could you please let me know if this PR is legit and if I should invest more time in it?
Nice to have: ideally, it would be a good think to invest time into an external contrib library that does
preg_match
correctly with the proper Exception handling.Thanks
Checklist:
composer run-script check --timeout=0
and no errors were reported