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

Allow to pass NULL for a parameter with type hinting and default value NULL #30

Merged
merged 2 commits into from
Jan 8, 2015

Conversation

daniel-meister
Copy link
Contributor

Whenever a function parameter with a type hint has a default value of NULL, passing NULL instead of a valid class instance is allowed (according the the PHP documentation at http://php.net/manual/en/language.oop5.typehinting.php). On the other hand, the instanceof operator does of course not recognize NULL as an instance of a given class.

This pull request would allow to pass NULL via the contructParams whenever the default value is set to NULL; I'm not sure how to properly solve this also for the substitutions. It should be easy to forward-port this to master; unfortunately, I don't have a PHP 5.6 setup readily available at the moment.

TRPB added a commit that referenced this pull request Jan 8, 2015
Allow to pass NULL for a parameter with type hinting and default value NULL
@TRPB TRPB merged commit af0b813 into Level-2:PHP5.4---PHP5.5 Jan 8, 2015
@TRPB
Copy link
Member

TRPB commented Jan 8, 2015

For substitutions, the fix is simple. I'll add a commit shortly after further testing

if ($subs && isset($subs[$class]) {}

isset bizarrely returns false if the key is set but the value is null ( http://php.net/manual/en/function.isset.php )

changing isset to array_key_exists will allow:

$rule = new \Dice\Rule;
$rule->substitutions['B'] = null;
$this->dice->addRule('MethodWithDefaultNull', $rule);
$obj = $this->dice->create('MethodWithDefaultNull');

TRPB added a commit that referenced this pull request Jan 8, 2015
TRPB added a commit that referenced this pull request Jan 8, 2015
TRPB added a commit that referenced this pull request Jan 8, 2015
garrettw added a commit to garrettw/Dice that referenced this pull request Jan 8, 2015
@TRPB TRPB mentioned this pull request Jan 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants