Completed
Pull Request — master (#42)
by Daniel
02:14 queued 18s
created

ArrayDriver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 30
loc 132
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C loadMetadataForClass() 24 93 7
A getAllClassNames() 0 4 1
A resolveConfig() 6 11 2

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
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Metadata\Driver;
6
7
use Metadata\Driver\AdvancedDriverInterface;
8
use Psi\Component\Grid\Metadata\ActionMetadata;
9
use Psi\Component\Grid\Metadata\ClassMetadata;
10
use Psi\Component\Grid\Metadata\ColumnMetadata;
11
use Psi\Component\Grid\Metadata\FilterMetadata;
12
use Psi\Component\Grid\Metadata\GridMetadata;
13
use Psi\Component\Grid\Metadata\QueryMetadata;
14
15
class ArrayDriver implements AdvancedDriverInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $config;
21
22
    /**
23
     * @param array $config
24
     */
25
    public function __construct(array $config)
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        if (!isset($this->config[$class->getName()])) {
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
36
            return;
37
        }
38
39
        $config = $this->resolveConfig([
40
            'grids' => [],
41
            'queries' => [],
42
        ], $this->config[$class->getName()]);
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
43
44
        $queries = [];
45
        foreach ($config['queries'] as $queryName => $queryConfig) {
46
            $queryConfig = $this->resolveConfig([
47
                'name' => null,
48
                'selects' => [],
49
                'joins' => [],
50
                'criteria' => [],
51
            ], $queryConfig);
52
            $queries[$queryName] = new QueryMetadata(
53
                $queryName, $queryConfig['selects'], $queryConfig['joins'], $queryConfig['criteria']
54
            );
55
        }
56
57
        $grids = [];
58
        foreach ($config['grids'] as $gridName => $gridConfig) {
59
            $gridConfig = $this->resolveConfig([
60
                'name' => null,
61
                'query' => null,
62
                'columns' => [],
63
                'filters' => [],
64
                'actions' => [],
65
                'page_size' => 50,
66
            ], $gridConfig);
67
68
            $columns = [];
69 View Code Duplication
            foreach ($gridConfig['columns'] as $columnName => $columnConfig) {
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...
70
                $columnConfig = $this->resolveConfig([
71
                    'type' => null,
72
                    'options' => [],
73
                ], $columnConfig);
74
75
                $columns[$columnName] = new ColumnMetadata(
76
                    $columnName,
77
                    $columnConfig['type'],
78
                    $columnConfig['options']
79
                );
80
            }
81
82
            $filters = [];
83
            foreach ($gridConfig['filters'] as $filterName => $filterConfig) {
84
                $filterConfig = $this->resolveConfig([
85
                    'type' => null,
86
                    'field' => null,
87
                    'options' => [],
88
                ], $filterConfig);
89
90
                $filters[$filterName] = new FilterMetadata(
91
                    $filterName,
92
                    $filterConfig['type'],
93
                    $filterConfig['field'],
94
                    $filterConfig['options']
95
                );
96
            }
97
98
            $actions = [];
99 View Code Duplication
            foreach ($gridConfig['actions'] as $actionName => $actionConfig) {
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...
100
                $actionConfig = $this->resolveConfig([
101
                    'type' => null,
102
                    'options' => [],
103
                ], $actionConfig);
104
105
                $actions[$actionName] = new ActionMetadata(
106
                    $actionName,
107
                    $actionConfig['type'],
108
                    $actionConfig['options']
109
                );
110
            }
111
112
            $grids[$gridName] = new GridMetadata(
113
                $gridName,
114
                $columns,
115
                $filters,
116
                $actions,
117
                $gridConfig['page_size'],
118
                $gridConfig['query']
119
            );
120
        }
121
122
        $classMetadata = new ClassMetadata($class->getName(), $grids, $queries);
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
123
124
        return $classMetadata;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getAllClassNames()
131
    {
132
        return array_keys($this->config);
133
    }
134
135
    private function resolveConfig(array $defaultConfig, array $config)
136
    {
137 View Code Duplication
        if ($diff = array_diff(array_keys($config), array_keys($defaultConfig))) {
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...
138
            throw new \InvalidArgumentException(sprintf(
139
                'Invalid configuration keys "%s" for grid, valid keys: "%s"',
140
                implode('", "', $diff), implode('", "', array_keys($defaultConfig))
141
            ));
142
        }
143
144
        return array_merge($defaultConfig, $config);
145
    }
146
}
147