Passed
Push — new-api ( 34a0a9...30b18d )
by Sebastian
04:06
created

StylesRenderer::factory()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 52
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 44
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 52
ccs 37
cts 37
cp 1
crap 8
rs 7.9715

How to fix   Long Method   

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
5
 *
6
 * @link        https://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2020 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Styles;
12
13
use Seboettg\CiteProc\CiteProc;
14
use SimpleXMLElement;
15
16
class StylesRenderer
17
{
18
19 139
    public static function factory(SimpleXMLElement $node)#
20
    {
21
        $formattingAttributes = [
22 139
            'font-style',
23
            'font-family',
24
            'font-weight',
25
            'font-variant',
26
            'text-decoration',
27
            'vertical-align'
28
        ];
29
30 139
        $prefix = $suffix = $textCase = $display = null;
31 139
        $quotes = false;
32 139
        $formatting = new FormattingRenderer();
33
34 139
        foreach ($node->attributes() as $attribute) {
35 138
            $name = $attribute->getName();
36
            switch ($name) {
37 138
                case 'prefix':
38 70
                    $prefix = (string)$attribute;
39 70
                    break;
40 138
                case 'suffix':
41 66
                    $suffix = (string)$attribute;
42 66
                    break;
43 137
                case 'text-case':
44 51
                    $textCase = new TextCase((string)$attribute);
45 51
                    break;
46 137
                case 'display':
47 2
                    $display = new Display((string)$attribute);
48 2
                    break;
49 137
                case 'quotes':
50 36
                    $quotes = "true" === (string)$attribute;
51 36
                    break;
52
                default:
53 137
                    if (in_array($attribute->getName(), $formattingAttributes)) {
54 48
                        $value = (string) $attribute;
55 138
                        $formatting->addFormattingOption($attribute->getName(), $value);
56
                    }
57
            }
58
        }
59 139
        $context = CiteProc::getContext();
60 139
        $locale = $context->getLocale();
61 139
        $affixes = AffixesRenderer::factory($context, $prefix, $suffix);
62 139
        $textCase = new TextCaseRenderer($textCase);
63 139
        $display = new DisplayRenderer($display);
64 139
        $quotes = new QuotesRenderer($quotes, $locale, $suffix);
65 139
        return new self(
66 139
            $affixes,
67 139
            $textCase,
68 139
            $display,
69 139
            $formatting,
70 139
            $quotes
71
        );
72
    }
73
74
    /** @var AffixesRenderer */
75
    private $affixes;
76
77
    /** @var TextCaseRenderer */
78
    private $textCase;
79
80
    /** @var DisplayRenderer */
81
    private $display;
82
83
    /** @var FormattingRenderer */
84
    private $formatting;
85
86
    /** @var QuotesRenderer */
87
    private $quotes;
88
89 139
    public function __construct(
90
        ?AffixesRenderer $affixes,
91
        ?TextCaseRenderer $textCase,
92
        ?DisplayRenderer $display,
93
        ?FormattingRenderer $formatting,
94
        ?QuotesRenderer $quotes
95
    ) {
96 139
        $this->affixes = $affixes;
97 139
        $this->textCase = $textCase;
98 139
        $this->display = $display;
99 139
        $this->formatting = $formatting;
100 139
        $this->quotes = $quotes;
101 139
    }
102
103 121
    public function renderAffixes(string $text): string
104
    {
105 121
        return $this->affixes->render($text);
106
    }
107
108 111
    public function renderTextCase(string $text): string
109
    {
110 111
        return $this->textCase->render($text);
111
    }
112
113 118
    public function renderDisplay(string $text): string
114
    {
115 118
        return $this->display->render($text);
116
    }
117
118 121
    public function renderFormatting(string $text): string
119
    {
120 121
        return $this->formatting->render($text);
121
    }
122
123 101
    public function renderQuotes(string $text): string
124
    {
125 101
        return $this->quotes->render($text);
126
    }
127
128 123
    public function getTextCase()
129
    {
130 123
        return $this->textCase;
131
    }
132
}
133