Completed
Pull Request — master (#248)
by
unknown
02:23
created

Media::addElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php declare(strict_types=1);
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\Rule;
12
13
use FeedIo\Feed\Item;
14
use FeedIo\Feed\Item\MediaInterface;
15
use FeedIo\Feed\ItemInterface;
16
use FeedIo\Feed\NodeInterface;
17
use FeedIo\RuleAbstract;
18
19
class Media extends RuleAbstract
20
{
21
    const NODE_NAME = 'enclosure';
22
23
    const MRSS_NAMESPACE = "http://search.yahoo.com/mrss/";
24
25
    protected $urlAttributeName = 'url';
26
27
    /**
28
     * @return string
29
     */
30 5
    public function getUrlAttributeName() : string
31
    {
32 5
        return $this->urlAttributeName;
33
    }
34
35
    /**
36
     * @param  string $name
37
     */
38 12
    public function setUrlAttributeName(string $name) : void
39
    {
40 12
        $this->urlAttributeName = $name;
41 12
    }
42
43
    /**
44
     * @param  NodeInterface $node
45
     * @param  \DOMElement   $element
46
     */
47 5
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
48
    {
49 5
        if ($node instanceof ItemInterface) {
50 5
            $media = $node->newMedia();
51 5
            $media->setNodeName($element->nodeName);
52
53 5
            if ($element->nodeName == "media:group" or $element->nodeName == "media:content") {
54 1
                $media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE));
55 1
                $media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE));
56 1
                $media->setThumbnail($this->getChildAttributeValue($element, 'thumbnail', 'url', static::MRSS_NAMESPACE));
57
58 1
                if ($element->nodeName == "media:content") {
59
                    $media->setUrl($this->getAttributeValue($element, "url"));
60
                }
61
62 1
                if ($element->nodeName == "media:group") {
63 1
                    $media->setUrl($this->getChildAttributeValue($element, 'content', 'url', static::MRSS_NAMESPACE));
64
                }
65
            } else {
66
                $media
67 4
                    ->setType($this->getAttributeValue($element, 'type'))
68 4
                    ->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName()))
69 4
                    ->setLength($this->getAttributeValue($element, 'length'));
70
            }
71 5
            $node->addMedia($media);
72
        }
73 5
    }
74
75
    /**
76
     * @param  \DomDocument   $document
77
     * @param  MediaInterface $media
78
     * @return \DomElement
79
     */
80 1
    public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement
81
    {
82 1
        $element = $document->createElement($this->getNodeName());
83 1
        $element->setAttribute($this->getUrlAttributeName(), $media->getUrl());
84 1
        $element->setAttribute('type', $media->getType() ?? '');
85 1
        $element->setAttribute('length', $media->getLength() ?? '');
86
87 1
        return $element;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93 5
    protected function hasValue(NodeInterface $node) : bool
94
    {
95 5
        return $node instanceof ItemInterface && !! $node->getMedias();
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101 5
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
102
    {
103 5
        foreach ($node->getMedias() as $media) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FeedIo\Feed\NodeInterface as the method getMedias() does only exist in the following implementations of said interface: FeedIo\Feed\Item.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
104 1
            $rootElement->appendChild($this->createMediaElement($document, $media));
105
        }
106 5
    }
107
}
108