Completed
Push — master ( 4c7da2...64af60 )
by Alex
02:34 queued 46s
created

Category::createElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Category::addElement() 0 6 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\Node\CategoryInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\RuleAbstract;
16
17
class Category extends RuleAbstract
18
{
19
    const NODE_NAME = 'category';
20
21
    /**
22
     * @param  NodeInterface $node
23
     * @param  \DOMElement   $element
24
     * @return mixed
25
     */
26 2
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
27
    {
28 2
        $category = $node->newCategory();
29 2
        $category->setScheme($this->getAttributeValue($element, 'domain'))
30 2
        ->setLabel($element->nodeValue)
31 2
        ->setTerm($element->nodeValue);
32 2
        $node->addCategory($category);
33 2
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 4
    protected function hasValue(NodeInterface $node) : bool
39
    {
40 4
        return !! $node->getCategories();
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 4
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
47
    {
48 4
        foreach ($node->getCategories() as $category) {
49 4
            $rootElement->appendChild($this->createCategoryElement($document, $category));
50
        }
51 4
    }
52
53
    /**
54
     * @param  \DomDocument   $document
55
     * @param  CategoryInterface $category
56
     * @return \DomElement
57
     */
58 4
    public function createCategoryElement(\DomDocument $document, CategoryInterface $category) : \DOMElement
59
    {
60 4
        $element = $document->createElement(
61 4
            $this->getNodeName(),
62 4
            is_null($category->getTerm()) ? $category->getLabel():$category->getTerm()
63
            );
64 4
        if (!! $category->getScheme()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $category->getScheme() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65 2
            $element->setAttribute('domain', $category->getScheme());
66
        }
67
68 4
        return $element;
69
    }
70
}
71