We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | abstract class TaggedServiceMappingPass implements CompilerPassInterface |
||
| 21 | 13 | { |
|
| 22 | private function getTaggedServiceMapping(ContainerBuilder $container, $tagName) |
||
| 23 | 13 | { |
|
| 24 | $serviceMapping = []; |
||
| 25 | 13 | ||
| 26 | $taggedServices = $container->findTaggedServiceIds($tagName); |
||
| 27 | 13 | ||
| 28 | 13 | foreach ($taggedServices as $id => $tags) { |
|
| 29 | 13 | $className = $container->findDefinition($id)->getClass(); |
|
| 30 | 13 | $isType = is_subclass_of($className, Type::class); |
|
|
|
|||
| 31 | 13 | foreach ($tags as $tag) { |
|
| 32 | 13 | $this->checkRequirements($id, $tag); |
|
| 33 | $tag = array_merge($tag, ['id' => $id]); |
||
| 34 | 13 | if (!$isType) { |
|
| 35 | $tag['method'] = isset($tag['method']) ? $tag['method'] : '__invoke'; |
||
| 36 | } |
||
| 37 | 13 | if (isset($tag['alias'])) { |
|
| 38 | $serviceMapping[$tag['alias']] = $tag; |
||
| 39 | 13 | } |
|
| 40 | 13 | ||
| 41 | 13 | // add FQCN alias |
|
| 42 | $alias = $className; |
||
| 43 | 13 | if (!$isType && '__invoke' !== $tag['method']) { |
|
| 44 | 13 | $alias .= '::'.$tag['method']; |
|
| 45 | 13 | } |
|
| 46 | $tag['alias'] = $alias; |
||
| 47 | 13 | $serviceMapping[$tag['alias']] = $tag; |
|
| 48 | 13 | } |
|
| 49 | } |
||
| 50 | |||
| 51 | return $serviceMapping; |
||
| 52 | 13 | } |
|
| 53 | 13 | ||
| 54 | 13 | public function process(ContainerBuilder $container) |
|
| 55 | { |
||
| 56 | 13 | $mapping = $this->getTaggedServiceMapping($container, $this->getTagName()); |
|
| 57 | $container->setParameter($this->getParameterName(), $mapping); |
||
| 58 | 13 | $resolverDefinition = $container->findDefinition($this->getResolverServiceID()); |
|
| 59 | |||
| 60 | foreach ($mapping as $name => $options) { |
||
| 61 | $cleanOptions = $options; |
||
| 62 | $solutionID = $options['id']; |
||
| 63 | 13 | ||
| 64 | $solutionDefinition = $container->findDefinition($options['id']); |
||
| 65 | |||
| 66 | $methods = array_map( |
||
| 67 | function ($methodCall) { |
||
| 68 | return $methodCall[0]; |
||
| 69 | }, |
||
| 70 | $solutionDefinition->getMethodCalls() |
||
| 71 | ); |
||
| 72 | if ( |
||
| 73 | is_subclass_of($solutionDefinition->getClass(), ContainerAwareInterface::class) |
||
| 74 | && !in_array('setContainer', $methods) |
||
| 75 | ) { |
||
| 76 | @trigger_error( |
||
| 77 | 'Autowire custom tagged (type, resolver or mutation) services is deprecated as of 0.9 and will be removed in 1.0. Use AutoMapping or set it manually instead.', |
||
| 78 | E_USER_DEPRECATED |
||
| 79 | ); |
||
| 80 | $solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]); |
||
| 81 | } |
||
| 82 | |||
| 83 | $resolverDefinition->addMethodCall('addSolution', [$name, new Reference($solutionID), $cleanOptions]); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function checkRequirements($id, array $tag) |
||
| 88 | { |
||
| 89 | View Code Duplication | if (isset($tag['alias']) && !is_string($tag['alias'])) { |
|
| 90 | throw new \InvalidArgumentException( |
||
| 91 | sprintf('Service tagged "%s" must have valid "alias" argument.', $id) |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | abstract protected function getTagName(); |
||
| 97 | |||
| 98 | abstract protected function getResolverServiceID(); |
||
| 99 | |||
| 100 | abstract protected function getParameterName(); |
||
| 101 | } |
||
| 102 |