Completed
Push — master ( 1ee05e...99144a )
by Gaetano
05:13
created

parseMigrationDefinitionData()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 48

Duplication

Lines 11
Ratio 22.92 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 11
loc 48
c 0
b 0
f 0
ccs 0
cts 29
cp 0
rs 8.2012
cc 7
nc 12
nop 3
crap 56
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\DefinitionParser;
4
5
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
7
8
class AbstractDefinitionParser
9
{
10
    /**
11
     * Parses a migration definition in the form of an array of steps
12
     *
13
     * @param array $data
14
     * @param MigrationDefinition $definition
15
     * @param string $format
16
     * @return MigrationDefinition
17
     */
18
    protected function parseMigrationDefinitionData($data, MigrationDefinition $definition, $format = 'Yaml')
19
    {
20
        // basic validation
21
22
        /// @todo move to using the Validator component...
23
24
        $status = MigrationDefinition::STATUS_PARSED;
25
26
        if (!is_array($data)) {
27
            $status = MigrationDefinition::STATUS_INVALID;
28
            $message = "$format migration file '{$definition->path}' must contain an array as top element";
29
        } else {
30
            foreach ($data as $i => $stepDef) {
31
                if (!isset($stepDef['type']) || !is_string($stepDef['type'])) {
32
                    $status = MigrationDefinition::STATUS_INVALID;
33
                    $message = "$format migration file '{$definition->path}' misses or has a non-string 'type' element in step $i";
34
                    break;
35
                }
36
            }
37
        }
38
39 View Code Duplication
        if ($status != MigrationDefinition::STATUS_PARSED)
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...
40
        {
41
            return new MigrationDefinition(
42
                $definition->name,
43
                $definition->path,
44
                $definition->rawDefinition,
45
                $status,
46
                array(),
47
                $message
0 ignored issues
show
Bug introduced by
The variable $message 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...
48
            );
49
        }
50
51
        $stepDefs = array();
52
        foreach ($data as $stepDef) {
53
            $type = $stepDef['type'];
54
            unset($stepDef['type']);
55
            $stepDefs[] = new MigrationStep($type, $stepDef, array('path' => $definition->path));
56
        }
57
58
        return new MigrationDefinition(
59
            $definition->name,
60
            $definition->path,
61
            $definition->rawDefinition,
62
            MigrationDefinition::STATUS_PARSED,
63
            $stepDefs
64
        );
65
    }
66
}
67