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.
Hello,
I have implemented a number of useful (but backwards-incompatible) fixes and improvements. They are included in the changelog. They are also listed (with brief explanations) below.
Ensure generated URLs are properly formatted (via
rawurlencode()
)Before this change, generated URLs were technically incorrect if a tracking number had spaces in it. Browsers can typically correct for this by encoding the URL's special characters under the hood, but A) not all browsers do this, and B) any programmatic URL verification (e.g. when using Symfony's
UrlType
field) would fail.I opted for
rawurlencode()
instead ofurlencode()
because some websites (e.g. DHL's tracking website) interpret plus signs (+) literally.Sort regular expressions by specificity to decrease likelihood of partial matches when using
LinkifyTracking::linkify()
.If two patterns exist—
ABC
andABCABC
—the first pattern would previously be matched earlier, resulting in the latter not being found. Note that I only loosely improved this—I suspect there is still overlap, especially when you consider that carriers can have multiple patterns. I spent about two seconds looking at it and then moved on.Prevent double linkification when using
LinkifyTracking::linkify()
.Similar to the previous issue, if one tracking number was linkified but then a subsequent pattern still matched it, the links would be created twice. Now, each replacement is added to a "linkification queue" that stores the replacements that need to be made. To prevent duplication, each match is temporarily replaced with
%s
. Once all matches have been made, the replacement values stored in the linkification queue are used to finalize the result.Update DHL tracking number patterns based on information available here. Due to ambiguity, some patterns are intentionally broad.
Title says all. Some could probably be improved to reduce redundancy, but that's a job for another day. Also, I think DHL tracks different types of shipments via different URLs, so we should look into that too.
...
Future changes should probably include adding some tests, adding more specific type hints/annotations, fixing pattern specificity, fixing pattern redundancy, and fixing DHL tracking URLs since I believe they have multiple. We should probably add a minimum PHP requirement to Composer as well. I did none of these things.
Let me know if you have any questions.