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

Commit 1afca73

Browse files
authored
Merge pull request #196 from smuggli/optimize_opcodes
Optimize PHP function calls
2 parents 93b78c5 + 32e8b82 commit 1afca73

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

src/AbstractFactory/ConfigAbstractFactory.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface
2727
*/
2828
public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName)
2929
{
30-
if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) {
30+
if (! $container->has('config') || ! \array_key_exists(self::class, $container->get('config'))) {
3131
return false;
3232
}
3333
$config = $container->get('config');
3434
$dependencies = $config[self::class];
3535

36-
return is_array($dependencies) && array_key_exists($requestedName, $dependencies);
36+
return \is_array($dependencies) && \array_key_exists($requestedName, $dependencies);
3737
}
3838

3939
/**
@@ -47,31 +47,31 @@ public function __invoke(\Interop\Container\ContainerInterface $container, $requ
4747

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

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

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

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

60-
if (! is_array($dependencies)
61-
|| ! array_key_exists($requestedName, $dependencies)
62-
|| ! is_array($dependencies[$requestedName])
60+
if (! \is_array($dependencies)
61+
|| ! \array_key_exists($requestedName, $dependencies)
62+
|| ! \is_array($dependencies[$requestedName])
6363
) {
6464
throw new ServiceNotCreatedException('Dependencies config must exist and be an array');
6565
}
6666

6767
$serviceDependencies = $dependencies[$requestedName];
6868

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

74-
$arguments = array_map([$container, 'get'], $serviceDependencies);
74+
$arguments = \array_map([$container, 'get'], $serviceDependencies);
7575

7676
return new $requestedName(...$arguments);
7777
}

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
220220
}
221221

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

225225
if (! $container->has($type)) {
226226
throw new ServiceNotFoundException(sprintf(

src/Config.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Config implements ConfigInterface
6868
public function __construct(array $config = [])
6969
{
7070
// Only merge keys we're interested in
71-
foreach (array_keys($config) as $key) {
71+
foreach (\array_keys($config) as $key) {
7272
if (! isset($this->allowedKeys[$key])) {
7373
unset($config[$key]);
7474
}
@@ -103,12 +103,12 @@ private function merge(array $a, array $b)
103103
foreach ($b as $key => $value) {
104104
if ($value instanceof MergeReplaceKeyInterface) {
105105
$a[$key] = $value->getData();
106-
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
106+
} elseif (isset($a[$key]) || \array_key_exists($key, $a)) {
107107
if ($value instanceof MergeRemoveKey) {
108108
unset($a[$key]);
109-
} elseif (is_int($key)) {
109+
} elseif (\is_int($key)) {
110110
$a[] = $value;
111-
} elseif (is_array($value) && is_array($a[$key])) {
111+
} elseif (\is_array($value) && \is_array($a[$key])) {
112112
$a[$key] = $this->merge($a[$key], $value);
113113
} else {
114114
$a[$key] = $value;

src/ServiceManager.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function get($name)
199199
return $this->services[$requestedName];
200200
}
201201

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

204204
// Next, if the alias should be shared, and we have cached the resolved
205205
// service, use it.
@@ -239,7 +239,7 @@ public function get($name)
239239
public function build($name, array $options = null)
240240
{
241241
// We never cache when using "build"
242-
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
242+
$name = $this->resolvedAliases[$name] ?? $name;
243243
return $this->doCreate($name, $options);
244244
}
245245

@@ -248,7 +248,7 @@ public function build($name, array $options = null)
248248
*/
249249
public function has($name)
250250
{
251-
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
251+
$name = $this->resolvedAliases[$name] ?? $name;
252252
$found = isset($this->services[$name]) || isset($this->factories[$name]);
253253

254254
if ($found) {
@@ -339,12 +339,12 @@ public function configure(array $config)
339339

340340
if (! empty($aliases)) {
341341
$config['aliases'] = (isset($config['aliases']))
342-
? array_merge($config['aliases'], $aliases)
342+
? \array_merge($config['aliases'], $aliases)
343343
: $aliases;
344344
}
345345

346346
$config['factories'] = (isset($config['factories']))
347-
? array_merge($config['factories'], $factories)
347+
? \array_merge($config['factories'], $factories)
348348
: $factories;
349349
}
350350

@@ -353,7 +353,7 @@ public function configure(array $config)
353353
}
354354

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

