Passed
Push — new-api ( 4bfe18...7ec1cc )
by Sebastian
05:06
created

NameOrderRenderer::render()   D

Complexity

Conditions 27
Paths 57

Size

Total Lines 57
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 28.0687

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 27
eloc 45
nc 57
nop 2
dl 0
loc 57
ccs 39
cts 44
cp 0.8864
crap 28.0687
rs 4.1666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php: NameOrderRenderer.php
5
 * User: Sebastian Böttger <[email protected]>
6
 * created at 29.12.20, 12:25
7
 */
8
9
namespace Seboettg\CiteProc\Rendering\Name;
10
11
use Seboettg\CiteProc\Exception\CiteProcException;
12
use Seboettg\CiteProc\Style\Options\DemoteNonDroppingParticle;
13
use Seboettg\CiteProc\Style\Options\GlobalOptions;
14
use Seboettg\CiteProc\Style\Options\NameOptions;
15
use Seboettg\CiteProc\Util\NameHelper;
16
use Seboettg\CiteProc\Util\StringHelper;
17
use stdClass;
18
19
class NameOrderRenderer
20
{
21
    /** @var GlobalOptions */
22
    private $globalOptions;
23
24
    /** @var NameOptions */
25
    private $nameOptions;
26
27
    /** @var NamePart[] */
28
    private $nameParts;
29
30
    /** @var string */
31
    private $delimiter;
32
33
    /**
34
     * NameOrderRenderer constructor.
35
     * @param GlobalOptions $globalOptions
36
     * @param NamePart[] $nameParts
37
     * @param string $delimiter
38
     */
39 118
    public function __construct(
40
        GlobalOptions $globalOptions,
41
        array $nameParts,
42
        string $delimiter
43
    ) {
44 118
        $this->globalOptions = $globalOptions;
45 118
        $this->nameParts = $nameParts;
46 118
        $this->delimiter = $delimiter;
47 118
    }
48
49
    /**
50
     * @param stdClass $data
51
     * @param integer  $rank
52
     *
53
     * @return string
54
     * @throws CiteProcException
55
     */
56 105
    public function render(stdClass $data, int $rank): string
57
    {
58 105
        $nameAsSortOrder = (($this->nameOptions->getNameAsSortOrder() === "first" && $rank === 0) ||
59 105
            $this->nameOptions->getNameAsSortOrder() === "all");
60 105
        $demoteNonDroppingParticle = $this->globalOptions->getDemoteNonDroppingParticles();
61 105
        $normalizedName = NameHelper::normalizeName($data);
62 105
        $delimiter = $this->nameOptions->getNameDelimiter() ?? $this->delimiter;
63 105
        if (StringHelper::isLatinString($normalizedName) || StringHelper::isCyrillicString($normalizedName)) {
64 104
            if ($this->nameOptions->getForm() === "long"
65 104
                && $nameAsSortOrder
66 45
                && ((string) $demoteNonDroppingParticle === DemoteNonDroppingParticle::NEVER
67 104
                    || (string) $demoteNonDroppingParticle === DemoteNonDroppingParticle::SORT_ONLY)
68
            ) {
69
                // [La] [Fontaine], [Jean] [de], [III]
70 29
                NameHelper::prependParticleTo($data, "family", "non-dropping-particle");
71 29
                NameHelper::appendParticleTo($data, "given", "dropping-particle");
72
73 29
                list($family, $given) = $this->renderNameParts($data);
74
75 29
                $text = $family . (!empty($given) ? $this->nameOptions->getSortSeparator() . $given : "");
76 29
                $text .= !empty($data->suffix) ? $this->nameOptions->getSortSeparator() . $data->suffix : "";
77 80
            } elseif ($this->nameOptions->getForm() === "long"
78 80
                && $nameAsSortOrder
79 16
                && (is_null($demoteNonDroppingParticle)
80 80
                    || (string) $demoteNonDroppingParticle === DemoteNonDroppingParticle::DISPLAY_AND_SORT)
81
            ) {
82
                // [Fontaine], [Jean] [de] [La], [III]
83 16
                NameHelper::appendParticleTo($data, "given", "dropping-particle");
84 16
                NameHelper::appendParticleTo($data, "given", "non-dropping-particle");
85 16
                list($family, $given) = $this->renderNameParts($data);
86 16
                $text = $family;
87 16
                $text .= !empty($given) ? $this->nameOptions->getSortSeparator() . $given : "";
88 16
                $text .= !empty($data->suffix) ? $this->nameOptions->getSortSeparator() . $data->suffix : "";
89 71
            } elseif ($this->nameOptions->getForm() === "long" && $nameAsSortOrder
90 71
                && empty($demoteNonDroppingParticle)) {
91
                list($family, $given) = $this->renderNameParts($data);
92
                $text = $family;
93
                $text .= !empty($given) ? $delimiter . $given : "";
94
                $text .= !empty($data->suffix) ? $this->nameOptions->getSortSeparator() . $data->suffix : "";
95 71
            } elseif ($this->nameOptions->getForm() === "short") {
96
                // [La] [Fontaine]
97 25
                NameHelper::prependParticleTo($data, "family", "non-dropping-particle");
98 25
                $text = $data->family;
99
            } else {// form "long" (default)
100
                // [Jean] [de] [La] [Fontaine] [III]
101 47
                NameHelper::prependParticleTo($data, "family", "non-dropping-particle");
102 47
                NameHelper::prependParticleTo($data, "family", "dropping-particle");
103 47
                NameHelper::appendParticleTo($data, "family", "suffix");
104 47
                list($family, $given) = $this->renderNameParts($data);
105 104
                $text = !empty($given) ? $given . " " . $family : $family;
106
            }
107 1
        } elseif (StringHelper::isAsianString(NameHelper::normalizeName($data))) {
108 1
            $text = $this->nameOptions->getForm() === "long" ? $data->family . $data->given : $data->family;
109
        } else {
110
            $text = $this->nameOptions->getForm() === "long" ? $data->family . " " . $data->given : $data->family;
111
        }
112 105
        return $text;
113
    }
114
115
116
    /**
117
     * @param  $data
118
     * @return array
119
     */
120 84
    private function renderNameParts($data): array
121
    {
122 84
        $given = "";
123 84
        if (array_key_exists("family", $this->nameParts)) {
124 2
            $family = $this->nameParts["family"]->render($data);
125
        } else {
126 82
            $family = $data->family;
127
        }
128 84
        if (isset($data->given)) {
129 82
            if (array_key_exists("given", $this->nameParts)) {
130 2
                $given = $this->nameParts["given"]->render($data);
131
            } else {
132 80
                $given = $data->given;
133
            }
134
        }
135 84
        return [$family, $given];
136
    }
137
138
    /**
139
     * @param NameOptions $nameOptions
140
     */
141 105
    public function setNameOptions(NameOptions $nameOptions): void
142
    {
143 105
        $this->nameOptions = $nameOptions;
144 105
    }
145
}
146