Failed Conditions
Pull Request — master (#30)
by Moesjarraf
03:19
created

Transform::applyToNode()   F

Complexity

Conditions 17
Paths 424

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 17

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 32
cts 32
cp 1
rs 3.5675
c 0
b 0
f 0
cc 17
eloc 25
nc 424
nop 1
crap 17

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
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Transform processor, apply transformation functions on data
10
 */
11
class Transform implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Default transformation functions
17
     * @var array
18
     */
19
    public static $defaultFunctions = [
20
        'hash' => 'hash',
21
        'hash_hmac' => 'hash_hmac',
22
        'base64_encode' => 'base64_encode',
23
        'base64_decode' => 'base64_decode',
24
        'json_encode' => 'json_encode',
25
        'json_decode' => 'json_decode',
26
        'serialize' => 'serialize',
27
        'unserialize' => 'unserialize',
28
        'strtotime' => 'strtotime',
29
        'date' => 'date'
30
    ];
31
    
32
    /**
33
     * Allowed transformation functions
34
     * @var array 
35
     */
36
    public $functions;
37
    
38
    
39
    /**
40
     * Class constructor
41
     * 
42
     * @param string $property  Property key with the processing instruction
43
     */
44 12
    public function __construct($property)
45
    {
46 12
        $this->property = $property;
47 12
        $this->functions = static::$defaultFunctions;
48 12
    }
49
    
50
    /**
51
     * Apply processing to a single node
52
     * 
53
     * @param Node $node
54
     */
55 8
    public function applyToNode(Node $node)
56
    {
57 8
        $instruction = $node->getInstruction($this);
58 8
        $transformations = !is_array($instruction) ? [$instruction] : $instruction;
59
        
60 8
        if (isset($node->input)) {
61 5
            $input = $this->resolveNodes($node->input);
62 5
        }
63
        
64 8
        foreach ($transformations as $transformation) {
65 8
            if (is_string($transformation) && !isset($input)) {
66 1
                continue;
67
            }
68
            
69 7
            if (is_string($transformation)) {
70 5
                list($key, $args) = explode(':', $transformation) + [null, null];
71 7
            } elseif (is_object($transformation) || is_array($transformation)) {
72 2
                $transformation = (object)$transformation;
73 2
                $key = isset($transformation->function) ? $transformation->function : null;
74 2
                $args = isset($transformation->args) ? $transformation->args : [];
75 2
            }
76
            
77 7
            if (!isset($this->functions[$key])) {
78 1
                throw new \Exception("Unknown transformation '$key'");
79
            }
80
            
81 6
            if (isset($args)) {
82 5
                $args = $this->resolveNodes($args);
83 5
            }
84
            
85 6
            $fn = $this->functions[$key];
0 ignored issues
show
Bug introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
            
87 6
            if (is_string($transformation)) {
88 4
                $result = isset($args) ? call_user_func($fn, $args, $input) : call_user_func($fn, $input);
0 ignored issues
show
Bug introduced by
The variable $input does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89 6
            } elseif (is_object($transformation)) {
90 2
                $result = call_user_func_array($fn, $args);
0 ignored issues
show
Bug introduced by
The variable $args does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
91 2
            }
92 7
        }
93
        
94 7
        if (isset($result)) {
95 6
            $node->setResult($result);
96 6
        }
97 7
    }
98
    
99
    /**
100
     * Resolve instructions of nodes by getting their results
101
     * 
102
     * @param Node $node
103
     * 
104
     * @return mixed $result
105
     */
106 7
    protected function resolveNodes($node) {
107 7
        if (!$node instanceof Node) {
108 7
            return $node;
109
        }
110
111 1
        return $node->getResult();
112
    }
113
}
114