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

Optimize PHP function calls #196

Merged
merged 2 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/AbstractFactory/ConfigAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface
*/
public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName)
{
if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) {
if (! $container->has('config') || ! \array_key_exists(self::class, $container->get('config'))) {
return false;
}
$config = $container->get('config');
$dependencies = $config[self::class];

return is_array($dependencies) && array_key_exists($requestedName, $dependencies);
return \is_array($dependencies) && \array_key_exists($requestedName, $dependencies);
}

/**
Expand All @@ -41,31 +41,31 @@ public function __invoke(\Interop\Container\ContainerInterface $container, $requ

$config = $container->get('config');

if (! (is_array($config) || $config instanceof ArrayObject)) {
if (! (\is_array($config) || $config instanceof ArrayObject)) {
throw new ServiceNotCreatedException('Config must be an array or an instance of ArrayObject');
}

if (! array_key_exists(self::class, $config)) {
if (! \array_key_exists(self::class, $config)) {
throw new ServiceNotCreatedException('Cannot find a `' . self::class . '` key in the config array');
}

$dependencies = $config[self::class];

if (! is_array($dependencies)
|| ! array_key_exists($requestedName, $dependencies)
|| ! is_array($dependencies[$requestedName])
if (! \is_array($dependencies)
|| ! \array_key_exists($requestedName, $dependencies)
|| ! \is_array($dependencies[$requestedName])
) {
throw new ServiceNotCreatedException('Dependencies config must exist and be an array');
}

$serviceDependencies = $dependencies[$requestedName];

if ($serviceDependencies !== array_values(array_map('strval', $serviceDependencies))) {
$problem = json_encode(array_map('gettype', $serviceDependencies));
if ($serviceDependencies !== \array_values(\array_map('strval', $serviceDependencies))) {
$problem = \json_encode(\array_map('gettype', $serviceDependencies));
throw new ServiceNotCreatedException('Service message must be an array of strings, ' . $problem . ' given');
}

$arguments = array_map([$container, 'get'], $serviceDependencies);
$arguments = \array_map([$container, 'get'], $serviceDependencies);

return new $requestedName(...$arguments);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
}

$type = $parameter->getClass()->getName();
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
$type = $this->aliases[$type] ?? $type;

if (! $container->has($type)) {
throw new ServiceNotFoundException(sprintf(
Expand Down
8 changes: 4 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Config implements ConfigInterface
public function __construct(array $config = [])
{
// Only merge keys we're interested in
foreach (array_keys($config) as $key) {
foreach (\array_keys($config) as $key) {
if (! isset($this->allowedKeys[$key])) {
unset($config[$key]);
}
Expand Down Expand Up @@ -98,12 +98,12 @@ private function merge(array $a, array $b)
foreach ($b as $key => $value) {
if ($value instanceof MergeReplaceKeyInterface) {
$a[$key] = $value->getData();
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
} elseif (isset($a[$key]) || \array_key_exists($key, $a)) {
if ($value instanceof MergeRemoveKey) {
unset($a[$key]);
} elseif (is_int($key)) {
} elseif (\is_int($key)) {
$a[] = $value;
} elseif (is_array($value) && is_array($a[$key])) {
} elseif (\is_array($value) && \is_array($a[$key])) {
$a[$key] = $this->merge($a[$key], $value);
} else {
$a[$key] = $value;
Expand Down
60 changes: 30 additions & 30 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function get($name)
return $this->services[$requestedName];
}

$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
$name = $this->resolvedAliases[$name] ?? $name;

// Next, if the alias should be shared, and we have cached the resolved
// service, use it.
Expand Down Expand Up @@ -223,7 +223,7 @@ public function get($name)
public function build($name, array $options = null)
{
// We never cache when using "build"
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
$name = $this->resolvedAliases[$name] ?? $name;
return $this->doCreate($name, $options);
}

Expand All @@ -232,7 +232,7 @@ public function build($name, array $options = null)
*/
public function has($name)
{
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
$name = $this->resolvedAliases[$name] ?? $name;
$found = isset($this->services[$name]) || isset($this->factories[$name]);

if ($found) {
Expand Down Expand Up @@ -323,12 +323,12 @@ public function configure(array $config)

if (! empty($aliases)) {
$config['aliases'] = (isset($config['aliases']))
? array_merge($config['aliases'], $aliases)
? \array_merge($config['aliases'], $aliases)
: $aliases;
}

$config['factories'] = (isset($config['factories']))
? array_merge($config['factories'], $factories)
? \array_merge($config['factories'], $factories)
: $factories;
}

Expand All @@ -337,7 +337,7 @@ public function configure(array $config)
}

if (isset($config['delegators'])) {
$this->delegators = array_merge_recursive($this->delegators, $config['delegators']);
$this->delegators = \array_merge_recursive($this->delegators, $config['delegators']);
}

