Completed
Push — master ( 0b1b13...66e6ea )
by Dmytro
03:15
created

StringHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 17.31 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 104
rs 10
wmc 10
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A upperCaseFirst() 9 11 2
A lowerCaseFirst() 9 11 2
A upperCaseWords() 0 7 2
A upperCase() 0 7 2
A lowerCase() 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 Asymptix\helpers;
4
5
/**
6
 * Strings connected methods.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2015 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class StringHelper {
16
17
    /**
18
     * Returns a string with the first character of str capitalized, if that
19
     * character is alphabetic.
20
     *
21
     * @param string The input string.
22
     * @param string $encoding The encoding parameter is the character encoding.
23
     *            If it is omitted, the internal character encoding value will
24
     *            be used.
25
     *
26
     * @return string Returns the resulting string.
27
     */
28 View Code Duplication
    public static function upperCaseFirst($str, $encoding = 'utf8') {
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...
29
        if ($encoding) {
30
            $firstLetter = mb_substr(mb_strtoupper($str, $encoding), 0, 1, $encoding);
31
32
            return ($firstLetter . mb_substr($str, 1, null, $encoding));
33
        } else {
34
            $firstLetter = mb_substr(mb_strtoupper($str), 0, 1);
35
36
            return ($firstLetter . mb_substr($str, 1));
37
        }
38
    }
39
40
    /**
41
     * Returns a string with the first character of str lowercased, if that
42
     * character is alphabetic.
43
     *
44
     * @param string The input string.
45
     * @param string $encoding The encoding parameter is the character encoding.
46
     *            If it is omitted, the internal character encoding value will
47
     *            be used.
48
     *
49
     * @return string Returns the resulting string.
50
     */
51 View Code Duplication
    public static function lowerCaseFirst($str, $encoding = 'utf8') {
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...
52
        if ($encoding) {
53
            $firstLetter = mb_substr(mb_strtoupper($str, $encoding), 0, 1, $encoding);
54
55
            return ($firstLetter . mb_substr($str, 1, null, $encoding));
56
        } else {
57
            $firstLetter = mb_substr(mb_strtoupper($str), 0, 1);
58
59
            return ($firstLetter . mb_substr($str, 1));
60
        }
61
    }
62
63
    /**
64
     * Returns a string with the first character of each word in str capitalized,
65
     * if that character is alphabetic.
66
     *
67
     * @param string The input string.
68
     * @param string $encoding The encoding parameter is the character encoding.
69
     *            If it is omitted, the internal character encoding value will
70
     *            be used.
71
     *
72
     * @return string Returns the resulting string.
73
     */
74
    public static function upperCaseWords($str, $encoding = 'utf8') {
75
        if ($encoding) {
76
            return mb_convert_case($str, MB_CASE_TITLE, $encoding);
77
        } else {
78
            return mb_convert_case($str, MB_CASE_TITLE);
79
        }
80
    }
81
82
    /**
83
     * Returns string with all alphabetic characters converted to uppercase.
84
     *
85
     * @param string The input string.
86
     * @param string $encoding The encoding parameter is the character encoding.
87
     *            If it is omitted, the internal character encoding value will
88
     *            be used.
89
     *
90
     * @return string Returns the resulting string.
91
     */
92
    public static function upperCase($str, $encoding = 'utf8') {
93
        if ($encoding) {
94
            return mb_strtoupper($str, $encoding);
95
        } else {
96
            return mb_strtoupper($str);
97
        }
98
    }
99
100
    /**
101
     * Returns string with all alphabetic characters converted to lowercase.
102
     *
103
     * @param string The input string.
104
     * @param string $encoding The encoding parameter is the character encoding.
105
     *            If it is omitted, the internal character encoding value will
106
     *            be used.
107
     *
108
     * @return string Returns the resulting string.
109
     */
110
    public static function lowerCase($str, $encoding = 'utf8') {
111
        if ($encoding) {
112
            return mb_strtolower($str, $encoding);
113
        } else {
114
            return mb_strtolower($str);
115
        }
116
    }
117
118
}
119