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

TextCaseTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 23.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 60
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initTextCaseAttributes() 14 14 3
C applyTextCase() 0 30 8
A keepNoCase() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Seboettg\CiteProc\Styles;
4
use Seboettg\CiteProc\Util\StringHelper;
5
6
7
/**
8
 * Trait TextCase
9
 * @package Seboettg\CiteProc\Styles
10
 *
11
 * @author Sebastian Böttger <[email protected]>
12
 */
13
trait TextCaseTrait
14
{
15
16
    private $textCase;
17
18 View Code Duplication
    protected function initTextCaseAttributes(\SimpleXMLElement $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        foreach ($node->attributes() as $attribute) {
21
            /** @var string $name */
22
            $name = $attribute->getName();
23
            $value = (string) $attribute;
24
25
            switch ($name) {
26
                case 'text-case':
27
                    $this->textCase = $value;
28
                    return;
29
            }
30
        }
31
    }
32
33
    public function applyTextCase($text, $lang = "en")
34
    {
35
36
        switch ($this->textCase) {
37
            case 'uppercase':
38
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
39
                break;
40
            case 'lowercase':
41
                $text = $this->keepNoCase(mb_strtolower($text), $text);
42
                break;
43
            case 'sentence':
44
                $text = $this->keepNoCase(mb_substr($text, 0, 1) . mb_strtolower(mb_substr($text, 1)), $text);
45
                break;
46
            case 'capitalize-all':
47
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
48
                break;
49
            case 'title':
50
                if ($lang === "en") {
51
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
52
                }
53
                break;
54
            case 'capitalize-first':
55
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
56
                break;
57
            default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
58
59
        }
60
61
        return $text;
62
    }
63
64
65
    private function keepNoCase($render, $original)
66
    {
67
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
68
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
69
        }
70
        return $render;
71
    }
72
}