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

PHPDefinitionParser::parseMigrationDefinition()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 60

Duplication

Lines 11
Ratio 18.33 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 11
loc 60
c 0
b 0
f 0
ccs 0
cts 34
cp 0
rs 8.2505
cc 6
nc 18
nop 1
crap 42

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 Kaliop\eZMigrationBundle\Core\DefinitionParser;
4
5
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
7
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
8
use PhpParser\Error;
9
use PhpParser\ParserFactory;
10
11
class PHPDefinitionParser implements DefinitionParserInterface
12
{
13
    protected $mandatoryInterface = 'Kaliop\eZMigrationBundle\API\MigrationInterface';
14
15
    /**
16
     * Tells whether the given file can be handled by this handler, by checking e.g. the suffix
17
     *
18
     * @param string $migrationName typically a filename
19
     * @return bool
20
     */
21
    public function supports($migrationName)
22
    {
23
        return pathinfo($migrationName, PATHINFO_EXTENSION) == 'php';
24
    }
25
26
    /**
27
     * Parses a migration definition file, and returns the list of actions to take
28
     *
29
     * @param MigrationDefinition $definition
30
     * @return MigrationDefinition
31
     */
32
    public function parseMigrationDefinition(MigrationDefinition $definition)
33
    {
34
        $status = MigrationDefinition::STATUS_PARSED;
35
36
        /// validate that php file is ok, contains a class with good interface
37
        $className = $this->getClassNameFromFile($definition->path);
38
39
        if ($className == '') {
40
            $status = MigrationDefinition::STATUS_INVALID;
41
            $message = 'The migration definition file should contain a valid class name. The class name is the part of the filename after the 1st underscore';
42
        } else {
43
44
            // we use smart parsing instead before plain file inclusion, by usage of nikic/php-parser
45
            // this should help with broken php migrations
46
47
            $pf = new ParserFactory();
48
            $parser = $pf->create(ParserFactory::PREFER_PHP7);
49
            try {
50
                $parser->parse(file_get_contents($definition->path));
51
52
                include_once($definition->path);
53
54
                if (!class_exists($className)) {
55
                    $status = MigrationDefinition::STATUS_INVALID;
56
                    $message = "The migration definition file should contain a valid class '$className'";
57
                } else {
58
                    $interfaces = class_implements($className);
59
                    if (!in_array($this->mandatoryInterface, $interfaces)) {
60
                        $status = MigrationDefinition::STATUS_INVALID;
61
                        $message = "The migration definition class '$className' should implement the interface '{$this->mandatoryInterface}'";
62
                    }
63
                }
64
            } catch (Error $e) {
65
                $status = MigrationDefinition::STATUS_INVALID;
66
                $message = "The migration definition file '{$definition->path}' is not valid php'";
67
            }
68
        }
69
70 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...
71
        {
72
            return new MigrationDefinition(
73
                $definition->name,
74
                $definition->path,
75
                $definition->rawDefinition,
76
                $status,
77
                array(),
78
                $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...
79
            );
80
        }
81
82
        return new MigrationDefinition(
83
            $definition->name,
84
            $definition->path,
85
            $definition->rawDefinition,
86
            MigrationDefinition::STATUS_PARSED,
87
            array(
88
                new MigrationStep('php', array('class' => $className), array('path' => $definition->path))
89
            )
90
        );
91
    }
92
93
    /**
94
     * @param string $fileName
95
     * @return string|null
96
     */
97
    protected function getClassNameFromFile($fileName)
98
    {
99
        $parts = explode('_', pathinfo($fileName, PATHINFO_FILENAME), 2);
100
        return isset($parts[1]) ? $parts[1] : null;
101
    }
102
}
103