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

Commit

Permalink
Optimize more PHP function calls and issets
Browse files Browse the repository at this point in the history
  • Loading branch information
smuggli committed Jul 6, 2017
1 parent 24c488b commit 2ae8b16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
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
14 changes: 7 additions & 7 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 @@ -656,7 +656,7 @@ 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)) {
Expand Down Expand Up @@ -710,7 +710,7 @@ private function createDelegatorFromName($name, array $options = null)

if (! \is_callable($delegatorFactory)) {
if (\is_string($delegatorFactory)) {
throw new ServiceNotCreatedException(sprintf(
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

0 comments on commit 2ae8b16

Please sign in to comment.