if (isset($config['shared'])) {
Expand All @@ -357,7 +357,7 @@ public function configure(array $config)
// If lazy service configuration was provided, reset the lazy services
// delegator factory.
if (isset($config['lazy_services']) && ! empty($config['lazy_services'])) {
$this->lazyServices = array_merge_recursive($this->lazyServices, $config['lazy_services']);
$this->lazyServices = \array_merge_recursive($this->lazyServices, $config['lazy_services']);
$this->lazyServicesDelegator = null;
}

Expand Down Expand Up @@ -516,7 +516,7 @@ public function setShared($name, $flag)
private function resolveAbstractFactories(array $abstractFactories)
{
foreach ($abstractFactories as $abstractFactory) {
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
if (\is_string($abstractFactory) && \class_exists($abstractFactory)) {
//Cached string
if (! isset($this->cachedAbstractFactories[$abstractFactory])) {
$this->cachedAbstractFactories[$abstractFactory] = new $abstractFactory();
Expand All @@ -526,15 +526,15 @@ private function resolveAbstractFactories(array $abstractFactories)
}

if ($abstractFactory instanceof Factory\AbstractFactoryInterface) {
$abstractFactoryObjHash = spl_object_hash($abstractFactory);
$abstractFactoryObjHash = \spl_object_hash($abstractFactory);
$this->abstractFactories[$abstractFactoryObjHash] = $abstractFactory;
continue;
}

// Error condition; let's find out why.

// If we still have a string, we have a class name that does not resolve
if (is_string($abstractFactory)) {
if (\is_string($abstractFactory)) {
throw new InvalidArgumentException(
sprintf(
'An invalid abstract factory was registered; resolved to class "%s" ' .
Expand Down Expand Up @@ -568,18 +568,18 @@ private function resolveAbstractFactories(array $abstractFactories)
private function resolveInitializers(array $initializers)
{
foreach ($initializers as $initializer) {
if (is_string($initializer) && class_exists($initializer)) {
if (\is_string($initializer) && \class_exists($initializer)) {
$initializer = new $initializer();
}

if (is_callable($initializer)) {
if (\is_callable($initializer)) {
$this->initializers[] = $initializer;
continue;
}

// Error condition; let's find out why.

if (is_string($initializer)) {
if (\is_string($initializer)) {
throw new InvalidArgumentException(
sprintf(
'An invalid initializer was registered; resolved to class or function "%s" ' .
Expand Down Expand Up @@ -656,15 +656,15 @@ private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases)
*/
private function getFactory($name)
{
$factory = isset($this->factories[$name]) ? $this->factories[$name] : null;
$factory = $this->factories[$name] ?? null;

$lazyLoaded = false;
if (is_string($factory) && class_exists($factory)) {
if (\is_string($factory) && \class_exists($factory)) {
$factory = new $factory();
$lazyLoaded = true;
}

if (is_callable($factory)) {
if (\is_callable($factory)) {
if ($lazyLoaded) {
$this->factories[$name] = $factory;
}
Expand Down Expand Up @@ -704,13 +704,13 @@ private function createDelegatorFromName($name, array $options = null)
$delegatorFactory = $this->createLazyServiceDelegatorFactory();
}

if (is_string($delegatorFactory) && class_exists($delegatorFactory)) {
if (\is_string($delegatorFactory) && \class_exists($delegatorFactory)) {
$delegatorFactory = new $delegatorFactory();
}

if (! is_callable($delegatorFactory)) {
if (is_string($delegatorFactory)) {
throw new ServiceNotCreatedException(sprintf(
if (! \is_callable($delegatorFactory)) {
if (\is_string($delegatorFactory)) {
throw new ServiceNotCreatedException(\sprintf(
'An invalid delegator factory was registered; resolved to class or function "%s" '
. 'which does not exist; please provide a valid function name or class name resolving '
. 'to an implementation of %s',
Expand All @@ -719,9 +719,9 @@ private function createDelegatorFromName($name, array $options = null)
));
}

throw new ServiceNotCreatedException(sprintf(
throw new ServiceNotCreatedException(\sprintf(
'A non-callable delegator, "%s", was provided; expected a callable or instance of "%s"',
is_object($delegatorFactory) ? get_class($delegatorFactory) : gettype($delegatorFactory),
\is_object($delegatorFactory) ? \get_class($delegatorFactory) : \gettype($delegatorFactory),
DelegatorFactoryInterface::class
));
}
Expand Down Expand Up @@ -814,7 +814,7 @@ private function createLazyServiceDelegatorFactory()
));
}

spl_autoload_register($factoryConfig->getProxyAutoloader());
\spl_autoload_register($factoryConfig->getProxyAutoloader());

$this->lazyServicesDelegator = new Proxy\LazyServiceFactory(
new LazyLoadingValueHolderFactory($factoryConfig),
Expand Down Expand Up @@ -892,31 +892,31 @@ private function validateOverrides(array $config)
}

if (isset($config['services'])) {
$this->validateOverrideSet(array_keys($config['services']), 'service');
$this->validateOverrideSet(\array_keys($config['services']), 'service');
}

if (isset($config['aliases'])) {
$this->validateOverrideSet(array_keys($config['aliases']), 'alias');
$this->validateOverrideSet(\array_keys($config['aliases']), 'alias');
}

if (isset($config['invokables'])) {
$this->validateOverrideSet(array_keys($config['invokables']), 'invokable class');
$this->validateOverrideSet(\array_keys($config['invokables']), 'invokable class');
}

if (isset($config['factories'])) {
$this->validateOverrideSet(array_keys($config['factories']), 'factory');
$this->validateOverrideSet(\array_keys($config['factories']), 'factory');
}

if (isset($config['delegators'])) {
$this->validateOverrideSet(array_keys($config['delegators']), 'delegator');
$this->validateOverrideSet(\array_keys($config['delegators']), 'delegator');
}

if (isset($config['shared'])) {
$this->validateOverrideSet(array_keys($config['shared']), 'sharing rule');
$this->validateOverrideSet(\array_keys($config['shared']), 'sharing rule');
}

if (isset($config['lazy_services']['class_map'])) {
$this->validateOverrideSet(array_keys($config['lazy_services']['class_map']), 'lazy service');
$this->validateOverrideSet(\array_keys($config['lazy_services']['class_map']), 'lazy service');
}
}

Expand Down