Completed
Branch develop (c2aa4c)
by Anton
05:17
created

AliasTrait   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 58
lcom 0
cbo 0
dl 0
loc 28
rs 4.8387

1 Method

Rating   Name   Duplication   Size   Complexity  
A mountAlias() 0 18 4

How to fix   Complexity   

Complex Class

Complex classes like AliasTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AliasTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
namespace Spiral\ORM\Entities\Traits;
10
11
/**
12
 * Replaces {@} with valid table alias in array where.
13
 */
14
trait AliasTrait
15
{
16
    /**
17
     * Replace {@} in where statement with valid alias.
18
     *
19
     * @param string $alias
20
     * @param array  $where
21
     * @return array
22
     */
23
    protected function mountAlias($alias, array $where)
24
    {
25
        $result = [];
26
27
        foreach ($where as $key => $value) {
28
            if (strpos($key, '{@}') !== false) {
29
                $key = str_replace('{@}', $alias, $key);
30
            }
31
32
            if (is_array($value)) {
33
                $value = $this->mountAlias($alias, $where);
34
            }
35
36
            $result[$key] = $value;
37
        }
38
39
        return $result;
40
    }
41
}