Passed
Push — master ( 661b73...ccb1dd )
by Sebastian
08:54 queued 05:17
created

FormattingTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 77
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
C format() 0 32 13
A initFormattingAttributes() 0 14 3
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
13
use Seboettg\Collection\ArrayList;
14
use SimpleXMLElement;
15
16
/**
17
 * Trait FormattingTrait
18
 * @package Seboettg\CiteProc\Styles
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
19
 * @author Sebastian Böttger <[email protected]>
1 ignored issue
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 1
Loading history...
20
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
21
trait FormattingTrait
22
{
23
24
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var array
26
     */
27
    static $formattingAttributes = ['font-style', 'font-family', 'font-weight', 'font-variant', 'text-decoration', 'vertical-align'];
28
29
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var ArrayList
31
     */
32
    private $formattingOptions;
0 ignored issues
show
Coding Style introduced by
Private member variable "formattingOptions" must be prefixed with an underscore
Loading history...
33
34
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var bool
36
     */
37
    private $stripPeriods = false;
0 ignored issues
show
Coding Style introduced by
Private member variable "stripPeriods" must be prefixed with an underscore
Loading history...
38
39
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var string
41
     */
42
    private $format;
0 ignored issues
show
Coding Style introduced by
Private member variable "format" must be prefixed with an underscore
Loading history...
43
44
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @param SimpleXMLElement $node
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47 144
    protected function initFormattingAttributes(SimpleXMLElement $node)
48
    {
49 144
        $this->formattingOptions = new ArrayList();
50
51
        /** @var SimpleXMLElement $attribute */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
52 144
        foreach ($node->attributes() as $attribute) {
53
54
            /** @var string $name */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
55 144
            $name = (string) $attribute->getName();
56 144
            $value = (string) $attribute;
57
58 144
            if (in_array($name, self::$formattingAttributes)) {
59 42
                $this->formattingOptions->add($name, $value);
60 42
                continue;
61
            }
62
        }
63 144
    }
64
65
66 140
    protected function format($text)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function format()
Loading history...
67
    {
68 140
        if (empty($text)) {
69 1
            return $text;
70
        }
71
72 140
        if (!empty($this->formattingOptions)) {
73 140
            $format = "";
74 140
            foreach ($this->formattingOptions as $option => $optionValue) {
75 32
                if ($optionValue === "italic") {
76 26
                    $text = "<i>$text</i>";
77 12
                } else if ($optionValue === "bold") {
78 6
                    $text = "<b>$text</b>";
79 8
                } else if ($optionValue === "normal") {
80
                    //$text = $text;
81 8
                } else if ($option === "vertical-align") {
82 2
                    if ($optionValue === "sub") {
83 1
                        $text = "<sub>$text</sub>";
84 2
                    } else if ($optionValue === "sup") {
85 1
                        $text = "<sup>$text</sup>";
86
                    }
87 6
                } else if ($option === "text-decoration" && $optionValue === "none") {
88
89
                } else {
90 6
                    $format .= "$option: $optionValue;";
91
                }
92
            }
93 140
            if (!empty($format)) {
94 6
                $text = '<span style="' . $format . '">' . $text . '</span>';
95
            }
96
        }
97 140
        return $text;
98
    }
99
}