Completed
Push — master ( 888d29...e13826 )
by Sebastian
06:20
created

Layout   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 12.09 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 91
rs 10
c 1
b 0
f 0
wmc 15
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
D render() 0 39 9
A renderSingle() 0 10 2
A getNumberOfCitedItems() 0 4 1
A wrapBibEntry() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Seboettg\CiteProc\Rendering;
4
5
use Seboettg\CiteProc\CiteProc;
6
use Seboettg\CiteProc\Rendering\RenderingInterface;
7
use Seboettg\CiteProc\Styles\AffixesTrait;
8
use Seboettg\CiteProc\Styles\FormattingTrait;
9
use Seboettg\CiteProc\Styles\DelimiterTrait;
10
use Seboettg\CiteProc\Util\Factory;
11
use Seboettg\Collection\ArrayList;
12
13
14
/**
15
 * Class Layout
16
 * @package Seboettg\CiteProc\Rendering
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
class Layout implements RenderingInterface
21
{
22
23
    private static $numberOfCitedItems = 0;
24
25
    use AffixesTrait,
26
        FormattingTrait,
27
        DelimiterTrait;
28
29
    /**
30
     * @var ArrayList
31
     */
32
    private $children;
33
34 View Code Duplication
    public function __construct($node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        self::$numberOfCitedItems = 0;
37
        $this->children = new ArrayList();
38
        foreach ($node->children() as $child) {
39
            $this->children->append(Factory::create($child));
40
        }
41
        $this->initDelimiterAttributes($node);
42
        $this->initAffixesAttributes($node);
43
        $this->initFormattingAttributes($node);
44
    }
45
46
    public function render($data)
47
    {
48
        $ret = "";
49
        $sorting = CiteProc::getContext()->getSorting();
50
        if (!empty($sorting)) {
51
            $sorting->sort($data);
52
        }
53
54
        if (CiteProc::getContext()->isModeBibliography()) {
55
            if (is_array($data)) {
56
                $arr = [];
57
                foreach ($data as $item) {
58
                    ++self::$numberOfCitedItems;
59
                    $arr[] = $this->wrapBibEntry($this->renderSingle($item));
60
                }
61
                $ret .= implode($this->delimiter, $arr);
62
            } else {
63
                $ret .= $this->wrapBibEntry($this->renderSingle($data));
64
            }
65
66
            return "<div class=\"csl-bib-body\">".$ret."\n</div>";
67
68
        } else if (CiteProc::getContext()->isModeCitation()) {
69
            if (is_array($data)) {
70
                $arr = [];
71
                foreach ($data as $item) {
72
                    if (CiteProc::getContext()->hasCitationItems()) {
73
                        continue;
74
                    }
75
                    $arr[] = $this->renderSingle($item);
76
                }
77
                $ret .= implode($this->delimiter, $arr);
78
            } else {
79
                $ret .= $this->renderSingle($data);
80
            }
81
        }
82
83
        return $this->addAffixes($ret);
84
    }
85
86
    private function renderSingle($data)
87
    {
88
        $ret = "";
89
        /** @var RenderingInterface $child */
90
        foreach ($this->children as $child) {
91
            $ret .= $child->render($data);
92
        }
93
94
        return $this->format($ret);
95
    }
96
97
    /**
98
     * @return int
99
     */
100
    public static function getNumberOfCitedItems()
101
    {
102
        return self::$numberOfCitedItems;
103
    }
104
105
    private function wrapBibEntry($value)
106
    {
107
        return "\n  <div class=\"csl-entry\">" . $value . "</div>";
108
    }
109
110
}