-
Notifications
You must be signed in to change notification settings - Fork 12
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
Refactor getenv access using factory methods #7
Conversation
This definitely can be a nicer interface and I use similar things myself. The BC Break is the main reason I didn't take a different approach. Defining a __construct method whose only parameters are there primarily for mocking abilities can lead to some annoying BC breaks as well if some new required parameter is added (as the required parameter should likely be first in the constructors arguments ahead of the "mock" parameters). This approach here also leaves a method that can only be tested by modifying the environment (namely @bcremer, what are your thoughts on handling the filesystem mocking? Would you still recommend mocking getenv in this manner if Isolator was already being used to mock the filesystem? |
It wouldn't be a BC break (and would be a cleaner API, IMO) if instead of creating factory methods you made the constructor something like this: private static $envKeys = array(
'USER',
'HOME',
'XDG_CONFIG_HOME',
'XDG_DATA_HOME',
'XDG_CACHE_HOME',
'XDG_CONFIG_DIRS',
'XDG_DATA_DIRS',
'XDG_RUNTIME_DIR',
);
public function __construct(array $env = array())
{
foreach (self::$envKeys as $key) {
$this->env[$key] = array_key_exists($key, $env) ? $env[$key] : getenv($key);
}
} That would let you mock as many or few of the environment variables as you want. |
What I meant by BC break is this. Say you have your |
12a0c34
to
9e4ef68
Compare
@bobthecow I like the the idea of partial env overrides using the constructor, I created a separate PR for that, see #9. @nubs BC should not be an issue right now. This lib is not yet marked as stable. For future BC stability an optional $config array can be added when needed: The scope of the Xdg class should stay narrow. |
I close this PR in favour of #9. |
This is an alternative to PR #6.
This contains a compability break:
Old:
New:
This makes it possible to test the class without mocking the
getenv
function at all.