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

TextCaseRenderer::render()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.1371

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 35
ccs 24
cts 27
cp 0.8889
crap 10.1371
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
final class TextCaseRenderer implements StylesRendererInterface
16
{
17
    /** @var TextCase */
18
    protected $textCase;
19
20
    /** @var string */
21
    protected $language = 'en';
22
23 139
    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 139
        if (null === $textCase) {
26 137
            $textCase = TextCase::NONE();
27
        }
28 139
        $this->textCase = $textCase;
29 139
        $this->language = $language;
30 139
    }
31
32 123
    public function setLanguage(string $language): void
33
    {
34 123
        $this->language = $language;
35 123
    }
36
37 111
    public function render(string $text): string
38
    {
39 111
        if (null === $this->textCase) {
40
            return $text;
41
        }
42
43 111
        switch ((string)$this->textCase) {
44 111
            case TextCase::UPPERCASE:
45 2
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
46 2
                break;
47 110
            case TextCase::LOWERCASE:
48 1
                $text = $this->keepNoCase(mb_strtolower($text), $text);
49 1
                break;
50 110
            case TextCase::SENTENCE:
51 1
                if (StringHelper::checkUpperCaseString($text)) {
52
                    $text = mb_strtolower($text);
53
                    $text = StringHelper::mb_ucfirst($text);
54
                } else {
55 1
                    $text = StringHelper::mb_ucfirst($text);
56
                }
57 1
                break;
58 110
            case TextCase::CAPITALIZE_ALL:
59 1
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
60 1
                break;
61 110
            case TextCase::TITLE:
62 13
                if ($this->language === "en") {
63 13
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
64
                }
65 13
                break;
66 109
            case TextCase::CAPITALIZE_FIRST:
67 22
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
68 22
                break;
69
        }
70
71 111
        return $text;
72
    }
73
74
75
    /**
76
     * @param  string $render
77
     * @param  string $original
78
     * @return string|string[]|null
79
     */
80 28
    private function keepNoCase(string $render, string $original)
81
    {
82 28
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
83
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
84
        }
85 28
        return $render;
86
    }
87
88 2
    public function getTextCase(): ?TextCase
89
    {
90 2
        return $this->textCase;
91
    }
92
}
93