PHPDefinitionParser::getClassNameFromFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 145
    public function supports($migrationName)
22
    {
23 145
        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 51
    public function parseMigrationDefinition(MigrationDefinition $definition)
33
    {
34 51
        $status = MigrationDefinition::STATUS_PARSED;
35
36
        /// validate that php file is ok, contains a class with good interface
37 51
        $className = $this->getClassNameFromFile($definition->path);
38
39 51
        if ($className == '') {
40 1
            $status = MigrationDefinition::STATUS_INVALID;
41 1
            $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 50
            $pf = new ParserFactory();
48 50
            $parser = $pf->create(ParserFactory::PREFER_PHP7);
49
            try {
50 50
                $parser->parse(file_get_contents($definition->path));
51
52 49
                include_once($definition->path);
53
54
                // we leave these checks to be done by the PHPExecutor
55
                /*
56
                if (!class_exists($className)) {
57
                    $status = MigrationDefinition::STATUS_INVALID;
58
                    $message = "The migration definition file should contain a valid class '$className'";
59
                } else {
60
                    $interfaces = class_implements($className);
61
                    if (!in_array($this->mandatoryInterface, $interfaces)) {
62
                        $status = MigrationDefinition::STATUS_INVALID;
63
                        $message = "The migration definition class '$className' should implement the interface '{$this->mandatoryInterface}'";
64
                    }
65
                }*/
66 1
            } catch (Error $e) {
67 1
                $status = MigrationDefinition::STATUS_INVALID;
68 1
                $message = "The migration definition file '{$definition->path}' is not valid php'";
69
            }
70
        }
71
72 51
        if ($status != MigrationDefinition::STATUS_PARSED)
73
        {
74 2
            return new MigrationDefinition(
75 2
                $definition->name,
76 2
                $definition->path,
77 2
                $definition->rawDefinition,
78
                $status,
79 2
                array(),
80
                $message
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
81
            );
82
        }
83
84 49
        return new MigrationDefinition(
85 49
            $definition->name,
86 49
            $definition->path,
87 49
            $definition->rawDefinition,
88 49
            MigrationDefinition::STATUS_PARSED,
89
            array(
90 49
                new MigrationStep('php', array('class' => $className), array('path' => $definition->path))
91
            )
92
        );
93
    }
94
95
    /**
96
     * @param string $fileName
97
     * @return string|null
98
     */
99 51
    protected function getClassNameFromFile($fileName)
100
    {
101 51
        $parts = explode('_', pathinfo($fileName, PATHINFO_FILENAME), 2);
0 ignored issues
show
Bug introduced by
It seems like pathinfo($fileName, Kali...rser\PATHINFO_FILENAME) can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        $parts = explode('_', /** @scrutinizer ignore-type */ pathinfo($fileName, PATHINFO_FILENAME), 2);
Loading history...
102 51
        return isset($parts[1]) ? $parts[1] : null;
103
    }
104
}
105