Completed
Pull Request — master (#578)
by Kévin
03:58
created

DataProviderPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 6
loc 24
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 5 1
A registerDataProviders() 6 12 4

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Registers data providers.
20
 *
21
 * @internal
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 */
25
final class DataProviderPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        $this->registerDataProviders($container, 'collection');
33
        $this->registerDataProviders($container, 'item');
34
    }
35
36
    private function registerDataProviders(ContainerBuilder $container, string $type) {
37
        $sortedServices = [];
38 View Code Duplication
        foreach ($container->findTaggedServiceIds('api_platform.'.$type.'_data_provider') as $serviceId => $tags) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            foreach ($tags as $tag) {
40
                $priority = isset($tag['priority']) ? $tag['priority'] : 0;
41
                $sortedServices[$priority][] = new Reference($serviceId);
42
            }
43
        }
44
        krsort($sortedServices);
45
46
        $container->getDefinition('api_platform.'.$type.'_data_provider')->addArgument(call_user_func_array('array_merge', $sortedServices));
47
    }
48
}
49