Completed
Push — master ( 1cc8c6...5e3855 )
by James Ekow Abaka
02:04
created

Text   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 0
cbo 0
dl 8
loc 84
rs 10
ccs 18
cts 28
cp 0.6429

5 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 19 2
A ucamelize() 0 4 1
A deCamelize() 0 11 1
A pluralize() 5 8 2
A singularize() 3 9 3

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
 * Ntentan Framework
4
 * Copyright (c) 2008-2015 James Ekow Abaka Ainooson
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 * 
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 * 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
24
 * 
25
 */
26
27
namespace ntentan\utils;
28
29
/**
30
 * A couple of utility functions for manipulating strings.
31
 */
32
class Text
33
{
34
    /**
35
     * Converts text separated by a specified separator to camel case. This 
36
     * function converts the entire text into lower case before performing the
37
     * camel case conversion. Due to this the first character would be
38
     * lower cased.
39
     * 
40
     * @param string $string The text to be converted.
41
     * @param string $separator The separator to consider for camel casing
42
     * @return string
43
     */
44 1
    public static function camelize($string, $separator = '_')
45
    {
46 1
        if(is_array($separator))
47
        {
48 1
            $separator = "(\\" . implode("|\\", $separator) . ")";
49
        }
50
        else
51
        {
52 1
            $separator = '\\' . $separator;
53
        }
54 1
        return preg_replace_callback(
55 1
            "/{$separator}[a-zA-Z]/", 
56
            function ($matches) 
57
            {
58 1
                return strtoupper($matches[0][1]);
59 1
            }, 
60 1
            strtolower($string)
61
        );
62
    }
63
    
64
    /**
65
     * Converts text separated by a specified separator to camel case. This 
66
     * method works just as the Text::camelize method except that it converts
67
     * the first character to uppercase.
68
     * 
69
     * @param string $string The text to be converted.
70
     * @param string $separator The separator to consider for camel casing
71
     * @return string
72
     */
73 1
    public static function ucamelize($string, $separator = '_')
74
    {
75 1
        return ucfirst(self::camelize($string, $separator));
76
    }
77
    
78
    /**
79
     * Converts camel case text into text separated with an arbitrary separator.
80
     * 
81
     * @param string $string The text to be converted.
82
     * @param string $separator The separator to be used.
83
     * @return string
84
     */    
85 1
    public static function deCamelize($string, $separator = '_')
86
    {
87 1
        return preg_replace_callback(
88 1
            "/[A-Z][a-z]/", 
89 1
            function ($matches) use($separator) 
90
            {
91 1
                return $separator . strtolower($matches[0]);
92 1
            }, 
93 1
            lcfirst($string)
94
        );        
95
    }
96
    
97
    public static function pluralize($text)
98
    {
99 View Code Duplication
        if(substr($text, -1) == 'y') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100
            return substr($text, 0, -1) . 'ies';
101
        } else {
102
            return $text . 's';
103
        }
104
    }
105
    
106
    public static function singularize($text)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
107
    {
108
        if(substr($text, -1) == 's') {
109
            return substr($text, 0, -1);
110 View Code Duplication
        } elseif (substr($text, -3) == 'ies') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
111
            return substr($text, 0, -3) . 'y';
112
        }
113
        return $text;
114
    }
115
}
116