Completed
Push — master ( 8146b2...061ea8 )
by Ryan
03:02
created

Feed::getAlias()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 11
nop 1
dl 0
loc 33
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
                            return $relFilter === $link->/** @scrutinizer ignore-call */ getRel()->getValue();
Loading history...
191
                        })
192
                    );
193
                }
194
195
                return $links;
196
197
            default:
198
                throw new SimplePieException(
199
                    $this->getUnresolvableMessage($nodeName)
200
                );
201
        }
202
    }
203
204
    // @phpcs:enable
205
}
206