359359
if (isset($config['shared'])) {
@@ -373,7 +373,7 @@ public function configure(array $config)
373373
// If lazy service configuration was provided, reset the lazy services
374374
// delegator factory.
375375
if (isset($config['lazy_services']) && ! empty($config['lazy_services'])) {
376-
$this->lazyServices = array_merge_recursive($this->lazyServices, $config['lazy_services']);
376+
$this->lazyServices = \array_merge_recursive($this->lazyServices, $config['lazy_services']);
377377
$this->lazyServicesDelegator = null;
378378
}
379379

@@ -532,7 +532,7 @@ public function setShared($name, $flag)
532532
private function resolveAbstractFactories(array $abstractFactories)
533533
{
534534
foreach ($abstractFactories as $abstractFactory) {
535-
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
535+
if (\is_string($abstractFactory) && \class_exists($abstractFactory)) {
536536
//Cached string
537537
if (! isset($this->cachedAbstractFactories[$abstractFactory])) {
538538
$this->cachedAbstractFactories[$abstractFactory] = new $abstractFactory();
@@ -542,15 +542,15 @@ private function resolveAbstractFactories(array $abstractFactories)
542542
}
543543

544544
if ($abstractFactory instanceof Factory\AbstractFactoryInterface) {
545-
$abstractFactoryObjHash = spl_object_hash($abstractFactory);
545+
$abstractFactoryObjHash = \spl_object_hash($abstractFactory);
546546
$this->abstractFactories[$abstractFactoryObjHash] = $abstractFactory;
547547
continue;
548548
}
549549

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

552552
// If we still have a string, we have a class name that does not resolve
553-
if (is_string($abstractFactory)) {
553+
if (\is_string($abstractFactory)) {
554554
throw new InvalidArgumentException(
555555
sprintf(
556556
'An invalid abstract factory was registered; resolved to class "%s" ' .
@@ -584,18 +584,18 @@ private function resolveAbstractFactories(array $abstractFactories)
584584
private function resolveInitializers(array $initializers)
585585
{
586586
foreach ($initializers as $initializer) {
587-
if (is_string($initializer) && class_exists($initializer)) {
587+
if (\is_string($initializer) && \class_exists($initializer)) {
588588
$initializer = new $initializer();
589589
}
590590

591-
if (is_callable($initializer)) {
591+
if (\is_callable($initializer)) {
592592
$this->initializers[] = $initializer;
593593
continue;
594594
}
595595

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

598-
if (is_string($initializer)) {
598+
if (\is_string($initializer)) {
599599
throw new InvalidArgumentException(
600600
sprintf(
601601
'An invalid initializer was registered; resolved to class or function "%s" ' .
@@ -672,15 +672,15 @@ private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases)
672672
*/
673673
private function getFactory($name)
674674
{
675-
$factory = isset($this->factories[$name]) ? $this->factories[$name] : null;
675+
$factory = $this->factories[$name] ?? null;
676676

677677
$lazyLoaded = false;
678-
if (is_string($factory) && class_exists($factory)) {
678+
if (\is_string($factory) && \class_exists($factory)) {
679679
$factory = new $factory();
680680
$lazyLoaded = true;
681681
}
682682

683-
if (is_callable($factory)) {
683+
if (\is_callable($factory)) {
684684
if ($lazyLoaded) {
685685
$this->factories[$name] = $factory;
686686
}
@@ -720,13 +720,13 @@ private function createDelegatorFromName($name, array $options = null)
720720
$delegatorFactory = $this->createLazyServiceDelegatorFactory();
721721
}
722722

723-
if (is_string($delegatorFactory) && class_exists($delegatorFactory)) {
723+
if (\is_string($delegatorFactory) && \class_exists($delegatorFactory)) {
724724
$delegatorFactory = new $delegatorFactory();
725725
}
726726

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

738-
throw new ServiceNotCreatedException(sprintf(
738+
throw new ServiceNotCreatedException(\sprintf(
739739
'A non-callable delegator, "%s", was provided; expected a callable or instance of "%s"',
740-
is_object($delegatorFactory) ? get_class($delegatorFactory) : gettype($delegatorFactory),
740+
\is_object($delegatorFactory) ? \get_class($delegatorFactory) : \gettype($delegatorFactory),
741741
DelegatorFactoryInterface::class
742742
));
743743
}
@@ -830,7 +830,7 @@ private function createLazyServiceDelegatorFactory()
830830
));
831831
}
832832

833-
spl_autoload_register($factoryConfig->getProxyAutoloader());
833+
\spl_autoload_register($factoryConfig->getProxyAutoloader());
834834

835835
$this->lazyServicesDelegator = new Proxy\LazyServiceFactory(
836836
new LazyLoadingValueHolderFactory($factoryConfig),
@@ -908,31 +908,31 @@ private function validateOverrides(array $config)
908908
}
909909

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

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

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

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

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

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

934934
if (isset($config['lazy_services']['class_map'])) {
935-
$this->validateOverrideSet(array_keys($config['lazy_services']['class_map']), 'lazy service');
935+
$this->validateOverrideSet(\array_keys($config['lazy_services']['class_map']), 'lazy service');
936936
}
937937
}
938938

0 commit comments

Comments
 (0)