1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml\Nodes; |
4
|
|
|
|
5
|
|
|
use Dallgoot\Yaml\NodeList; |
6
|
|
|
use Dallgoot\Yaml\TagFactory; |
7
|
|
|
use Dallgoot\Yaml\Tagged; |
8
|
|
|
use Dallgoot\Yaml\Regex; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @author Stéphane Rebai <[email protected]> |
13
|
|
|
* @license Apache 2.0 |
14
|
|
|
* @link https://github.com/dallgoot/yaml |
15
|
|
|
* @todo handle tags like <tag:clarkevans.com,2002:invoice> |
16
|
|
|
*/ |
17
|
|
|
class Tag extends Actions |
18
|
|
|
{ |
19
|
|
|
|
20
|
1 |
|
public function isAwaitingChild(NodeGeneric $node):bool |
21
|
|
|
{ |
22
|
1 |
|
return is_null($this->value); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric |
26
|
|
|
{ |
27
|
1 |
|
if (is_null($this->value)) { |
28
|
1 |
|
return $this; |
29
|
|
|
} else { |
30
|
1 |
|
return $this->getParent(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Builds a tag and its value (also built) and encapsulates them in a Tag object. |
36
|
|
|
* |
37
|
|
|
* @param array|object|null $parent The parent |
38
|
|
|
* |
39
|
|
|
* @return Tagged|mixed The tag object of class Dallgoot\Yaml\Tagged. |
40
|
|
|
*/ |
41
|
1 |
|
public function build(&$parent = null) |
42
|
|
|
{ |
43
|
1 |
|
if (is_null($this->value) && $this->getParent() instanceof Root) { |
44
|
1 |
|
if (!preg_match(Regex::TAG_PARTS, $this->tag, $matches)) { |
45
|
|
|
throw new \UnexpectedValueException("Tag '$this->tag' is invalid", 1); |
46
|
|
|
} |
47
|
|
|
// var_dump($matches['handle'], $matches['tagname']); |
48
|
1 |
|
$handle = $matches['handle']; |
49
|
1 |
|
$tagname = $matches['tagname']; |
50
|
1 |
|
$this->getRoot()->getYamlObject()->addTag($handle, $tagname); |
|
|
|
|
51
|
1 |
|
return; |
52
|
|
|
} |
53
|
1 |
|
$value = $this->value; |
54
|
1 |
|
if (is_null($parent) && $value->isOneOf('Item', 'Key')) { |
|
|
|
|
55
|
|
|
$value = new NodeList(/** @scrutinizer ignore-type */ $value); |
56
|
|
|
} |
57
|
|
|
// if (TagFactory::isKnown((string) $this->tag)) { |
58
|
|
|
try { |
59
|
1 |
|
if ($value instanceof Literals) { |
60
|
|
|
$value = $value->value; |
61
|
|
|
} |
62
|
1 |
|
$transformed = TagFactory::transform((string) $this->tag, $value); |
63
|
1 |
|
if ($transformed instanceof NodeGeneric || $transformed instanceof NodeList) { |
64
|
|
|
return $transformed->build($parent); |
65
|
|
|
} |
66
|
1 |
|
return $transformed; |
67
|
|
|
// } else { |
68
|
|
|
} catch (\Throwable $e) { |
69
|
|
|
if ($e instanceof \UnexpectedValueException) { |
70
|
|
|
return new Tagged($this->tag, is_null($value) ? null : $value->build($parent)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
throw new \Exception("Can NOT build Tag", 1, $e); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
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. Please note the @ignore annotation hint above.