Feed   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 98.48%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 32
eloc 69
c 4
b 0
f 0
dl 0
loc 143
ccs 65
cts 66
cp 0.9848
rs 9.84

4 Methods

Rating   Name   Duplication   Size   Complexity  
D getHandler() 0 51 19
A getDefaultNs() 0 3 1
A __construct() 0 5 1
B getAlias() 0 33 11
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 Psr\Log\NullLogger;
14
use SimplePie\Configuration as C;
15
use SimplePie\Exception\SimplePieException;
16
use SimplePie\Mixin as Tr;
17
use SimplePie\Parser\Date as DateParser;
18
use stdClass;
19
20
/**
21
 * The top-most element in a feed.
22
 *
23
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#411-the-atomfeed-element
24
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#53-channel
25
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#required-channel-elements
26
 * @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#top-level
27
 *
28
 * @phpcs:disable Generic.Files.LineLength.MaxExceeded
29
 *
30
 * @method array getAuthors(string $namespaceAlias) Returns the Authors associated with this feed.
31
 * @method array getCategories(string $namespaceAlias) Returns the list of Categories/Tags/Topics associated with this feed.
32
 * @method array getContributors(string $namespaceAlias) Returns the list of Contributors associated with this feed.
33
 * @method SimplePie\Type\Node getCopyright(string $namespaceAlias) Alias for `getRights()`.
34
 * @method array getEntries(string $namespaceAlias) Returns the list of Entries/Items associated with this feed.
35
 * @method SimplePie\Type\Generator getGenerator(string $namespaceAlias) Returns the Generator associated with this feed.
36
 * @method SimplePie\Type\Node getGuid(string $namespaceAlias) Alias for `getId()`.
37
 * @method SimplePie\Type\Node getId(string $namespaceAlias) Returns the ID associated with this feed.
38
 * @method SimplePie\Type\Image getIcon(string $namespaceAlias) Returns the Icon associated with this feed.
39
 * @method array getItems(string $namespaceAlias) Alias for `getEntries()`.
40
 * @method SimplePie\Type\Node getLang(string $namespaceAlias) Alias for `getLanguage()`.
41
 * @method SimplePie\Type\Node getLanguage(string $namespaceAlias) Returns the language associated with this feed.
42
 * @method array getLinks(string $namespaceAlias, string $relFilter) Returns the list of Links associated with this feed.
43
 * @method SimplePie\Type\Image getLogo(string $namespaceAlias) Returns the Logo associated with this feed.
44
 * @method \DateTime getPubDate(string $namespaceAlias) Alias for `getPublished()`.
45
 * @method \DateTime getPublished(string $namespaceAlias) Returns the date that the feed was published.
46
 * @method SimplePie\Type\Node getRights(string $namespaceAlias) Returns the copyright information associated with this feed.
47
 * @method SimplePie\Type\Node getSubtitle(string $namespaceAlias) Returns the sub-title associated with this feed.
48
 * @method SimplePie\Type\Node getSummary(string $namespaceAlias) Returns the summary associated with this feed.
49
 * @method SimplePie\Type\Node getTitle(string $namespaceAlias) Returns the title associated with this feed.
50
 * @method \DateTime getUpdated(string $namespaceAlias) Returns the date that the feed was updated.
51
 *
52
 * @phpcs:enable
53
 */
54
class Feed extends AbstractType implements BranchInterface, C\SetLoggerInterface
55
{
56
    use Tr\DateTrait;
57
    use Tr\DeepTypeTrait;
58
    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...
59
    use Tr\RootTrait;
60
61
    /**
62
     * The preferred namespace alias for a given XML namespace URI. Should be
63
     * the result of a call to `SimplePie\Util\Ns`.
64
     *
65
     * @var string
66
     */
67
    protected $namespaceAlias;
68
69
    /**
70
     * Constructs a new instance of this class.
71
     *
72
     * @param string $namespaceAlias [description]
73
     */
74 560
    public function __construct(string $namespaceAlias)
75
    {
76 560
        $this->root           = new stdClass();
77 560
        $this->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...
78 560
        $this->namespaceAlias = $namespaceAlias;
79 560
    }
80
81
    /**
82
     * Finds the default namespace alias for the feed type.
83
     */
84 3
    public function getDefaultNs(): string
85
    {
86 3
        return $this->namespaceAlias;
87
    }
88
89
    /**
90
     * Finds the common internal alias for a given method name.
91
     *
92
     * @param string $nodeName The name of the method being called.
93
     *
94
     * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
95
     */
96 560
    public function getAlias(string $nodeName): string
97
    {
98 560
        switch ($nodeName) {
99 560
            case 'authors':
100 13
                return 'author';
101
102 547
            case 'categories':
103 3
                return 'category';
104
105 544
            case 'contributors':
106 9
                return 'contributor';
107
108 535
            case 'copyright':
109
                return 'rights';
110
111 535
            case 'entries':
112 372
            case 'items':
113 180
                return 'entry';
114
115 372
            case 'guid':
116 1
                return 'id';
117
118 372
            case 'language':
119 2
                return 'lang';
120
121 370
            case 'links':
122 20
                return 'link';
123
124 350
            case 'pubDate':
125 1
                return 'published';
126
127
            default:
128 349
                return $nodeName;
129
        }
130
    }
131
132
    // @phpcs:enable
133
134
    /**
135
     * Get the correct handler for a whitelisted method name.
136
     *
137
     * @param string $nodeName The name of the method being called.
138
     * @param array  $args     Any arguments passed into that method. The default value is an empty array.
139
     *
140
     * @throws SimplePieException
141
     *
142
     * @return mixed Either `TypeInterface` or `TypeInterface[]`.
143
     *
144
     * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
145
     */
146 560
    public function getHandler(string $nodeName, array $args = [])
147
    {
148 560
        switch ($nodeName) {
149 560
            case 'base':
150 560
            case 'id':
151 557
            case 'lang':
152 549
            case 'rights':
153 535
            case 'subtitle':
154 522
            case 'summary':
155 520
            case 'title':
156 332
                return $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null);
157
158 245
            case 'published':
159 240
            case 'updated':
160 6
                return (new DateParser(
161 6
                    $this->getScalarSingleValue($this->getRoot(), $nodeName, $args[0] ?? null)->getValue(),
162 6
                    $this->outputTimezone,
163 6
                    $this->createFromFormat
164 6
                ))->getDateTime();
165
166 239
            case 'generator':
167 7
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Generator::class, $args[0] ?? null);
168
169 232
            case 'icon':
170 230
            case 'logo':
171 6
                return $this->getComplexSingleValue($this->getRoot(), $nodeName, Image::class, $args[0] ?? null);
172
173 226
            case 'author':
174 213
            case 'category':
175 210
            case 'contributor':
176 201
            case 'entry':
177 205
                return $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
178
179 21
            case 'link':
180 20
                $links = $this->getComplexMultipleValues($this->getRoot(), $nodeName, $args[0] ?? null);
181
182 20
                if (isset($args[1])) {
183 1
                    $relFilter = $args[1];
184
185 1
                    return \array_values(
186
                        \array_filter($links, static function (Link $link) use ($relFilter) {
187 1
                            return $relFilter === $link->getRel()->getValue();
188 1
                        })
189
                    );
190
                }
191
192 19
                return $links;
193
194
            default:
195 1
                throw new SimplePieException(
196 1
                    $this->getUnresolvableMessage($nodeName)
197
                );
198
        }
199
    }
200
201
    // @phpcs:enable
202
}
203