Passed
Push — master ( f81cc4...5281ab )
by stéphane
04:50
created

Directive   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 42
ccs 9
cts 16
cp 0.5625
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A build() 0 22 6
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml;
6
use Dallgoot\Yaml\Regex;
7
use Dallgoot\Yaml\TagFactory;
8
9
/**
10
 *
11
 * @author  Stéphane Rebai <[email protected]>
12
 * @license Apache 2.0
13
 * @link    https://github.com/dallgoot/yaml
14
 */
15
class Directive extends NodeGeneric
16
{
17
    private const ERROR_BUILDING = "Error : can not build Directive";
18
    private const WARNING_LOWER_VERSION  = "The declared version '%f' is obsolete, there may be features that are deprecated and therefore not handled, minimum supported is :".Yaml::VERSION_SUPPORT;
19
    private const WARNING_HIGHER_VERSION = "The declared version '%f' is not yet supported, minimum supported is :".Yaml::VERSION_SUPPORT;
20
21
    /**
22
     * Builds a Directive : update YamlObject if applicable.
23
     *
24
     * @param      object|array       $parent  The parent
25
     *
26
     * @throws     \ParseError  If Tag handle has been already set before.
27
     *
28
     * @return     null
29
     */
30 1
    public function build(&$parent = null)
31
    {
32 1
        if (preg_match(Regex::DIRECTIVE_TAG, $this->raw, $matches)) {
33
            try {
34
                $yamlObject = $this->getRoot()->getYamlObject();
35
                //Try registering the handle in TagFactory
36
                TagFactory::registerHandle($matches['handle'], $matches['prefix']);
37
                $yamlObject->addTag($matches['handle'], $matches['prefix']);
0 ignored issues
show
Unused Code introduced by
The call to Dallgoot\Yaml\YamlObject::addTag() has too many arguments starting with $matches['prefix']. ( Ignorable by Annotation )

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

37
                $yamlObject->/** @scrutinizer ignore-call */ 
38
                             addTag($matches['handle'], $matches['prefix']);

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.

Loading history...
38
            } catch (\Throwable $e) {
39
                throw new \ParseError(self::ERROR_BUILDING, 1, $e);
40
            }
41
        }
42 1
        if (preg_match(Regex::DIRECTIVE_VERSION, $this->raw, $matches)) {
43 1
            $contentVersion = (float) $matches['version'];
44 1
            if ($contentVersion > Yaml::VERSION_SUPPORT) {
45
                trigger_error(sprintf(self::WARNING_HIGHER_VERSION, $contentVersion), E_USER_WARNING);
46
            }
47 1
            if ($contentVersion < Yaml::VERSION_SUPPORT) {
48
                trigger_error(sprintf(self::WARNING_LOWER_VERSION, $contentVersion), E_USER_WARNING);
49
            }
50
        }
51 1
        return null;
52
    }
53
54 1
    public function add(NodeGeneric $child):NodeGeneric
55
    {
56 1
        return $child;
57
    }
58
}