-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Optimize MutableCreationOptionsInterface capability #4408
Optimize MutableCreationOptionsInterface capability #4408
Conversation
protected function createServiceViaCallback($callable, $cName, $rName) | ||
{ | ||
do { | ||
if (!is_array($callable)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please factorize all those if into one single if (or at least group them in two if if you prefer) as you break in all cases.
I'm not good at English, so I'm sorry if I misunderstood you. |
What I told you is that all your if break, so you should factorize the if. For instance, replace this: if (A) {
break;
}
if (B) {
break;
} by: if (A || B) {
break;
} it's more efficient and more compact :). |
Oh I'm sorry for misunderstanding you.
The habit of these is efficient to debug with step-in tools .And more easy to check lines, easy to edit. if (!isset($factory) || !is_object($factory)) {
break;
}
if (!$factory instanceof MutableCreationOptionsInterface) {
break;
}
if (null === $this->creationOptions) {
break;
}
if (is_array($this->creationOptions) && empty($this->creationOptions)) {
break;
} But My habit is trivial matter. if (is_object($callable)) {
$factory = $callable;
} elseif (is_array($callable)) {
$factory = reset($callable);
}
if (isset($factory) && is_object($factory) && ($factory instanceof MutableCreationOptionsInterface)
&& (null !== $this->creationOptions) && is_array($this->creationOptions) && !empty($this->creationOptions)){
$factory->setCreationOptions($this->creationOptions);
} |
これわすごいね! Service manager is a component that need to be efficient as it's used nearly everywhere, so if we can avoid some if... it's always good ;-) |
@bakura10 You did typo! s/これわすごいね!/これはすごいね!/ (´・ω・`) |
Oh yeah... How did I make this mistake, this is the only thing irregular in japanese :D. |
} | ||
|
||
if (isset($factory) && is_object($factory) && ($factory instanceof MutableCreationOptionsInterface) | ||
&& (null !== $this->creationOptions) && is_array($this->creationOptions) && !empty($this->creationOptions)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're testing $factory
against an interface, you can remove is_object()
, and you don't need to check if null !== $this->creationOptions
because is_array()
will return false if it is null.
Thanks all. I used to check by is_object in php old version but now not necessary. I removed it. (null !== $this->creationOptions) is used in current And so, should I remake the 6 commits to 2 ? |
That one line is slightly different and I think it is right the way it is.
That will be good since it will make the git history cleaner. |
I have no idea about this.
|
That have been happening for some time, I'm not sure why but you don't need to worry about that, it is just a Travis issue. |
…nOptions Optimize MutableCreationOptionsInterface capability
- re-flow conditionals, one condition per line; brace onto next line for multi-line conditionals. - add comments where needed - add imports where needed
&& is_array($this->creationOptions) && !empty($this->creationOptions)){ | ||
$factory->setCreationOptions($this->creationOptions); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memo: if $factory is not object, 'instanceof' raises fatal error. To do fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That actually does not happen, at least with php 5.3 and up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that really?
Document saids,
Constants, however, are not allowed.
http://php.net/manual/en/language.operators.type.php#example-130
I tested it.
> php -v
PHP 5.4.13 (cli) (built: Mar 15 2013 02:07:14)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.2, Copyright (c) 2002-2013, by Derick Rethans
<?php
var_dump((false instanceof \StdClass));
raises
PHP Fatal error: instanceof expects an object instance, constant given in - on line 2
Please try it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, but the code is not using a constant, it is using a variable ($factory), so there's no way of this to happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many thanks.
I was anxious about it . Now I see.
…FactoriesWithMutableCreationOptions Optimize MutableCreationOptionsInterface capability
- re-flow conditionals, one condition per line; brace onto next line for multi-line conditionals. - add comments where needed - add imports where needed
This enables Not only the factories but also the abstract factories implementing MutableCreationOptionsInterface to be able to get creation options.