Passed
Push — master ( 59a82b...2f34e5 )
by Ryan
14:52
created

Atom::__invoke()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 37
nc 36
nop 3
dl 0
loc 52
rs 7.2396
c 0
b 0
f 0

How to fix   Long Method   

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 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Middleware\Xml;
12
13
use DOMXPath;
14
use SimplePie\Configuration as C;
15
use SimplePie\Mixin as Tr;
16
use SimplePie\Type as T;
17
use stdClass;
18
19
/**
20
 * Support for the Atom 1.0 grammar.
21
 *
22
 * @see https://tools.ietf.org/html/rfc4287
23
 * @see https://www.w3.org/wiki/Atom
24
 */
25
class Atom extends AbstractXmlMiddleware implements XmlInterface, C\SetLoggerInterface
26
{
27
    use Tr\LoggerTrait;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __invoke(stdClass $feedRoot, string $namespaceAlias, DOMXPath $xpath): void
33
    {
34
        // lang (single, scalar)
35
        $this->addArrayProperty($feedRoot, 'lang');
36
        $xq = $xpath->query($this->applyNsToQuery('/%s:feed[attribute::xml:lang][1]/@xml:lang', $namespaceAlias));
37
38
        $feedRoot->lang[$namespaceAlias] = ($xq->length > 0)
39
            ? T\Node::factory((string) $xq->item(0)->nodeValue)
40
            : null;
41
42
        // single, scalar types
43
        foreach ([
44
            'icon',
45
            'id',
46
            'logo',
47
            'published',
48
            'rights',
49
            'subtitle',
50
            'summary',
51
            'title',
52
            'updated',
53
        ] as $nodeName) {
54
            $this->addArrayProperty($feedRoot, $nodeName);
55
            $feedRoot->{$nodeName}[$namespaceAlias] = $this->getSingle($nodeName, $namespaceAlias, $xpath);
56
        }
57
58
        // single, complex types
59
        foreach ([
60
            'author' => T\Person::class,
61
            'generator' => T\Generator::class,
62
        ] as $name => $class) {
63
            $this->addArrayProperty($feedRoot, $name);
64
            $xq = $xpath->query($this->generateQuery($namespaceAlias, true, 'feed', $name));
65
66
            $feedRoot->{$name}[$namespaceAlias] = ($xq->length > 0)
67
                ? new $class($xq->item(0), $this->getLogger())
68
                : null;
69
        }
70
71
        // multiple, complex types
72
        foreach ([
73
            'category' => T\Category::class,
74
            'contributor' => T\Person::class,
75
            'link' => T\Link::class,
76
        ] as $name => $class) {
77
            $this->addArrayProperty($feedRoot, $name);
78
            $xq = $xpath->query($this->generateQuery($namespaceAlias, true, 'feed', $name));
79
80
            $feedRoot->{$name}[$namespaceAlias] = [];
81
82
            foreach ($xq as $result) {
83
                $feedRoot->{$name}[$namespaceAlias][] = new $class($result, $this->getLogger());
84
            }
85
        }
86
    }
87
88
    /**
89
     * Find and read the contents of the feed-level nodes which only have a single value.
90
     *
91
     * @param string   $nodeName       The name of the namespaced XML node to read.
92
     * @param string   $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result
93
     *                                 of a call to `SimplePie\Util\Ns`.
94
     * @param DOMXPath $xpath          The `DOMXPath` object with this middleware's namespace alias applied.
95
     *
96
     * @return Node
0 ignored issues
show
Bug introduced by
The type SimplePie\Middleware\Xml\Node 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...
97
     */
98
    protected function getSingle(string $nodeName, string $namespaceAlias, DOMXPath $xpath): T\Node
99
    {
100
        $query = $this->generateQuery($namespaceAlias, true, 'feed', $nodeName);
101
        $this->getLogger()->debug(\sprintf('%s is running an XPath query:', __CLASS__), [$query]);
102
103
        return $this->handleSingleNode(static function () use ($xpath, $query) {
104
            return $xpath->query($query);
105
        });
106
    }
107
}
108