Completed
Push — master ( 888d29...e13826 )
by Sebastian
06:20
created

Text::render()   C

Complexity

Conditions 11
Paths 40

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 22
nc 40
nop 1
dl 0
loc 31
rs 5.2653
c 1
b 0
f 0

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
namespace Seboettg\CiteProc\Rendering;
4
use Seboettg\CiteProc\CiteProc;
5
use Seboettg\CiteProc\Styles\AffixesTrait;
6
use Seboettg\CiteProc\Styles\DisplayTrait;
7
use Seboettg\CiteProc\Styles\FormattingTrait;
8
use Seboettg\CiteProc\Styles\QuotesTrait;
9
use Seboettg\CiteProc\Styles\TextCaseTrait;
10
11
12
/**
13
 * Class Term
14
 * @package Seboettg\CiteProc\Node\Style
15
 *
16
 * @author Sebastian Böttger <[email protected]>
17
 */
18
class Text implements RenderingInterface
19
{
20
    use FormattingTrait,
21
        AffixesTrait,
22
        TextCaseTrait,
23
        DisplayTrait;
24
25
    /**
26
     * @var string
27
     */
28
    private $toRenderType;
29
30
    /**
31
     * @var string
32
     */
33
    private $toRenderTypeValue;
34
35
    private $form = "long";
36
37
    public function __construct(\SimpleXMLElement $node)
38
    {
39
        foreach ($node->attributes() as $attribute) {
40
            $name = $attribute->getName();
41
            if (in_array($name, ['value', 'variable', 'macro', 'term'])) {
42
                $this->toRenderType = $name;
43
                $this->toRenderTypeValue = (string) $attribute;
44
            }
45
            if ($name === "form") {
46
                $this->form = (string) $attribute;
47
            }
48
        }
49
        $this->initFormattingAttributes($node);
50
        $this->initDisplayAttributes($node);
51
        $this->initTextCaseAttributes($node);
52
        $this->initAffixesAttributes($node);
53
    }
54
55
    public function render($data)
56
    {
57
        $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
58
59
        $renderedText = "";
60
        switch ($this->toRenderType) {
61
            case 'value':
62
                $renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang);
63
                break;
64
            case 'variable':
65
                // check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract
66
                // test case group_ShortOutputOnly.json
67
                if (in_array($this->form, ["short", "long"])) {
68
                    $attrWithPrefix = $this->form . ucfirst($this->toRenderTypeValue);
69
                    if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) {
70
                        $renderedText = $this->applyTextCase($data->{$attrWithPrefix}, $lang);
71
                    }
72
                }
73
                if (isset($data->{$this->toRenderTypeValue})) {
74
                    $renderedText = $this->applyTextCase($data->{$this->toRenderTypeValue}, $lang);
75
                }
76
                break;
77
            case 'macro':
78
                $renderedText = CiteProc::getContext()->getMacro($this->toRenderTypeValue)->render($data);
79
                break;
80
            case 'term':
81
                $renderedText = $this->applyTextCase(CiteProc::getContext()->getLocale()->filter("terms", $this->toRenderTypeValue, $this->form)->single, $lang);
82
        }
83
        $text = $this->format($renderedText);
84
        return $this->addAffixes($text, $this->quotes);
0 ignored issues
show
Unused Code introduced by
The call to Text::addAffixes() has too many arguments starting with $this->quotes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
    }
86
}