Passed
Pull Request — master (#38)
by Ryan
25:32 queued 10:46
created

DeepTypeTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 6

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