Completed
Push — master ( d819eb...202c45 )
by Andrei
03:53
created

XrefTokenResolverCollection   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 152
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 82.64%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 32
c 6
b 1
f 0
lcom 1
cbo 7
dl 16
loc 152
ccs 100
cts 121
cp 0.8264
rs 9.6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A addCollection() 0 4 1
A prepend() 0 4 1
A prependCollection() 0 4 1
D resolveTokens() 0 34 9
C getTokensFromArray() 16 31 8
A applyToString() 0 10 2
D applyToArray() 0 41 9

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
namespace ConfigToken\TreeCompiler;
4
5
6
use ConfigToken\TokenCollection;
7
use ConfigToken\TokenInjector;
8
use ConfigToken\TokenParser;
9
use ConfigToken\TokenResolver\Types\RegisteredTokenResolver;
10
use ConfigToken\TokenResolver\Types\ScopeTokenResolver;
11
use ConfigToken\TreeCompiler;
12
13
class XrefTokenResolverCollection
14
{
15
    /** @var XrefTokenResolver[] */
16
    protected $collection = array();
17
18
    protected static $_KEY = 0;
19
    protected static $_KEY_TOKENS = 1;
20
    protected static $_KEY_REF = 2;
21
    protected static $_VALUE = 3;
22
    protected static $_VALUE_TOKENS = 4;
23
    protected static $_VALUE_REF = 5;
24
25 5
    public function add(XrefTokenResolver $xrefTokenResolver)
26
    {
27 5
        $this->collection[] = $xrefTokenResolver;
28 5
    }
29
30 3
    public function addCollection(XrefTokenResolverCollection $xrefTokenResolverCollection)
31
    {
32 3
        $this->collection = array_merge($this->collection, $xrefTokenResolverCollection->collection);
33 3
    }
34
35
    public function prepend(XrefTokenResolver $xrefTokenResolver)
36
    {
37
        array_unshift($this->collection, $xrefTokenResolver);
38
    }
39
40
    public function prependCollection(XrefTokenResolverCollection $xrefTokenResolverCollection)
41
    {
42
        $this->collection = array_merge($xrefTokenResolverCollection->collection, $this->collection);
43
    }
44
45 5
    protected function resolveTokens(TokenCollection $tokens, XrefTokenResolver $xrefTokenResolver)
46
    {
47 5
        if (!$xrefTokenResolver->hasTokenResolver()) {
48
            throw new \Exception('No token resolver found for Xref token resolver.');
49
        }
50 5
        $tokenResolver = $xrefTokenResolver->getTokenResolver();
51 5
        $hasValues = False;
52 5
        if ($tokenResolver instanceof RegisteredTokenResolver) {
53 5
            $hasValues = $tokenResolver->hasRegisteredTokenValues();
54 5
        }
55 5
        if ($tokenResolver instanceof ScopeTokenResolver) {
56
            $hasValues = $tokenResolver->hasScope();
57
        }
58 5
        if (!$hasValues) {
59 4
            if ($xrefTokenResolver->hasXref()) {
60
                $xref = $xrefTokenResolver->getXref();
61
                if (!$xref->isResolved()) {
62
                    $treeCompiler = new TreeCompiler();
63
                    $compiledValues = $treeCompiler->compileXref($xref);
0 ignored issues
show
Bug introduced by
It seems like $xref defined by $xrefTokenResolver->getXref() on line 60 can be null; however, ConfigToken\TreeCompiler::compileXref() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
64
                    $xref->setData($compiledValues);
65
                }
66
                $values = $xref->getData();
67
            } else {
68 4
                $values = $xrefTokenResolver->getRegisteredTokenValues();
69
            }
70 4
            if ($tokenResolver instanceof RegisteredTokenResolver) {
71 4
                $tokenResolver->setRegisteredTokenValues($values);
72 4
            }
73 4
            if ($tokenResolver instanceof ScopeTokenResolver) {
74
                $tokenResolver->setScope($values);
75
            }
76 4
        }
77 5
        $xrefTokenResolver->resolve($tokens);
78 5
    }
79
80 5
    protected function getTokensFromArray(&$array, TokenParser $parser, &$result)
81
    {
82 5
        foreach ($array as $key => &$value) {
83 5
            $t = array();
84 5 View Code Duplication
            if (gettype($key) == 'string') {
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...
85 5
                $tokens = $parser->parseString($key);
86 5
                if (!$tokens->isEmpty()) {
87 1
                    $t[self::$_KEY] = $key;
88 1
                    $t[self::$_KEY_TOKENS] = $tokens;
89 1
                    $t[self::$_KEY_REF] = &$array;
90 1
                }
91 5
            }
92 5
            $valueType = gettype($value);
93 5 View Code Duplication
            if ($valueType == 'string') {
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...
94 5
                $tokens = $parser->parseString($value);
95 5
                if (!$tokens->isEmpty()) {
96 5
                    $t[self::$_VALUE] = $value;
97 5
                    $t[self::$_VALUE_TOKENS] = $tokens;
98 5
                    $t[self::$_VALUE_REF] = &$value;
99 5
                }
100 5
            }
101 5
            if (count($t) > 0) {
102 5
                $result[] = $t;
103 5
                unset($t);
104 5
            }
105 5
            if ($valueType == 'array') {
106
                $this->getTokensFromArray($value, $parser, $result);
107
            }
108 5
        }
109 5
        unset($value);
110 5
    }
111
112 3
    public function applyToString($string)
113
    {
114 3
        foreach ($this->collection as $xrefTokenResolver) {
115 2
            $tokenParser = $xrefTokenResolver->getTokenParser();
116 2
            $tokens = $tokenParser->parseString($string);
117 2
            $this->resolveTokens($tokens, $xrefTokenResolver);
118 2
            $string = TokenInjector::injectString($string, $tokens);
119 3
        }
120 3
        return $string;
121
    }
122
123 5
    public function applyToArray(&$array)
124
    {
125 5
        foreach ($this->collection as $xrefTokenResolver) {
126 5
            $tokenParser = $xrefTokenResolver->getTokenParser();
127 5
            $lookup = array();
128 5
            $this->getTokensFromArray($array, $tokenParser, $lookup);
129 5
            foreach ($lookup as $record) {
130 5
                if (isset($record[self::$_VALUE_TOKENS])) {
131 5
                    $this->resolveTokens($record[self::$_VALUE_TOKENS], $xrefTokenResolver);
132 5
                }
133 5
                if (isset($record[self::$_KEY_TOKENS])) {
134 1
                    $this->resolveTokens($record[self::$_KEY_TOKENS], $xrefTokenResolver);
135 1
                }
136 5
            }
137 5
            foreach ($lookup as &$record) {
138 5
                if (isset($record[self::$_VALUE])) {
139 5
                    $newValue = TokenInjector::injectString(
140 5
                        $record[self::$_VALUE],
141 5
                        $record[self::$_VALUE_TOKENS]
142 5
                    );
143 5
                    $record[self::$_VALUE_REF] = $newValue;
144 5
                    unset($record[self::$_VALUE_TOKENS]);
145 5
                    unset($record[self::$_VALUE_REF]);
146 5
                }
147 5
                if (isset($record[self::$_KEY])) {
148 1
                    $oldKey = $record[self::$_KEY];
149 1
                    $newKey = TokenInjector::injectString(
150 1
                        $oldKey,
151 1
                        $record[self::$_KEY_TOKENS]
152 1
                    );
153 1
                    unset($record[self::$_KEY_TOKENS]);
154 1
                    if ($oldKey != $newKey) {
155 1
                        $record[self::$_KEY_REF][$newKey] = $record[self::$_KEY_REF][$oldKey];
156 1
                        unset($record[self::$_KEY_REF][$oldKey]);
157 1
                        unset($record[self::$_KEY_REF]);
158 1
                    }
159 1
                }
160 5
            }
161 5
            unset($record);
162 5
        }
163
    }
164
}