FormattingTrait::format()   C
last analyzed

Complexity

Conditions 13
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

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
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        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Styles;
12
13
use Seboettg\Collection\Map\MapInterface;
14
use SimpleXMLElement;
15
use function Seboettg\Collection\Map\emptyMap;
16
17
/**
18
 * Trait FormattingTrait
19
 * @package Seboettg\CiteProc\Styles
20
 * @author Sebastian Böttger <[email protected]>
21
 */
22
trait FormattingTrait
23
{
24
25
    /**
26
     * @var array
27
     */
28
    private static array $formattingAttributes = [
29
        'font-style',
30
        'font-family',
31
        'font-weight',
32
        'font-variant',
33
        'text-decoration',
34
        'vertical-align'
35
    ];
36
37
    private MapInterface $formattingOptions;
38
    private bool $stripPeriods = false;
39
    private string $format;
40
41
    /**
42
     * @param SimpleXMLElement $node
43
     */
44
    protected function initFormattingAttributes(SimpleXMLElement $node)
45
    {
46
        $this->formattingOptions = emptyMap();
47
48
        /** @var SimpleXMLElement $attribute */
49
        foreach ($node->attributes() as $attribute) {
50
            $name = $attribute->getName();
51
            $value = (string) $attribute;
52
            if (in_array($name, self::$formattingAttributes)) {
53
                $this->formattingOptions->put($name, $value);
54
            }
55
        }
56
    }
57
58
59
    protected function format($text)
60
    {
61
        if (empty($text)) {
62
            return $text;
63
        }
64
65
        if ($this->formattingOptions->count() > 0) {
66
            $format = "";
67
            foreach ($this->formattingOptions as $option => $optionValue) {
68
                if ($optionValue === "italic") {
69
                    $text = "<i>$text</i>";
70
                } elseif ($optionValue === "bold") {
71
                    $text = "<b>$text</b>";
72
                } elseif ($optionValue === "normal") {
73
                    $text = "$text";
74
                } elseif ($option === "vertical-align") {
75
                    if ($optionValue === "sub") {
76
                        $text = "<sub>$text</sub>";
77
                    } elseif ($optionValue === "sup") {
78
                        $text = "<sup>$text</sup>";
79
                    }
80
                } elseif ($option === "text-decoration" && $optionValue === "none") {
81
                    $format .= "";
82
                } else {
83
                    $format .= "$option: $optionValue;";
84
                }
85
            }
86
            if (!empty($format)) {
87
                $text = sprintf("<span style=\"%s\">%s</span>", $format, $text);
88
            }
89
        }
90
        return $text;
91
    }
92
}
93