Completed
Push — issue/131 ( a63de6 )
by Alex
15:36
created

LinkNode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 57
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setProperty() 0 10 2
A createElement() 0 12 3
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\Atom;
12
13
use FeedIo\Feed\ItemInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\RuleAbstract;
16
use FeedIo\RuleSet;
17
18
class LinkNode extends RuleAbstract
19
{
20
    const NODE_NAME = 'link';
21
22
    /**
23
     * @var \FeedIo\RuleSet
24
     */
25
    protected $ruleSet;
26
27
    /**
28
     * @param string $nodeName
29
     */
30 9
    public function __construct($nodeName = null)
31
    {
32 9
        parent::__construct($nodeName);
33 9
        $mediaRule = new Media();
34 9
        $mediaRule->setUrlAttributeName('href');
35 9
        $this->ruleSet = new RuleSet(new Link('related'));
36 9
        $this->ruleSet->add($mediaRule, ['media', 'enclosure']);
37 9
    }
38
39
    /**
40
     * @param  NodeInterface $node
41
     * @param  \DOMElement   $element
42
     * @return mixed
43
     */
44 5
    public function setProperty(NodeInterface $node, \DOMElement $element)
45
    {
46 5
        if ($element->hasAttribute('rel')) {
47 3
            $this->ruleSet->get($element->getAttribute('rel'))->setProperty($node, $element);
48 3
        } else {
49 4
            $this->ruleSet->getDefault()->setProperty($node, $element);
50
        }
51
52 5
        return $this;
53
    }
54
55
    /**
56
     * creates the accurate DomElement content according to the $item's property
57
     *
58
     * @param  \DomDocument  $document
59
     * @param  NodeInterface $node
60
     * @return \DomElement
61
     */
62 2
    public function createElement(\DomDocument $document, NodeInterface $node)
63
    {
64 2
        $output = new \ArrayIterator();
65 2
        if ($node instanceof ItemInterface) {
66 2
            foreach ($node->getMedias() as $media) {
67
                $output->append($this->ruleSet->get('media')->createElement($document, $node));
68 2
            }
69 2
        }
70 2
        $output->append($this->ruleSet->getDefault()->createElement($document, $node));
71
72 2
        return $output;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $output; (ArrayIterator) is incompatible with the return type declared by the abstract method FeedIo\RuleAbstract::createElement of type DOMElement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
73
    }
74
}
75