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

TextCaseRenderer::render()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.097

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 21
cts 27
cp 0.7778
crap 11.097
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 StyleRendererInterface
16
{
17
    /** @var TextCase */
18
    protected $textCase;
19
20
    /** @var string */
21
    protected $language = 'en';
22
23 78
    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 78
        if (null === $textCase) {
26 73
            $textCase = TextCase::NONE();
27
        }
28 78
        $this->textCase = $textCase;
29 78
        $this->language = $language;
30 78
    }
31
32 63
    public function setLanguage(string $language): void
33
    {
34 63
        $this->language = $language;
35 63
    }
36
37 36
    public function render(string $text): string
38
    {
39 36
        if (null === $this->textCase) {
40
            return $text;
41
        }
42
43 36
        switch ((string)$this->textCase) {
44 36
            case TextCase::UPPERCASE:
45 2
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
46 2
                break;
47 35
            case TextCase::LOWERCASE:
48 1
                $text = $this->keepNoCase(mb_strtolower($text), $text);
49 1
                break;
50 35
            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 35
            case TextCase::CAPITALIZE_ALL:
59 1
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
60 1
                break;
61 35
            case TextCase::TITLE:
62
                if ($this->language === "en") {
63
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
64
                }
65
                break;
66 35
            case TextCase::CAPITALIZE_FIRST:
67 3
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
68 3
                break;
69
        }
70
71 36
        return $text;
72
    }
73
74
75
    /**
76
     * @param  string $render
77
     * @param  string $original
78
     * @return string|string[]|null
79
     */
80 4
    private function keepNoCase(string $render, string $original)
81
    {
82 4
        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 4
        return $render;
86
    }
87
88 2
    public function getTextCase(): ?TextCase
89
    {
90 2
        return $this->textCase;
91
    }
92
}
93