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

FormattingRenderer::render()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.0122

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 26
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 37
ccs 23
cts 24
cp 0.9583
crap 13.0122
rs 6.6166

How to fix   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
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\Collection\ArrayList;
14
use SimpleXMLElement;
15
16
final class FormattingRenderer implements StyleRendererInterface
17
{
18
19 78
    public static function factory(SimpleXMLElement $node)
20
    {
21
        $formattingAttributes = [
22 78
            'font-style',
23
            'font-family',
24
            'font-weight',
25
            'font-variant',
26
            'text-decoration',
27
            'vertical-align'
28
        ];
29 78
        $instance = new self();
30 78
        foreach ($node->attributes() as $attribute) {
31 77
            $name = (string) $attribute->getName();
32 77
            $value = (string) $attribute;
33 77
            if (in_array($name, $formattingAttributes)) {
34 77
                $instance->addFormattingOption($name, $value);
35
            }
36
        }
37 78
        return $instance;
38
    }
39
40
    /**
41
     * @var ArrayList
42
     */
43
    private $formattingOptions;
44
45 78
    public function __construct()
46
    {
47 78
        $this->formattingOptions = new ArrayList();
48 78
    }
49
50 10
    public function addFormattingOption($option, $optionValue)
51
    {
52 10
        $this->formattingOptions->add($option, $optionValue);
53 10
    }
54
55 36
    public function render(string $text): string
56
    {
57 36
        if (empty($text)) {
58
            return $text;
59
        }
60
61 36
        if (!empty($this->formattingOptions)) {
62 36
            $format = [];
63 36
            foreach ($this->formattingOptions as $option => $optionValue) {
64
                switch ($optionValue) {
65 7
                    case "italic":
66 2
                        $text = sprintf("<i>%s</i>", $text);
67 2
                        break;
68 6
                    case "bold":
69 1
                        $text = sprintf("<b>%s</b>", $text);
70 1
                        break;
71 6
                    case "normal":
72 3
                        break;
73
                    default:
74 6
                        if ($option === "vertical-align") {
75 1
                            if ($optionValue === "sub") {
76 1
                                $text = sprintf("<sub>%s</sub>", $text);
77 1
                            } elseif ($optionValue === "sup") {
78 1
                                $text = sprintf("<sup>%s</sup>", $text);
79
                            }
80
                        } else {
81 5
                            if ($option !== "text-decoration" || $optionValue !== "none") {
82 7
                                $format[] = "$option: $optionValue";
83
                            }
84
                        }
85
                }
86
            }
87 36
            if (!empty($format)) {
88 5
                $text = sprintf('<span style="%s">%s</span>', implode(";", $format), $text);
89
            }
90
        }
91 36
        return $text;
92
    }
93
}
94