Namespace value() helper to avoid conflicts with host applications#11
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Namespaced functions cannot collide with global ones, and Composer's files autoload guarantees single-load, so the guard is unnecessary. Removing it also makes the function definition unconditionally coverable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
namespace CoyoteCert\Support;tosrc/Support/helpers.phpso thevalue()function lives atCoyoteCert\Support\value()instead of the global namespacefunction_exists()guard to check__NAMESPACE__ . '\\value'rather than the bare string'value'use Closure;import (required once the file has a namespace declaration)use function CoyoteCert\Support\value;toHelpersTest.phpso the test file can still resolve the function without a namespace of its ownWhy
As a Composer package, defining
value()in the global namespace collides with the identically-named helper shipped by Laravel, Illuminate, and other popular packages. Any host application that already defines or importsvalue()would trigger a fatal redeclaration error at autoload time. Namespacing the function eliminates the conflict entirely.Arr.php(the only internal caller) required no changes — it already lives inCoyoteCert\Support, so PHP resolves the unqualifiedvalue()call to the same namespace automatically.Test plan
vendor/bin/pest tests/Unit/Support/passes (134 assertions, all green)Arr.php— verified via existingArrTestsuite