Person   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 15
eloc 38
c 3
b 1
f 0
dl 0
loc 126
ccs 31
cts 33
cp 0.9394
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 8 2
A getHandler() 0 15 5
A getNode() 0 3 1
A __toString() 0 12 4
A __construct() 0 13 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 Person element.
22
 *
23
 * @method SimplePie\Type\Node getAvatar() Returns the avatar URL of the Person.
24
 * @method SimplePie\Type\Node getEmail() Returns the email address of the Person.
25
 * @method SimplePie\Type\Node getName() Returns the name of the Person.
26
 * @method SimplePie\Type\Node getUri() Alias for `getUrl()`.
27
 * @method SimplePie\Type\Node getUrl() Returns the URL of the Person.
28
 *
29
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#32-person-constructs
30
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#author-sub-element-of-item
31
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#top-level
32
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-iTunes-Podcast-RSS#itunesauthor
33
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-iTunes-Podcast-RSS#itunesowner
34
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Media-RSS#mediacredit
35
 */
36
class Person extends AbstractType implements C\SetLoggerInterface, NodeInterface, TypeInterface
37
{
38
    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...
39
40
    /**
41
     * The DOMNode element to parse.
42
     *
43
     * @var DOMNode
44
     */
45
    protected $node;
46
47
    /**
48
     * The person's name.
49
     *
50
     * @var Node
51
     */
52
    protected $name;
53
54
    /**
55
     * The person's URL.
56
     *
57
     * @var Node
58
     */
59
    protected $uri;
60
61
    /**
62
     * The person's URL.
63
     *
64
     * @var Node
65
     */
66
    protected $url;
67
68
    /**
69
     * The person's email address.
70
     *
71
     * @var Node
72
     */
73
    protected $email;
74
75
    /**
76
     * The person's avatar.
77
     *
78
     * @var Node
79
     */
80
    protected $avatar;
81
82
    /**
83
     * Constructs a new instance of this class.
84
     *
85
     * @param DOMNode|null    $node   The `DOMNode` element to parse.
86
     * @param LoggerInterface $logger The PSR-3 logger.
87
     */
88 76
    public function __construct(?DOMNode $node = null, LoggerInterface $logger = null)
89
    {
90 76
        if ($node) {
91 76
            $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...
92 76
            $this->node   = $node;
93 76
            $this->name   = new Node($this->node);
94
95 76
            foreach ($this->node->childNodes as $child) {
96 76
                $this->{$child->tagName} = new Node($child);
97
            }
98
99
            // Map to url if uri doesn't exist.
100 76
            $this->uri = $this->uri ?? $this->url ?? null;
101
        }
102 76
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 6
    public function __toString(): string
108
    {
109 6
        if (null !== $this->name && (null !== $this->uri || null !== $this->email)
110
        ) {
111 6
            return \trim(\sprintf('%s <%s>', (string) $this->name, (string) ($this->uri ?? $this->email)));
112
        }
113
114 1
        return \trim(
115 1
            (string) $this->name
116
            ?? (string) $this->uri
117
            ?? (string) $this->email
118 1
            ?? 'Unknown'
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 2
    public function getNode(): ?DOMNode
126
    {
127 2
        return $this->node;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 36
    public function getAlias(string $nodeName): string
134
    {
135 36
        switch ($nodeName) {
136 36
            case 'url':
137 11
                return 'uri';
138
139
            default:
140 28
                return $nodeName;
141
        }
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 36
    public function getHandler(string $nodeName, array $args = []): Node
148
    {
149
        // Shut up, linter.
150
        $args;
151
152 36
        switch ($nodeName) {
153 36
            case 'name':
154 28
            case 'uri':
155 11
            case 'email':
156 1
            case 'avatar':
157 35
                return $this->{$nodeName} ?? new Node();
158
159
            default:
160 1
                throw new SimplePieException(
161 1
                    $this->getUnresolvableMessage($nodeName)
162
                );
163
        }
164
    }
165
}
166