Completed
Pull Request — master (#211)
by Ryan
07:26
created

Evaluator::data()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 43
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 5.2653
cc 11
eloc 20
nc 17
nop 2

How to fix   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 namespace Anomaly\Streams\Platform\Support;
2
3
use ArrayAccess;
4
use Illuminate\Contracts\Container\Container;
5
6
/**
7
 * Class Evaluator
8
 *
9
 * @link    http://anomaly.is/streams-platform
10
 * @author  AnomalyLabs, Inc. <[email protected]>
11
 * @author  Ryan Thompson <[email protected]>
12
 * @package Anomaly\Streams\Platform\Support
13
 */
14
class Evaluator
15
{
16
17
    /**
18
     * The IoC container.
19
     *
20
     * @var \Illuminate\Contracts\Container\Container
21
     */
22
    protected $container;
23
24
    /**
25
     * Create a new Evaluator instance.
26
     *
27
     * @param Container $container
28
     */
29
    public function __construct(Container $container)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * Evaluate a target entity with arguments.
36
     *
37
     * @param        $target
38
     * @param  array $arguments
39
     * @return mixed
40
     */
41
    public function evaluate($target, array $arguments = [])
42
    {
43
        /**
44
         * If the target is an instance of Closure then
45
         * call through the IoC it with the arguments.
46
         */
47
        if ($target instanceof \Closure) {
48
            return $this->container->call($target, $arguments);
49
        }
50
51
        /**
52
         * If the target is an array then evaluate
53
         * each of it's values.
54
         */
55
        if (is_array($target)) {
56
            foreach ($target as &$value) {
57
                $value = $this->evaluate($value, $arguments);
58
            }
59
        }
60
61
        /**
62
         * if the target is a string and is in a traversable
63
         * format then traverse the target using the arguments.
64
         */
65
        if (is_string($target) && !isset($arguments[$target]) && $this->isTraversable($target)) {
66
            $target = data_get($arguments, $target, $target);
67
        }
68
69
        return $target;
70
    }
71
72
    /**
73
     * Check if a string is in a traversable format.
74
     *
75
     * @param  $target
76
     * @return bool
77
     */
78
    protected function isTraversable($target)
79
    {
80
        return (!preg_match('/[^a-z._]/', $target));
81
    }
82
}
83