Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit b6223b8

Browse files
committed
CS/consistency fixes for ServiceManager changes
- Edits comment strings for grammar, spelling, punctuation, formatting, and content. - Adds missing `@throws` annotations. - Reverses several conditionals to make exceptions conditional vs the hot path. - Edits exception messages.
1 parent 589f19b commit b6223b8

File tree

1 file changed

+74
-70
lines changed

1 file changed

+74
-70
lines changed

src/ServiceManager.php

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,17 @@ public function getServiceLocator()
183183
*/
184184
public function get($name)
185185
{
186-
// We start by checking if we have cached the requested service (this
187-
// is the fastest method).
186+
// We start by checking if we have cached the requested service;
187+
// this is the fastest method.
188188
if (isset($this->services[$name])) {
189189
return $this->services[$name];
190190
}
191191

192-
// Determine if the service should be shared
192+
// Determine if the service should be shared.
193193
$sharedService = isset($this->shared[$name]) ? $this->shared[$name] : $this->sharedByDefault;
194194

195195
// We achieve better performance if we can let all alias
196-
// considerations out
196+
// considerations out.
197197
if (! $this->aliases) {
198198
$object = $this->doCreate($name);
199199

@@ -204,27 +204,29 @@ public function get($name)
204204
return $object;
205205
}
206206

207-
// Here we have to deal with requests which may be aliases
207+
// We now deal with requests which may be aliases.
208208
$resolvedName = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
209209

210-
// Can only become true, if the requested service is an shared alias
210+
// The following is only true if the requested service is a shared alias.
211211
$sharedAlias = $sharedService && isset($this->services[$resolvedName]);
212-
// If the alias is configured as shared service, we are done.
212+
213+
// If the alias is configured as a shared service, we are done.
213214
if ($sharedAlias) {
214215
$this->services[$name] = $this->services[$resolvedName];
215216
return $this->services[$resolvedName];
216217
}
217218

218-
// At this point we have to create the object. We use the
219-
// resolved name for that.
219+
// At this point, we have to create the object.
220+
// We use the resolved name for that.
220221
$object = $this->doCreate($resolvedName);
221222

222223
// Cache the object for later, if it is supposed to be shared.
223224
if ($sharedService) {
224225
$this->services[$resolvedName] = $object;
225226
}
226227

227-
// Also do so for aliases, this allows sharing based on service name used.
228+
// Also cache under the alias name; this allows sharing based on the
229+
// service name used.
228230
if ($sharedAlias) {
229231
$this->services[$name] = $object;
230232
}
@@ -237,7 +239,7 @@ public function get($name)
237239
*/
238240
public function build($name, array $options = null)
239241
{
240-
// We never cache when using "build"
242+
// We never cache when using "build".
241243
$name = $this->aliases[$name] ?? $name;
242244
return $this->doCreate($name, $options);
243245
}
@@ -247,30 +249,30 @@ public function build($name, array $options = null)
247249
*/
248250
public function has($name)
249251
{
250-
// Check services and factories first to speedup the most common requests
252+
// Check services and factories first to speedup the most common requests.
251253
if (isset($this->services[$name]) || isset($this->factories[$name])) {
252254
return true;
253255
}
254256

255-
// Check abstract factories next
257+
// Check abstract factories next.
256258
foreach ($this->abstractFactories as $abstractFactory) {
257259
if ($abstractFactory->canCreate($this->creationContext, $name)) {
258260
return true;
259261
}
260262
}
261263

262-
// If $name is no alias, we are done
264+
// If $name is not an alias, we are done.
263265
if (! isset($this->aliases[$name])) {
264266
return false;
265267
}
266268

267-
// Finally check aliases
269+
// Check aliases.
268270
$resolvedName = $this->aliases[$name];
269271
if (isset($this->services[$resolvedName]) || isset($this->factories[$resolvedName])) {
270272
return true;
271273
}
272274

273-
// Check abstract factories on $resolvedName also
275+
// Check abstract factories on the $resolvedName as well.
274276
foreach ($this->abstractFactories as $abstractFactory) {
275277
if ($abstractFactory->canCreate($this->creationContext, $resolvedName)) {
276278
return true;
@@ -340,8 +342,8 @@ public function getAllowOverride()
340342
*/
341343
public function configure(array $config)
342344
{
343-
// This is a bulk update/initial configuration
344-
// So we check all definitions upfront
345+
// This is a bulk update/initial configuration,
346+
// so we check all definitions up front.
345347
$this->validateServiceNames($config);
346348

347349
if (isset($config['services'])) {
@@ -386,7 +388,7 @@ public function configure(array $config)
386388
// instantiate them to avoid checks during service construction.
387389
if (isset($config['abstract_factories'])) {
388390
$abstractFactories = $config['abstract_factories'];
389-
// $key not needed, but foreach faster
391+
// $key not needed, but foreach is faster than foreach + array_values.
390392
foreach ($abstractFactories as $key => $abstractFactory) {
391393
$this->resolveAbstractFactoryInstance($abstractFactory);
392394
}
@@ -406,14 +408,16 @@ public function configure(array $config)
406408
*
407409
* @param string $alias
408410
* @param string $target
411+
* @throws ContainerModificationsNotAllowedException if $alias already
412+
* exists as a service and overrides are disallowed.
409413
*/
410414
public function setAlias($alias, $target)
411415
{
412-
if (! isset($this->services[$alias]) || $this->allowOverride) {
413-
$this->mapAliasToTarget($alias, $target);
414-
return;
416+
if (isset($this->services[$alias]) && ! $this->allowOverride) {
417+
throw ContainerModificationsNotAllowedException::fromExistingService($alias);
415418
}
416-
throw ContainerModificationsNotAllowedException::fromExistingService($alias);
419+
420+
$this->mapAliasToTarget($alias, $target);
417421
}
418422

419423
/**
@@ -422,14 +426,16 @@ public function setAlias($alias, $target)
422426
* @param string $name Service name
423427
* @param null|string $class Class to which to map; if omitted, $name is
424428
* assumed.
429+
* @throws ContainerModificationsNotAllowedException if $name already
430+
* exists as a service and overrides are disallowed.
425431
*/
426432
public function setInvokableClass($name, $class = null)
427433
{
428-
if (! isset($this->services[$name]) || $this->allowOverride) {
429-
$this->createAliasesAndFactoriesForInvokables([$name => $class ?? $name]);
430-
return;
434+
if (isset($this->services[$name]) && ! $this->allowOverride) {
435+
throw ContainerModificationsNotAllowedException::fromExistingService($name);
431436
}
432-
throw ContainerModificationsNotAllowedException::fromExistingService($name);
437+
438+
$this->createAliasesAndFactoriesForInvokables([$name => $class ?? $name]);
433439
}
434440

435441
/**
@@ -438,14 +444,16 @@ public function setInvokableClass($name, $class = null)
438444
* @param string $name Service name
439445
* @param string|callable|Factory\FactoryInterface $factory Factory to which
440446
* to map.
447+
* @throws ContainerModificationsNotAllowedException if $name already
448+
* exists as a service and overrides are disallowed.
441449
*/
442450
public function setFactory($name, $factory)
443451
{
444-
if (! isset($this->services[$name]) || $this->allowOverride) {
445-
$this->factories[$name] = $factory;
446-
return;
452+
if (isset($this->services[$name]) && ! $this->allowOverride) {
453+
throw ContainerModificationsNotAllowedException::fromExistingService($name);
447454
}
448-
throw ContainerModificationsNotAllowedException::fromExistingService($name);
455+
456+
$this->factories[$name] = $factory;
449457
}
450458

451459
/**
@@ -463,7 +471,8 @@ public function mapLazyService($name, $class = null)
463471
/**
464472
* Add an abstract factory for resolving services.
465473
*
466-
* @param string|Factory\AbstractFactoryInterface $factory Service name
474+
* @param string|Factory\AbstractFactoryInterface $factory Abstract factory
475+
* instance or class name.
467476
*/
468477
public function addAbstractFactory($factory)
469478
{
@@ -497,29 +506,32 @@ public function addInitializer($initializer)
497506
*
498507
* @param string $name Service name
499508
* @param array|object $service
509+
* @throws ContainerModificationsNotAllowedException if $name already
510+
* exists as a service and overrides are disallowed.
500511
*/
501512
public function setService($name, $service)
502513
{
503-
if (! isset($this->services[$name]) || $this->allowOverride) {
504-
$this->services[$name] = $service;
505-
return;
514+
if (isset($this->services[$name]) && ! $this->allowOverride) {
515+
throw ContainerModificationsNotAllowedException::fromExistingService($name);
506516
}
507-
throw ContainerModificationsNotAllowedException::fromExistingService($name);
517+
$this->services[$name] = $service;
508518
}
509519

510520
/**
511521
* Add a service sharing rule.
512522
*
513523
* @param string $name Service name
514524
* @param boolean $flag Whether or not the service should be shared.
525+
* @throws ContainerModificationsNotAllowedException if $name already
526+
* exists as a service and overrides are disallowed.
515527
*/
516528
public function setShared($name, $flag)
517529
{
518-
if (! isset($this->services[$name]) || $this->allowOverride) {
519-
$this->shared[$name] = (bool) $flag;
520-
return;
530+
if (isset($this->services[$name]) && ! $this->allowOverride) {
531+
throw ContainerModificationsNotAllowedException::fromExistingService($name);
521532
}
522-
throw ContainerModificationsNotAllowedException::fromExistingService($name);
533+
534+
$this->shared[$name] = (bool) $flag;
523535
}
524536

525537
/**
@@ -608,9 +620,9 @@ private function createDelegatorFromName($name, array $options = null)
608620
if (! is_callable($delegatorFactory)) {
609621
if (is_string($delegatorFactory)) {
610622
throw new ServiceNotCreatedException(sprintf(
611-
'An invalid delegator factory was registered; resolved to class or function "%s" '
612-
. 'which does not exist; please provide a valid function name or class name resolving '
613-
. 'to an implementation of %s',
623+
'An invalid delegator factory was registered; resolved to class or function "%s"'
624+
. ' which does not exist; please provide a valid function name or class name resolving'
625+
. ' to an implementation of %s',
614626
$delegatorFactory,
615627
DelegatorFactoryInterface::class
616628
));
@@ -762,64 +774,57 @@ private function validateServiceNames(array $config)
762774

763775
if (isset($config['services'])) {
764776
foreach ($config['services'] as $service => $_) {
765-
if (! isset($this->services[$service]) || $this->allowOverride) {
766-
continue;
777+
if (isset($this->services[$service]) && ! $this->allowOverride) {
778+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
767779
}
768-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
769780
}
770781
}
771782

772783
if (isset($config['aliases'])) {
773784
foreach ($config['aliases'] as $service => $_) {
774-
if (! isset($this->services[$service]) || $this->allowOverride) {
775-
continue;
785+
if (isset($this->services[$service]) && ! $this->allowOverride) {
786+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
776787
}
777-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
778788
}
779789
}
780790

781791
if (isset($config['invokables'])) {
782792
foreach ($config['invokables'] as $service => $_) {
783-
if (! isset($this->services[$service]) || $this->allowOverride) {
784-
continue;
793+
if (isset($this->services[$service]) && ! $this->allowOverride) {
794+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
785795
}
786-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
787796
}
788797
}
789798

790799
if (isset($config['factories'])) {
791800
foreach ($config['factories'] as $service => $_) {
792-
if (! isset($this->services[$service]) || $this->allowOverride) {
793-
continue;
801+
if (isset($this->services[$service]) && ! $this->allowOverride) {
802+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
794803
}
795-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
796804
}
797805
}
798806

799807
if (isset($config['delegators'])) {
800808
foreach ($config['delegators'] as $service => $_) {
801-
if (! isset($this->services[$service]) || $this->allowOverride) {
802-
continue;
809+
if (isset($this->services[$service]) && ! $this->allowOverride) {
810+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
803811
}
804-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
805812
}
806813
}
807814

808815
if (isset($config['shared'])) {
809816
foreach ($config['shared'] as $service => $_) {
810-
if (! isset($this->services[$service]) || $this->allowOverride) {
811-
continue;
817+
if (isset($this->services[$service]) && ! $this->allowOverride) {
818+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
812819
}
813-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
814820
}
815821
}
816822

817823
if (isset($config['lazy_services']['class_map'])) {
818824
foreach ($config['lazy_services']['class_map'] as $service => $_) {
819-
if (! isset($this->services[$service]) || $this->allowOverride) {
820-
continue;
825+
if (isset($this->services[$service]) && ! $this->allowOverride) {
826+
throw ContainerModificationsNotAllowedException::fromExistingService($service);
821827
}
822-
throw ContainerModificationsNotAllowedException::fromExistingService($service);
823828
}
824829
}
825830
}
@@ -900,20 +905,19 @@ private function mapAliasesToTargets()
900905
private function resolveAbstractFactoryInstance($abstractFactory)
901906
{
902907
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
903-
// cached string
908+
// Cached string factory name
904909
if (! isset($this->cachedAbstractFactories[$abstractFactory])) {
905910
$this->cachedAbstractFactories[$abstractFactory] = new $abstractFactory();
906911
}
907912

908913
$abstractFactory = $this->cachedAbstractFactories[$abstractFactory];
909914
}
910915

911-
if ($abstractFactory instanceof Factory\AbstractFactoryInterface) {
912-
$abstractFactoryObjHash = spl_object_hash($abstractFactory);
913-
$this->abstractFactories[$abstractFactoryObjHash] = $abstractFactory;
914-
return;
916+
if (! $abstractFactory instanceof Factory\AbstractFactoryInterface) {
917+
throw InvalidArgumentException::fromInvalidAbstractFactory($abstractFactory);
915918
}
916919

917-
throw InvalidArgumentException::fromInvalidAbstractFactory($abstractFactory);
920+
$abstractFactoryObjHash = spl_object_hash($abstractFactory);
921+
$this->abstractFactories[$abstractFactoryObjHash] = $abstractFactory;
918922
}
919923
}

0 commit comments

Comments
 (0)