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

FormattingTrait::format()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13.1112

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 23
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 32
ccs 21
cts 23
cp 0.913
crap 13.1112
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
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Styles;
11
12
use Seboettg\Collection\ArrayList;
13
use SimpleXMLElement;
14
15
/**
16
 * Trait FormattingTrait
17
 * @package Seboettg\CiteProc\Styles
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
trait FormattingTrait
21
{
22
23
    /**
24
     * @var array
25
     */
26
    private static $formattingAttributes = [
27
        'font-style',
28
        'font-family',
29
        'font-weight',
30
        'font-variant',
31
        'text-decoration',
32
        'vertical-align'
33
    ];
34
35
    /**
36
     * @var ArrayList
37
     */
38
    private $formattingOptions;
39
40
    /**
41
     * @var bool
42
     */
43
    private $stripPeriods = false;
44
45
    /**
46
     * @var string
47
     */
48
    private $format;
49
50
    /**
51
     * @param SimpleXMLElement $node
52
     */
53 171
    protected function initFormattingAttributes(SimpleXMLElement $node)
54
    {
55 171
        $this->formattingOptions = new ArrayList();
56
57
        /** @var SimpleXMLElement $attribute */
58 171
        foreach ($node->attributes() as $attribute) {
59
60
            /** @var string $name */
61 170
            $name = (string) $attribute->getName();
62 170
            $value = (string) $attribute;
63
64 170
            if (in_array($name, self::$formattingAttributes)) {
65 44
                $this->formattingOptions->add($name, $value);
66 170
                continue;
67
            }
68
        }
69 171
    }
70
71
72 167
    protected function format($text)
73
    {
74 167
        if (empty($text)) {
75 2
            return $text;
76
        }
77
78 167
        if (!empty($this->formattingOptions)) {
79 167
            $format = "";
80 167
            foreach ($this->formattingOptions as $option => $optionValue) {
81 34
                if ($optionValue === "italic") {
82 32
                    $text = "<i>$text</i>";
83 7
                } elseif ($optionValue === "bold") {
84 5
                    $text = "<b>$text</b>";
85 3
                } elseif ($optionValue === "normal") {
86 1
                    $text = "$text";
87 3
                } elseif ($option === "vertical-align") {
88 1
                    if ($optionValue === "sub") {
89
                        $text = "<sub>$text</sub>";
90 1
                    } elseif ($optionValue === "sup") {
91 1
                        $text = "<sup>$text</sup>";
92
                    }
93 2
                } elseif ($option === "text-decoration" && $optionValue === "none") {
94
                    $format .= "";
95
                } else {
96 34
                    $format .= "$option: $optionValue;";
97
                }
98
            }
99 167
            if (!empty($format)) {
0 ignored issues
show
introduced by
The condition empty($format) is always true.
Loading history...
100 2
                $text = '<span style="' . $format . '">' . $text . '</span>';
101
            }
102
        }
103 167
        return $text;
104
    }
105
}
106