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); |
|
|
|
|
47
|
|
|
return $schema; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function checkFormat() |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$res = SymfonyYaml\Yaml::parse($this->schemaConfig); |
|
|
|
|
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
|
|
|
|
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.