DeepTypeTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 4
b 0
f 0
dl 0
loc 90
ccs 12
cts 15
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getComplexMultipleValues() 0 14 2
A getComplexSingleValue() 0 15 2
A getScalarSingleValue() 0 14 2
1
<?php
2
/**
3
 * Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2019 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Mixin;
12
13
use SimplePie\Type\Node;
14
use SimplePie\Type\TypeInterface;
15
use Skyzyx\UtilityPack\Types;
0 ignored issues
show
Bug introduced by
The type Skyzyx\UtilityPack\Types was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * Shared code for working with deeply-nested elements for types.
19
 */
20
trait DeepTypeTrait
21
{
22
    /**
23
     * The preferred alias for a particular XML Namespace URI.
24
     *
25
     * @var string
26
     */
27
    protected $namespaceAlias;
28
29
    /**
30
     * Retrieves nodes that are simple scalars, and there is only one allowed value.
31
     *
32
     * @param object      $root           The root node for performing the lookup. Expected to be either a `stdClass`
33
     *                                    (for `Feed`), or an `SimplePie\Type\Entry` instance.
34
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
35
     *                                    looking at the response from `getRoot()`.
36
     * @param string|null $namespaceAlias The XML namespace alias to apply.
37
     *
38
     * @phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
39
     */
40 459
    protected function getScalarSingleValue(
41
        object $root,
42
        string $nodeName,
43
        ?string $namespaceAlias = null
44
    ): Node {
45
        // @phpcs:enable
46
47 459
        $alias = $namespaceAlias ?? $this->namespaceAlias;
48
49 459
        if (isset($root->{$nodeName}[$alias])) {
50 459
            return $root->{$nodeName}[$alias];
51
        }
52
53
        return new Node();
54
    }
55
56
    /**
57
     * Retrieves nodes that are complex types, and there is only one allowed value.
58
     *
59
     * @param object      $root           The root node for performing the lookup. Expected to be either a `stdClass`
60
     *                                    (for `Feed`), or an `SimplePie\Type\Entry` instance.
61
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
62
     *                                    looking at the response from `getRoot()`.
63
     * @param string      $className      The class name to instantiate when there is not a defined value.
64
     * @param string|null $namespaceAlias The XML namespace alias to apply.
65
     *
66
     * @phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
67
     */
68 13
    protected function getComplexSingleValue(
69
        object $root,
70
        string $nodeName,
71
        string $className,
72
        ?string $namespaceAlias = null
73
    ): TypeInterface {
74
        // @phpcs:enable
75
76 13
        $alias = $namespaceAlias ?? $this->namespaceAlias;
77
78 13
        if (isset($root->{$nodeName}[$alias])) {
79 13
            return new $className($root->{$nodeName}[$alias]->getNode());
80
        }
81
82
        return new $className();
83
    }
84
85
    /**
86
     * Retrieves nodes that are complex types, and there may be are more than one value.
87
     *
88
     * @param object      $root           The root node for performing the lookup. Expected to be either a `stdClass`
89
     *                                    (for `Feed`), or an `SimplePie\Type\Entry` instance.
90
     * @param string      $nodeName       The name of the tree node to retrieve. Available tree nodes can be viewed by
91
     *                                    looking at the response from `getRoot()`.
92
     * @param string|null $namespaceAlias The XML namespace alias to apply.
93
     *
94
     * @phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine
95
     */
96 225
    protected function getComplexMultipleValues(
97
        object $root,
98
        string $nodeName,
99
        ?string $namespaceAlias = null
100
    ): iterable {
101
        // @phpcs:enable
102
103 225
        $alias = $namespaceAlias ?? $this->namespaceAlias;
104
105 225
        if (isset($root->{$nodeName}[$alias])) {
106 225
            return $root->{$nodeName}[$alias];
107
        }
108
109
        return [];
110
    }
111
}
112