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

AbstractDefinitionParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 18.64 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 11
loc 59
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B parseMigrationDefinitionData() 11 48 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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