Completed
Push — master ( 19a358...32fc21 )
by Mehmet
04:10
created

Yaml::yamlParse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Entity\Parser;
5
6
use Selami\Entity\Interfaces\ParserInterface;
7
use InvalidArgumentException;
8
use UnexpectedValueException;
9
use Symfony\Component\Yaml as SymfonyYaml;
10
use Symfony\Component\Yaml\Exception\ParseException as SymfonyParseException;
11
12
/**
13
 * Yaml Parser
14
 *
15
 * @package Selami\Entity\Parser
16
 */
17
class Yaml implements ParserInterface
18
{
19
    use ParserTrait;
20
21
    private $yamlParser = 'ext';
22
23
    private static $yamlParsers = ['ext','symfony'];
24
25
    /**
26
     * Yaml constructor.
27
     * @param string $yamlParser
28
     * @param  string $schemaConfig
29
     * @throws InvalidArgumentException
30
     * @throws UnexpectedValueException
31
     */
32
    public function __construct(string $yamlParser = 'ext', string $schemaConfig = null)
33
    {
34
        $this->setYamlParser($yamlParser);
35
        if ($schemaConfig !== null) {
36
            $this->setConfig($schemaConfig);
37
        }
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function parse()
44
    {
45
        $this->isConfigEmpty($this->schemaConfig);
46
        $schema = $this->yamlParse($this->schemaConfig);
0 ignored issues
show
Unused Code introduced by
The call to Yaml::yamlParse() has too many arguments starting with $this->schemaConfig.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
        return $schema;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $schema; (array|string|stdClass) is incompatible with the return type declared by the interface Selami\Entity\Interfaces\ParserInterface::parse of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function checkFormat()
54
    {
55
        try {
56
            $res = SymfonyYaml\Yaml::parse($this->schemaConfig);
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
            return true;
58
        } catch (SymfonyParseException $e) {
59
            return false;
60
        }
61
    }
62
63
    /**
64
     * @param string $selectedParser
65
     * @throws UnexpectedValueException
66
     */
67
    private function setYamlParser(string $selectedParser = 'ext')
68
    {
69
        if (!in_array($selectedParser, self::$yamlParsers, true)) {
70
            throw new UnexpectedValueException('Invalid parser. Possible values are: '
71
                . implode(', ', self::$yamlParsers));
72
        }
73
        $yamlParser = 'symfony';
74
        if (($selectedParser === 'ext') && extension_loaded('yaml')) {
75
            $yamlParser = 'ext';
76
        }
77
        $this->yamlParser = $yamlParser;
78
    }
79
80
    /**
81
     * @return array
82
     * @throws InvalidArgumentException
83
     */
84
    private function yamlParse()
85
    {
86
        if ($this->yamlParser === 'ext') {
87
            return $this->extParse();
88
        }
89
        return $this->symfonyParse();
90
    }
91
92
    /**
93
     * @return array
94
     * @throws InvalidArgumentException
95
     */
96
    private function extParse()
97
    {
98
        try {
99
            return yaml_parse($this->schemaConfig);
100
        } catch (\Exception $e) {
101
            throw new InvalidArgumentException($e->getMessage());
102
        }
103
    }
104
105
    /**
106
     * @return array
107
     * @throws InvalidArgumentException
108
     */
109
    private function symfonyParse()
110
    {
111
        try {
112
            return SymfonyYaml\Yaml::parse($this->schemaConfig);
113
        } catch (SymfonyParseException $e) {
114
            throw new InvalidArgumentException($e->getMessage());
115
        }
116
    }
117
}
118