Completed
Pull Request — project/php-7 (#133)
by Alex
04:04 queued 01:49
created

Media::getUrlAttributeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\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
    protected $urlAttributeName = 'url';
24
25
    /**
26
     * @return string
27
     */
28 5
    public function getUrlAttributeName()
29
    {
30 5
        return $this->urlAttributeName;
31
    }
32
33
    /**
34
     * @param  string $name
35
     * @return $this
36
     */
37 8
    public function setUrlAttributeName($name)
38
    {
39 8
        $this->urlAttributeName = $name;
40
41 8
        return $this;
42
    }
43
44
    /**
45
     * @param  NodeInterface $node
46
     * @param  \DOMElement   $element
47
     * @return $this
48
     */
49 4
    public function setProperty(NodeInterface $node, \DOMElement $element)
50
    {
51 4
        if ($node instanceof ItemInterface) {
52 4
            $media = $node->newMedia();
53 4
            $media->setType($this->getAttributeValue($element, 'type'))
54 4
                ->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName()))
55 4
                ->setLength($this->getAttributeValue($element, 'length'));
56
57 4
            $node->addMedia($media);
58
        }
59
60 4
        return $this;
61
    }
62
63
    /**
64
     * @param  \DomDocument   $document
65
     * @param  MediaInterface $media
66
     * @return \DomElement
67
     */
68 1
    public function createMediaElement(\DomDocument $document, MediaInterface $media)
69
    {
70 1
        $element = $document->createElement($this->getNodeName());
71 1
        $element->setAttribute($this->getUrlAttributeName(), $media->getUrl());
72 1
        $element->setAttribute('type', $media->getType());
73 1
        $element->setAttribute('length', $media->getLength());
74
75 1
        return $element;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 2
    protected function hasValue(NodeInterface $node) : bool
82
    {
83 2
        return $node instanceof ItemInterface && !! $node->getMedias();
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 2
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
90
    {
91 2
        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...
92 1
            $rootElement->appendChild($this->createMediaElement($document, $media));
93
        }
94 2
    }
95
}
96