Passed
Push — new-api ( 42b595...e6ef7d )
by Sebastian
05:57
created

TextCaseRenderer::render()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 25.3732

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 37
ccs 13
cts 28
cp 0.4643
crap 25.3732
rs 7.6666

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\CiteProc\Util\StringHelper;
14
15
class TextCaseRenderer implements StyleRendererInterface
16
{
17
    /** @var TextCase */
18
    protected $textCase;
19
20
    /** @var string */
21
    protected $language = 'en';
22
23 60
    public function __construct(?TextCase $textCase = null, string $language = 'en')
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$textCase" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$textCase"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$language" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$language"; expected 0 but found 1
Loading history...
24
    {
25 60
        $this->textCase = $textCase;
26 60
        $this->language = $language;
27 60
    }
28
29 45
    public function setLanguage(string $language): void
30
    {
31 45
        $this->language = $language;
32 45
    }
33
34 18
    public function render(string $text): string
35
    {
36 18
        if (null === $this->textCase) {
37 17
            return $text;
38
        }
39
40 2
        switch ((string)$this->textCase) {
41 2
            case TextCase::UPPERCASE:
42
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
43
                break;
44 2
            case TextCase::LOWERCASE:
45
                $text = $this->keepNoCase(mb_strtolower($text), $text);
46
                break;
47 2
            case TextCase::SENTENCE:
48
                if (StringHelper::checkUpperCaseString($text)) {
49
                    $text = mb_strtolower($text);
50
                    $text = StringHelper::mb_ucfirst($text);
51
                } else {
52
                    $text = StringHelper::mb_ucfirst($text);
53
                }
54
                break;
55 2
            case TextCase::CAPITALIZE_ALL:
56
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
57
                break;
58 2
            case TextCase::TITLE:
59
                if ($this->language === "en") {
60
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
61
                }
62
                break;
63 2
            case TextCase::CAPITALIZE_FIRST:
64 2
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
65 2
                break;
66
            default:
67
                break;
68
        }
69
70 2
        return $text;
71
    }
72
73
74
    /**
75
     * @param  string $render
76
     * @param  string $original
77
     * @return string|string[]|null
78
     */
79 2
    private function keepNoCase(string $render, string $original)
80
    {
81 2
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
82
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
83
        }
84 2
        return $render;
85
    }
86
}
87