TokenInjector::injectString()   D
last analyzed

Complexity

Conditions 13
Paths 385

Size

Total Lines 62
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 13.0014

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 48
cts 49
cp 0.9796
rs 4.5568
c 0
b 0
f 0
cc 13
eloc 40
nc 385
nop 3
crap 13.0014

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ConfigToken;
4
5
use ConfigToken\TokenResolver\Exception\UnknownTokenSourceException;
6
7
/**
8
 * Provides a method to inject the resolved token values collection back into the original string.
9
 */
10
class TokenInjector
11
{
12
    /**
13
     * Inject the resolved token values in the given string.
14
     *
15
     * @param string $string The string where to inject the resolved token values.
16
     * @param TokenCollection $tokens The tokens.
17
     * @param boolean $checkHash If true, the hash of the string must match the source hash of the tokens.
18
     * @throws UnknownTokenSourceException
19
     * @return string
20
     */
21 7
    public static function injectString($string, TokenCollection $tokens, $checkHash = False) {
22 7
        if ($checkHash) {
23 1
            $hash = md5($string);
24 1
            if ($tokens->hasSourceHash() && ($tokens->getSourceHash() !== $hash)) {
25
                throw new UnknownTokenSourceException('Unable to inject tokens.');
26
            }
27 1
        }
28
29 7
        $offsets = array();
30 7
        $allTokens = $tokens->getArray();
31 7
        foreach ($allTokens as $tokenString => $token) {
32 7
            $tokenOffsets = $token->getOffsets();
33 7
            foreach ($tokenOffsets as $offset) {
34 7
                $offsets[$offset] = $tokenString;
35 7
            }
36 7
        }
37 7
        ksort($offsets);
38
39 7
        $blocks = array();
40 7
        $lastOffset = 0;
41 7
        $offsetDelta = 0;
42
        /** @var Token[] $injected */
43 7
        $injected = array();
44 7
        foreach ($offsets as $offset => $tokenString) {
45
            /** @var Token $token */
46 7
            $token = $allTokens[$tokenString];
47 7
            if (!$token->getIsInjected()) {
48 7
                $token->adjustOffset($offset, $offsetDelta);
49 7
                if ($token->getIsResolved()) {
50 7
                    $tokenValue = $token->getTokenValue();
51 7
                    $blocks[] = substr($string, $lastOffset, $offset - $lastOffset);
52 7
                    $blocks[] = $tokenValue;
53 7
                    $lastOffset = $offset + strlen($tokenString);
54 7
                    $offsetDelta += strlen($tokenValue) - strlen($tokenString);
55 7
                    $injected[$tokenString] = $token;
56 7
                }
57 7
            }
58 7
        }
59
60 7
        if ($lastOffset > 0) {
61 7
            $blocks[] = substr($string, $lastOffset);
62 7
        }
63
64 7
        foreach ($injected as $token) {
65 7
            $token->setIsInjected(true);
66 7
        }
67
68 7
        if (empty($blocks)) {
69 2
            $newString = $string;
70 2
        } else {
71 7
            $newString = implode('', $blocks);
72
        }
73
74 7
        unset($string);
75 7
        unset($blocks);
76
77 7
        if ($checkHash) {
78 1
            $tokens->setSourceHash(md5($newString));
79 1
        }
80
81 7
        return $newString;
82
    }
83
}