Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Optimize MutableCreationOptionsInterface capability #4408

Conversation

noopable
Copy link
Contributor

@noopable noopable commented May 3, 2013

This enables Not only the factories but also the abstract factories implementing MutableCreationOptionsInterface to be able to get creation options.

protected function createServiceViaCallback($callable, $cName, $rName)
{
do {
if (!is_array($callable)) {
Copy link
Contributor

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.

@noopable
Copy link
Contributor Author

noopable commented May 4, 2013

I'm not good at English, so I'm sorry if I misunderstood you.
The method (createServiceViaCallback) is a protected method. It is only called from 'createFromFactory' and 'createFromAbstractFactory'. And it is a common gateway to create an object by the *factory. These call it by a callback array. And the creationOptions is only set at AbstractPluginManager::get if a client needs to set constructor options.
Thus I think this patch(or hotfix) will not cover all cases but covering the known cases.
My first commit is a step-by-step approach getting ability to get the creationOptions with a determinate minimum side effect.
My second commit enables a callable object to get creationOptions.
Are there any other necessary things?

@bakura10
Copy link
Contributor

bakura10 commented May 5, 2013

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 :).

@noopable
Copy link
Contributor Author

noopable commented May 5, 2013

Oh I'm sorry for misunderstanding you.
But is it a code standard problem or a project rule that you pointed out ?
Or too ugly?
I usually chunk code-blocks with the aim of preserving the condition intent.
Each if-line is

  • Is it factory object or not?
  • Does the interface want to get creationOptions?
  • The client want to use creation option or not ?
  • Is it valid the specified creationOptions ?

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.
I can remake these if-clause to the one-line.
Is that better?

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); 
}

@bakura10
Copy link
Contributor

bakura10 commented May 5, 2013

これわすごいね!

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 ;-)

@sasezaki
Copy link
Contributor

sasezaki commented May 5, 2013

@bakura10 You did typo! s/これわすごいね!/これはすごいね!/ (´・ω・`)

@bakura10
Copy link
Contributor

bakura10 commented May 5, 2013

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)){
Copy link
Contributor

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.

@noopable
Copy link
Contributor Author

noopable commented May 5, 2013

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
https://github.com/zendframework/zf2/blob/master/library/Zend/ServiceManager/AbstractPluginManager.php#L192
Can I rewrite it on the way?

And so, should I remake the 6 commits to 2 ?

@iquabius
Copy link
Contributor

iquabius commented May 6, 2013

(null !== $this->creationOptions) is used in current
https://github.com/zendframework/zf2/blob/master/library/Zend/ServiceManager/AbstractPluginManager.php#L192
Can I rewrite it on the way?

That one line is slightly different and I think it is right the way it is.

And so, should I remake the 6 commits to 2 ?

That will be good since it will make the git history cleaner.

@noopable
Copy link
Contributor Author

noopable commented May 6, 2013

I have no idea about this.
Travis saids:

BUILD FAILED

/home/travis/build/zendframework/zf2/build.xml:15: Can't get http://cs.sensiolabs.org/get/php-cs-fixer.phar to
/home/travis/build/zendframework/zf2/php-cs-fixer.phar

@iquabius
Copy link
Contributor

iquabius commented May 6, 2013

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.

weierophinney added a commit that referenced this pull request May 6, 2013
…nOptions

Optimize MutableCreationOptionsInterface capability
weierophinney added a commit that referenced this pull request May 6, 2013
- re-flow conditionals, one condition per line; brace onto next line for
  multi-line conditionals.
- add comments where needed
- add imports where needed
@ghost ghost assigned weierophinney May 6, 2013
weierophinney added a commit that referenced this pull request May 6, 2013
&& is_array($this->creationOptions) && !empty($this->creationOptions)){
$factory->setCreationOptions($this->creationOptions);
}

Copy link
Contributor Author

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?

Copy link
Contributor

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!

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@noopable noopable deleted the pr-FactoriesWithMutableCreationOptions branch December 13, 2013 13:07
weierophinney added a commit to zendframework/zend-servicemanager that referenced this pull request May 15, 2015
…FactoriesWithMutableCreationOptions

Optimize MutableCreationOptionsInterface capability
weierophinney added a commit to zendframework/zend-servicemanager that referenced this pull request May 15, 2015
- re-flow conditionals, one condition per line; brace onto next line for
  multi-line conditionals.
- add comments where needed
- add imports where needed
weierophinney added a commit to zendframework/zend-servicemanager that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-servicemanager that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants