Completed
Pull Request — master (#97)
by Alex
05:49 queued 03:13
created

XmlParser::checkBodyStructure()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 2
crap 4
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Parser;
12
13
14
use FeedIo\Parser;
15
use FeedIo\RuleSet;
16
use FeedIo\FeedInterface;
17
use FeedIo\Feed\NodeInterface;
18
use FeedIo\ParserAbstract;
19
use FeedIo\Reader\Document;
20
use FeedIo\Parser\MissingFieldsException;
21
use FeedIo\Parser\UnsupportedFormatException;
22
23
/**
24
 * Parses a DOM document if its format matches the parser's standard
25
 *
26
 * Depends on :
27
 *  - FeedIo\StandardAbstract
28
 *  - Psr\Log\LoggerInterface
29
 *
30
 */
31
class XmlParser extends ParserAbstract
32
{
33
34
    /**
35
     * @param $tagName
36
     * @return bool
37
     */
38 8
    public function isItem($tagName)
39
    {
40 8
        return (strtolower($this->standard->getItemNodeName()) === strtolower($tagName));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FeedIo\StandardAbstract as the method getItemNodeName() does only exist in the following sub-classes of FeedIo\StandardAbstract: FeedIo\Standard\Atom, FeedIo\Standard\Rdf, FeedIo\Standard\Rss, FeedIo\Standard\XmlAbstract. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
41
    }
42
43
    /**
44
     * @param  Document                       $document
45
     * @param  FeedInterface                  $feed
46
     * @return \FeedIo\FeedInterface
47
     * @throws Parser\MissingFieldsException
48
     * @throws Parser\UnsupportedFormatException
49
     */
50 8
    public function parseContent(Document $document, FeedInterface $feed)
51
    {
52 8
        $element = $this->standard->getMainElement($document->getDOMDocument());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FeedIo\StandardAbstract as the method getMainElement() does only exist in the following sub-classes of FeedIo\StandardAbstract: FeedIo\Standard\Atom, FeedIo\Standard\Rdf, FeedIo\Standard\Rss, FeedIo\Standard\XmlAbstract. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
53
54 8
        $this->parseNode($feed, $element, $this->standard->getFeedRuleSet());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FeedIo\StandardAbstract as the method getFeedRuleSet() does only exist in the following sub-classes of FeedIo\StandardAbstract: FeedIo\Standard\Atom, FeedIo\Standard\Rdf, FeedIo\Standard\Rss, FeedIo\Standard\XmlAbstract. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
55
56 8
        return $feed;
57
    }
58
59
    /**
60
     * @param  Document            $document
61
     * @param  array                  $mandatoryFields
62
     * @return $this
63
     * @throws MissingFieldsException
64
     */
65 11
    public function checkBodyStructure(Document $document, array $mandatoryFields)
66
    {
67 11
        $errors = array();
68
69 11
        $element = $document->getDOMDocument()->documentElement;
70 11
        foreach ($mandatoryFields as $field) {
71 6
            $list = $element->getElementsByTagName($field);
72 6
            if (0 === $list->length) {
73 2
                $errors[] = $field;
74 2
            }
75 11
        }
76
77 11
        if (!empty($errors)) {
78 2
            $message = "missing mandatory field(s) : ".implode(',', $errors);
79 2
            $this->logger->warning($message);
80 2
            throw new MissingFieldsException($message);
81
        }
82
83 9
        return $this;
84
    }
85
86
    /**
87
     * @param  NodeInterface $item
88
     * @param  \DOMElement   $element
89
     * @param  RuleSet       $ruleSet
90
     * @return NodeInterface
91
     */
92 9
    public function parseNode(NodeInterface $item, \DOMElement $element, RuleSet $ruleSet)
93
    {
94 9
        foreach ($element->childNodes as $node) {
95 8
            if ($node instanceof \DOMElement) {
96 8
                $this->handleNode($item, $node, $ruleSet);
97 8
            }
98 9
        }
99
100 9
        return $item;
101
    }
102
103
    /**
104
     * @param NodeInterface $item
105
     * @param \DOMElement $node
106
     * @param RuleSet $ruleSet
107
     * @return $this
108
     */
109 8
    protected function handleNode(NodeInterface $item, \DOMElement $node, RuleSet $ruleSet)
110
    {
111 8
        if ($this->isItem($node->tagName) && $item instanceof FeedInterface) {
112 7
            $newItem = $this->parseNode($item->newItem(), $node, $this->standard->getItemRuleSet());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FeedIo\StandardAbstract as the method getItemRuleSet() does only exist in the following sub-classes of FeedIo\StandardAbstract: FeedIo\Standard\Atom, FeedIo\Standard\Rdf, FeedIo\Standard\Rss, FeedIo\Standard\XmlAbstract. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
113 7
            $this->addValidItem($item, $newItem);
114 7
        } else {
115 8
            $rule = $ruleSet->get($node->tagName);
116 8
            $rule->setProperty($item, $node);
117
        }
118
119 8
        return $this;
120
    }
121
122
123
}
124