Completed
Push — version2.0 ( af5a57...2f739c )
by Sebastian
06:52
created

NamePart   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 20.27 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 15
loc 74
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 3
B render() 0 34 6
A getName() 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\Name;
4
use Seboettg\CiteProc\Styles\AffixesTrait;
5
use Seboettg\CiteProc\Styles\FormattingTrait;
6
use Seboettg\CiteProc\Styles\TextCaseTrait;
7
8
/**
9
 * Class NamePart
10
 *
11
 * The cs:name element may contain one or two cs:name-part child elements for name-part-specific formatting.
12
 * cs:name-part must carry the name attribute, set to either “given” or “family”.
13
 *
14
 * If set to “given”, formatting and text-case attributes on cs:name-part affect the “given” and “dropping-particle”
15
 * name-parts. affixes surround the “given” name-part, enclosing any demoted name particles for inverted names.
16
 *
17
 * If set to “family”, formatting and text-case attributes affect the “family” and “non-dropping-particle” name-parts.
18
 * affixes surround the “family” name-part, enclosing any preceding name particles, as well as the “suffix” name-part
19
 * for non-inverted names.
20
 *
21
 * The “suffix” name-part is not subject to name-part formatting. The use of cs:name-part elements does not influence
22
 * which, or in what order, name-parts are rendered.
23
 *
24
 *
25
 * @package Seboettg\CiteProc\Rendering\Name
26
 *
27
 * @author Sebastian Böttger <[email protected]>
28
 */
29
class NamePart
30
{
31
32
    use FormattingTrait,
33
        TextCaseTrait,
34
        AffixesTrait;
35
36
    private $name;
37
38
    /**
39
     * @var Name
40
     */
41
    private $parent;
42
43
    /**
44
     * NamePart constructor.
45
     * @param \SimpleXMLElement $node
46
     */
47 View Code Duplication
    public function __construct(\SimpleXMLElement $node, $parent)
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...
48
    {
49
        $this->parent = $parent;
50
51
        /** @var \SimpleXMLElement $attribute */
52
        foreach ($node->attributes() as $attribute) {
53
            if ($attribute->getName() === 'name') {
54
                $this->name = (string) $attribute;
55
            }
56
        }
57
58
        $this->initFormattingAttributes($node);
59
        $this->initTextCaseAttributes($node);
60
        $this->initAffixesAttributes($node);
61
    }
62
63
    public function render($data)
64
    {
65
        $ret = "";
66
        if (!$data->{$this->name}) {
67
            return "";
68
        }
69
70
        switch ($this->name) {
71
72
            /* If set to “given”, formatting and text-case attributes on cs:name-part affect the “given” and
73
            “dropping-particle” name-parts. affixes surround the “given” name-part, enclosing any demoted name particles
74
            for inverted names.*/
75
            case 'given':
76
                $given = $this->format($this->applyTextCase($data->given));
77
                if (isset($data->{'dropping-particle'})) {
78
                    $given = " " . $this->format($this->applyTextCase($data->{'dropping-particle'}));
79
                }
80
                $ret = $given;
81
                break;
82
83
            /* if name set to “family”, formatting and text-case attributes affect the “family” and
84
            “non-dropping-particle” name-parts. affixes surround the “family” name-part, enclosing any preceding name
85
            particles, as well as the “suffix” name-part for non-inverted names.*/
86
            case 'family':
87
                $family = $this->format($this->applyTextCase($data->family));
88
                if (isset($data->{'non-dropping-particle'})) {
89
                    $family = $this->format($this->applyTextCase($data->{'non-dropping-particle'})) . " " . $family;
90
                }
91
                $ret = $family;
92
                break;
93
        }
94
95
        return $this->addAffixes($ret);
96
    }
97
98
    public function getName()
99
    {
100
        return $this->name;
101
    }
102
}