Generator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 28
c 1
b 0
f 0
dl 0
loc 98
ccs 25
cts 25
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 8 2
A getHandler() 0 14 4
A getNode() 0 3 1
A __toString() 0 3 1
A __construct() 0 9 3
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\Type;
12
13
use DOMNode;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
use SimplePie\Configuration as C;
17
use SimplePie\Exception\SimplePieException;
18
use SimplePie\Mixin as Tr;
19
20
/**
21
 * A type model for a Generator element.
22
 *
23
 * @method SimplePie\Type\Node getName() Returns the name of the Generator.
24
 * @method SimplePie\Type\Node getUri() Alias for `getUrl()`.
25
 * @method SimplePie\Type\Node getUrl() Returns the URL of the Generator.
26
 * @method SimplePie\Type\Node getVersion() Returns the version of the Generator.
27
 *
28
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#424-the-atomgenerator-element
29
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#optional-channel-elements
30
 */
31
class Generator extends AbstractType implements C\SetLoggerInterface, NodeInterface, TypeInterface
32
{
33
    use Tr\LoggerTrait;
0 ignored issues
show
Bug introduced by
The type SimplePie\Mixin\LoggerTrait 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...
34
35
    /**
36
     * The DOMNode element to parse.
37
     *
38
     * @var DOMNode
39
     */
40
    protected $node;
41
42
    /**
43
     * The generator name.
44
     *
45
     * @var string
46
     */
47
    protected $name;
48
49
    /**
50
     * The generator URI.
51
     *
52
     * @var string
53
     */
54
    protected $uri;
55
56
    /**
57
     * The generator version.
58
     *
59
     * @var string
60
     */
61
    protected $version;
62
63
    /**
64
     * Constructs a new instance of this class.
65
     *
66
     * @param DOMNode|null    $node   The `DOMNode` element to parse.
67
     * @param LoggerInterface $logger The PSR-3 logger.
68
     */
69 44
    public function __construct(?DOMNode $node = null, LoggerInterface $logger = null)
70
    {
71 44
        if ($node) {
72 44
            $this->logger = $logger ?? new NullLogger();
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73 44
            $this->node   = $node;
74 44
            $this->name   = new Node($this->node);
75
76 44
            foreach ($this->node->attributes as $attribute) {
77 44
                $this->{$attribute->name} = new Node($attribute);
78
            }
79
        }
80 44
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function __toString(): string
86
    {
87 2
        return \trim(\sprintf('%s %s', $this->name, $this->version));
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 7
    public function getNode(): ?DOMNode
94
    {
95 7
        return $this->node;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 6
    public function getAlias(string $nodeName): string
102
    {
103 6
        switch ($nodeName) {
104 6
            case 'url':
105 3
                return 'uri';
106
107
            default:
108 6
                return $nodeName;
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 6
    public function getHandler(string $nodeName, array $args = []): Node
116
    {
117
        // Shut up, linter.
118
        $args;
119
120 6
        switch ($nodeName) {
121 6
            case 'name':
122 5
            case 'uri':
123 3
            case 'version':
124 5
                return $this->{$nodeName} ?? new Node();
125
126
            default:
127 1
                throw new SimplePieException(
128 1
                    $this->getUnresolvableMessage($nodeName)
129
                );
130
        }
131
    }
132
}
133