Completed
Push — master ( dbac5b...8dd423 )
by Alejandro
08:15
created

Utils::removeDouble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Alejandro Peña Florentín ([email protected]).
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace Tight;
28
29
/**
30
 * Description of Utils
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class Utils
35
{
36
37
    /**
38
     * Removes the first ocurrence of a substring in a string
39
     * @param string $string String
40
     * @param string $substrToRemove Substring to remove
41
     * @return string String without $substrToRemove or $string if $substrToRemove hasn't been found
42
     */
43
    public static function removeSubstring($string, $substrToRemove) {
44
        // Check index position on $string
45
        $firstIndex = strpos($string, $substrToRemove);
46
        // If $firstIndex is false $substrToRemove wasn't found in $string
47
        if ($firstIndex === FALSE)
48
            return $string;
49
        // Check last index
50
        $lastIndex = $firstIndex + strlen($substrToRemove);
51
        // Get the substring before $substrToRemove
52
        $pre = substr($string, 0, $firstIndex);
53
        // Get the substring after $substrToRemove
54
        $post = substr($string, $lastIndex);
55
        // Return $pre and $post
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
        return $pre . $post;
57
    }
58
59
    /**
60
     * Check if a substring is in a string
61
     * @param string $string String
62
     * @param string $strToSearch Substring to search
63
     * @return boolean TRUE on success,FALSE otherwise
64
     */
65
    public static function inString($string, $strToSearch) {
66
        return (strpos($string, $strToSearch) !== FALSE);
67
    }
68
69
    /**
70
     * Adds a trailing slash to a given path.
71
     * 
72
     * This method does not add the trailing slash if is already set.
73
     * 
74
     * @param string $path Path
75
     * @return string 
76
     */
77
    public static function addTrailingSlash($path) {
78
        if (null !== $path && is_string($path) && !empty($path)) {
79
            if (substr($path, -1) !== "/") {
80
                $path .="/";
81
            }
82
        }
83
        return $path;
84
    }
85
86
    /**
87
     * Replace all the backslash for slashes
88
     * 
89
     * @param string $path Path
90
     * @return string Converted path
91
     */
92
    public static function filterPath($path) {
93
        return self::addTrailingSlash(str_replace(DIRECTORY_SEPARATOR, "/", str_replace("\\", "/", $path)));
94
    }
95
96
    /**
97
     * Replace repeated substring.
98
     * 
99
     * @param string $string String
100
     * @param string $substring Substring
101
     * @return string
102
     */
103
    public static function removeDouble($string, $substring) {
104
        return str_replace($substring . $substring, $substring, $string);
105
    }
106
107
}
108