Passed
Push — master ( c41dc5...63b79a )
by Sebastian
03:21
created

TextCaseTrait::keepNoCase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Styles;
11
use Seboettg\CiteProc\Util\StringHelper;
12
13
14
/**
15
 * Trait TextCase
16
 * @package Seboettg\CiteProc\Styles
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
trait TextCaseTrait
21
{
22
23
    private $textCase;
24
25 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...
26
    {
27
        foreach ($node->attributes() as $attribute) {
28
            /** @var string $name */
29
            $name = $attribute->getName();
30
            $value = (string) $attribute;
31
32
            switch ($name) {
33
                case 'text-case':
34
                    $this->textCase = $value;
35
                    return;
36
            }
37
        }
38
    }
39
40
    /**
41
     * @param string $text
42
     * @param string $lang
43
     * @return string
44
     */
45
    public function applyTextCase($text, $lang = "en")
46
    {
47
48
        switch ($this->textCase) {
49
            case 'uppercase':
50
                $text = $this->keepNoCase(mb_strtoupper($text), $text);
51
                break;
52
            case 'lowercase':
53
                $text = $this->keepNoCase(mb_strtolower($text), $text);
54
                break;
55
            case 'sentence':
56
                if (StringHelper::checkUpperCaseString($text)) {
57
                    $text = mb_strtolower($text);
58
                    return StringHelper::mb_ucfirst($text);
59
                } else {
60
                    return StringHelper::mb_ucfirst($text);
61
                }
62
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
63
            case 'capitalize-all':
64
                $text = $this->keepNoCase(StringHelper::capitalizeAll($text), $text);
65
                break;
66
            case 'title':
67
                if ($lang === "en") {
68
                    $text = $this->keepNoCase(StringHelper::capitalizeForTitle($text), $text);
69
                }
70
                break;
71
            case 'capitalize-first':
72
                $text = $this->keepNoCase(StringHelper::mb_ucfirst($text), $text);
73
                break;
74
            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...
75
76
        }
77
78
        return $text;
79
    }
80
81
82
    private function keepNoCase($render, $original)
83
    {
84
        if (preg_match('/<span class=\"nocase\">(\p{L}+)<\/span>/i', $original, $match)) {
85
            return preg_replace('/(<span class=\"nocase\">\p{L}+<\/span>)/i', $match[1], $render);
86
        }
87
        return $render;
88
    }
89
}