Passed
Push — new-api ( 18d26d...074931 )
by Sebastian
04:59
created

Macro::factory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Style;
11
12
use Seboettg\CiteProc\Data\DataList;
13
use Seboettg\CiteProc\Exception\CiteProcException;
14
use Seboettg\CiteProc\Rendering\HasParent;
15
use Seboettg\CiteProc\Rendering\Rendering;
16
use Seboettg\CiteProc\Root\Root;
17
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
18
use Seboettg\CiteProc\Util\Factory;
19
use Seboettg\Collection\ArrayList as ArrayList;
20
use Seboettg\Collection\ArrayList\ArrayListInterface;
21
use SimpleXMLElement;
22
23
/**
24
 * Class Macro
25
 *
26
 * Macros, defined with cs:macro elements, contain formatting instructions. Macros can be called with cs:text from
27
 * within other macros and the cs:layout element of cs:citation and cs:bibliography, and with cs:key from within cs:sort
28
 * of cs:citation and cs:bibliography. It is recommended to place macros after any cs:locale elements and before the
29
 * cs:citation element.
30
 *
31
 * Macros are referenced by the value of the required name attribute on cs:macro. The cs:macro element must contain one
32
 * or more rendering elements.
33
 *
34
 * @package Seboettg\CiteProc\Rendering
35
 *
36
 * @author Sebastian Böttger <[email protected]>
37
 */
38
class Macro implements Rendering, HasParent
39
{
40
    use ConsecutivePunctuationCharacterTrait;
41
42
    /**
43
     * @var ArrayList
44
     */
45
    private $children;
46
47
    /**
48
     * @var string
49
     */
50
    private $name;
51
52
    /**
53
     * @var Root
54
     */
55
    private $parent;
56
57 71
    public static function factory(SimpleXMLElement $node, $parent): Macro
58
    {
59 71
        $name = (string) $node->attributes()['name'];
60 71
        $children = new ArrayList();
61 71
        foreach ($node->children() as $child) {
62 71
            $children->append(Factory::create($child, $parent));
63
        }
64 71
        return new Macro($children, $parent, $name);
65
    }
66
67
    /**
68
     * Macro constructor.
69
     * @param ArrayListInterface $children
70
     * @param mixed $parent
71
     * @param $name
72
     */
73 71
    public function __construct(ArrayListInterface $children, $parent, $name)
74
    {
75 71
        $this->children = $children;
0 ignored issues
show
Documentation Bug introduced by
$children is of type Seboettg\Collection\ArrayList\ArrayListInterface, but the property $children was declared to be of type Seboettg\Collection\ArrayList. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
76 71
        $this->parent = $parent;
77 71
        $this->name = $name;
78 71
    }
79
80
    /**
81
     * @param array|DataList $data
82
     * @param int|null $citationNumber
83
     * @return string
84
     */
85 65
    public function render($data, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
86
    {
87 65
        $ret = [];
88
        /** @var Rendering $child */
89 65
        foreach ($this->children as $child) {
90 65
            $res = $child->render($data, $citationNumber);
91 65
            $this->getChildrenAffixesAndDelimiter($child);
92 65
            if (!empty($res)) {
93 65
                $ret[] = $res;
94
            }
95
        }
96 65
        $res = implode("", $ret);
97 65
        if (!empty($res)) {
98 65
            $res = $this->removeConsecutiveChars($res);
99
        }
100 65
        return $res;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 71
    public function getName()
107
    {
108 71
        return $this->name;
109
    }
110
111
    /**
112
     * @return Root
113
     */
114
    public function getParent()
115
    {
116
        return $this->parent;
117
    }
118
119
    public function setParent($parent)
120
    {
121
        $this->parent = $parent;
122
    }
123
}
124