zendframework/zf2 — pull request #3577 by Deserved (2013-01-27 23:21:36+00:00)
Title: canCreate unproper behavior in ServiceManager?

canCreate() method I understand as a helper for create() method, before to create a service, I have to be sure that I can create one. canCreate() method,also looks for service inside both $aliases and $instances containers. This behavior is a little bit misleading as $instances used for services, that already created or not instances at all, or instances that passed to SM straightforward; and aliases they are aliases you never know what behind the scene. So if I would do:

$this->setService($foo, $bar);
$this->canCreate($foo); //will get true
$this->create($foo); //I expect $bar, but will get Exception.

$this->setAlias($foo, $bar);
$this->canCreate($foo); //will get true
$this->create($foo); //I expect $bar, but will get Exception.

To resolve this I would suggest following changes:
1. Do not include $instances and $aliases containers within canCreate() method, it only will check in those containers in which it will be able to create a new instance ($invokableClasses, $factories and $abstract_factories).

2. If Alias was passed to canCreate(), resolve alias and than perform checks.

3. Move $instances and $aliases checks into has() method before canCreate().

In this way, I presume checks will have semantics and single responsibility. Code attached. The only backward incompatibility after this check will be for people that uses SM in this way:

$this->setService($foo, $bar);
$this->canCreate($foo); //after attached changes will return false, use has() instead.