Completed
Pull Request — master (#148)
by Kevin
02:16
created

Interpolator::interpolate()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.4119
c 0
b 0
f 0
cc 8
eloc 40
nc 4
nop 1

How to fix   Long Method   

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 Magium\TestCase\Configurable;
4
5
use Magium\Util\Log\Logger;
6
use Zend\Di\Di;
7
8
class Interpolator
9
{
10
11
    protected $container;
12
    protected $logger;
13
14
    public function __construct(
15
        Di $di,
16
        Logger $logger
17
    )
18
    {
19
        $this->container = $di;
20
        $this->logger = $logger;
21
    }
22
23
    public function interpolate($string)
24
    {
25
        $this->logger->info('Interpolating string: ' . $string);
26
        $matches = null;
27
        // Finding {{product->setEntityId(123)->load()}} type methods
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
        if (preg_match('/\{\{([^{]+)\}\}/', $string, $matches)) {
29
            array_shift($matches);
30
            foreach ($matches as $match) {
31
                $result = null;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
                // Just replace it if it is a straight value reference
33
                if (strpos($match, '->') === false) {
34
                    $result = $this->container->get($match);
35
                } else {
36
                    // However, if it contains object method reference tokens let's recursively call them!  So much good
37
                    $parts = explode('->', $match);
38
                    $classOrAlias = array_shift($parts);
39
                    $result = $this->container->get($classOrAlias);
40
                    foreach ($parts as $method) {
41
                        $params = [];
42
                        $matchedParams = null;
43
                        if (preg_match('/\(([^)]+)\)/', $method, $matchedParams)) {
44
                            array_shift($matchedParams); // Don't need the full match
45
                            $matchedParams = array_shift($matchedParams); // Should only need the first match
46
                            $params = explode(',', $matchedParams);
47
                            $params = array_map('trim', $params);
48
                        }
49
50
                        if (strpos($method, '(') === false) {
51
                            $this->logger->debug(sprintf('Reading property %s->%s', get_class($result), $method));
52
                            $result = $result->$method;
53
                        } else {
54
                            $method = preg_replace('/\([^\)]*\)/', '', $method);
55
                            $callback = [$result, $method];
56
                            $this->logger->debug(
57
                                sprintf('Calling %s->%s with params %s', get_class($result), $method, serialize($params))
58
                            );
59
                            if (is_callable($callback)) {
60
                                $result = call_user_func_array($callback, $params);
61
                            } else {
62
                                throw new InvalidInstructionException('Result is not callable');
63
                            }
64
                        }
65
                    }
66
                }
67
                $result = (string)$result;
68
                $this->logger->info(sprintf('Replacing %s with %s', $match, $result));
69
                $search = sprintf('{{%s}}', $match);
70
                $string = str_replace($search, $result, $string);
71
            }
72
        } else {
73
            $this->logger->debug('No interpolations found');
74
        }
75
        return $string;
76
    }
77
78
}