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

Commit d0b38fb

Browse files
author
fhein
committed
Unified redundant exception messages for invalid initializers and
invalid abstract factories. Moved them to Named Constructors of InvalidArgumentException. Adjusted test which checks the wording of the exception messages to reflect the change.
1 parent 25ac27f commit d0b38fb

File tree

3 files changed

+34
-62
lines changed

3 files changed

+34
-62
lines changed

src/Exception/InvalidArgumentException.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,32 @@
88
namespace Zend\ServiceManager\Exception;
99

1010
use InvalidArgumentException as SplInvalidArgumentException;
11+
use Zend\ServiceManager\Initializer\InitializerInterface;
12+
use Zend\ServiceManager\AbstractFactoryInterface;
1113

1214
/**
1315
* @inheritDoc
1416
*/
1517
class InvalidArgumentException extends SplInvalidArgumentException implements ExceptionInterface
1618
{
19+
public static function fromInvalidInitializer($initializer)
20+
{
21+
return new self(sprintf(
22+
'An invalid initializer was registered. Expected a callable, or an instance of '
23+
. '(or valid class name or function name resolving to) "%s", '
24+
. 'but "%s" was received',
25+
InitializerInterface::class,
26+
(is_object($initializer) ? get_class($initializer) : gettype($initializer))
27+
));
28+
}
29+
30+
public static function fromInvalidAbstractFactory($abstractFactory)
31+
{
32+
return new self(sprintf(
33+
'An invalid abstract factory was registered. Expected an instance of or a '
34+
. 'valid class name resolving to an implementation of "%s", but "%s" was received.',
35+
AbstractFactoryInterface::class,
36+
(is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory))
37+
));
38+
}
1739
}

src/ServiceManager.php

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -522,46 +522,6 @@ public function setShared($name, $flag)
522522
throw ContainerModificationsNotAllowedException::fromExistingService($name);
523523
}
524524

525-
/**
526-
* Instantiate initializers for to avoid checks during service construction.
527-
*
528-
* @param string[]|Initializer\InitializerInterface[]|callable[] $initializers
529-
*
530-
* @return void
531-
*/
532-
private function resolveInitializer($initializer)
533-
{
534-
if (is_string($initializer) && class_exists($initializer)) {
535-
$initializer = new $initializer();
536-
}
537-
538-
if (is_callable($initializer)) {
539-
$this->initializers[] = $initializer;
540-
return;
541-
}
542-
543-
// Error condition; let's find out why.
544-
545-
if (is_string($initializer)) {
546-
throw new InvalidArgumentException(sprintf(
547-
'An invalid initializer was registered; resolved to class or function "%s" '
548-
. 'which does not exist; please provide a valid function name or class '
549-
. 'name resolving to an implementation of %s',
550-
$initializer,
551-
Initializer\InitializerInterface::class
552-
));
553-
}
554-
555-
// Otherwise, we have an invalid type.
556-
throw new InvalidArgumentException(sprintf(
557-
'An invalid initializer was registered. Expected a callable, or an instance of '
558-
. '(or string class name resolving to) "%s", '
559-
. 'but "%s" was received',
560-
Initializer\InitializerInterface::class,
561-
(is_object($initializer) ? get_class($initializer) : gettype($initializer))
562-
));
563-
}
564-
565525
/**
566526
* Instantiate initializers for to avoid checks during service construction.
567527
*
@@ -572,7 +532,16 @@ private function resolveInitializer($initializer)
572532
private function resolveInitializers(array $initializers)
573533
{
574534
foreach ($initializers as $initializer) {
575-
$this->resolveInitializer($initializer);
535+
if (is_string($initializer) && class_exists($initializer)) {
536+
$initializer = new $initializer();
537+
}
538+
539+
if (is_callable($initializer)) {
540+
$this->initializers[] = $initializer;
541+
return;
542+
}
543+
544+
throw InvalidArgumentException::fromInvalidInitializer($initializer);
576545
}
577546
}
578547

@@ -947,25 +916,6 @@ private function resolveAbstractFactoryInstance($abstractFactory)
947916
return;
948917
}
949918

950-
// Error condition; let's find out why.
951-
952-
// If we still have a string, we have a class name that does not resolve
953-
if (is_string($abstractFactory)) {
954-
throw new InvalidArgumentException(sprintf(
955-
'An invalid abstract factory was registered; resolved to class "%s" '
956-
. 'which does not exist; please provide a valid class name resolving '
957-
. 'to an implementation of %s',
958-
$abstractFactory,
959-
AbstractFactoryInterface::class
960-
));
961-
}
962-
963-
// Otherwise, we have an invalid type.
964-
throw new InvalidArgumentException(sprintf(
965-
'An invalid abstract factory was registered. Expected an instance of "%s", '
966-
. 'but "%s" was received',
967-
AbstractFactoryInterface::class,
968-
(is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory))
969-
));
919+
throw InvalidArgumentException::fromInvalidAbstractFactory($abstractFactory);
970920
}
971921
}

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public function testCanSpecifyInitializerUsingStringViaConfiguration()
545545
public function invalidInitializers()
546546
{
547547
$factories = $this->invalidFactories();
548-
$factories['non-class-string'] = ['non-callable-string', 'valid function name or class name'];
548+
$factories['non-class-string'] = ['non-callable-string', 'valid class name or function name'];
549549
return $factories;
550550
}
551551

0 commit comments

Comments
 (0)