Str::getBefore()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 17
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 11/12/2015
9
 * Time: 23:39
10
 * Part of twLib
11
 * http://www.thomaswhiston.com
12
 */
13
namespace twhiston\twLib\Str;
14
15
/**
16
 * Class Str
17
 * String helper functions
18
 * @package twhiston\twLib
19
 */
20
class Str
21
{
22
23
    /**
24
     * Does the string start with?
25
     * @param $haystack
26
     * @param $needles
27
     * @return bool|string if an array is passed in the matching string will be returned, else true/false
28
     */
29 5
    static public function startsWith($haystack, $needles)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
30
    {
31 5
        if ($haystack === null) {
32
            return false;
33
        }
34 5
        if (is_array($needles)) {
35 3
            foreach ((array)$needles as $needle) {
36 3
                if ($needle != '' && strpos($haystack, $needle) === 0) {
37 1
                    return $needle;
38
                }
39 3
            }
40 3
        } else {
41 3
            if ($needles != '' && strpos($haystack, $needles) === 0) {
42 2
                return true;
43
            }
44
        }
45
46 5
        return false;
47
    }
48
49
    /**
50
     * Does the string end with?
51
     * @param $haystack
52
     * @param $needles
53
     * @return array|bool if an array is passed in the matching string will be returned, else true/false
54
     */
55 3
    static public function endsWith($haystack, $needles)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
56
    {
57 3
        if ($haystack === null) {
58
            return false;
59
        }
60 3
        if (is_array($needles)) {
61 1
            foreach ((array)$needles as $needle) {
62 1 View Code Duplication
                if (($temp = strlen($haystack) - strlen(
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...
63
                            $needle
64 1
                        )) >= 0 && strpos(
65 1
                        $haystack,
66 1
                        $needle,
67
                        $temp
68 1
                    ) !== false
69 1
                ) {
70 1
                    return $needle;
71
                }
72 1
            }
73 1 View Code Duplication
        } else {
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...
74 3
            if (($temp = strlen($haystack) - strlen($needles)) >= 0 && strpos(
75 3
                    $haystack,
76 3
                    $needles,
77
                    $temp
78 3
                ) !== false
79 3
            ) {
80 2
                return true;
81
            }
82
        }
83
84 3
        return false;
85
    }
86
87
    /**
88
     * Does the haystack contain needles? If array in will return array out or false
89
     * @param $haystack
90
     * @param $needles
91
     * @return array|bool if an array is passed in the matching string will be returned, else true/false
92
     */
93 1
    static public function contains($haystack, $needles)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
94
    {
95 1
        if ($haystack === null) {
96
            return false;
97
        }
98 1
        if (is_array($needles)) {
99 1
            $results = array();
100 1
            foreach ((array)$needles as $needle) {
101 1
                if (strpos($haystack, $needle) !== false) {
102 1
                    $results[] = $needle;
103 1
                }
104 1
            }
105 1
            if (empty($results)) {
106 1
                return false;
107
            }
108
109 1
            return $results;
110
        } else {
111 1
            if (strpos($haystack, $needles) !== false) {
112 1
                return true;
113
            }
114
        }
115
116 1
        return false;
117
118
    }
119
120
    /**
121
     * Get part of a string before a phrase or array of phrases. Returns an array of results keyed by phrase
122
     * Making recursive true makes this work like tokenizing the string
123
     * You can preserve the phrase by marking the variable true
124
     * output array is in the form of
125
     * $data[$phrase][$index] where index is a numeric value
126
     * @param $string
127
     * @param $phrase string|[string]
128
     * @return array
129
     */
130 1 View Code Duplication
    static public function getBefore($string, $phrase)
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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
131
    {
132
133 1
        if (!is_array($phrase)) {
134 1
            $phrase = [$phrase];
135 1
        }
136
137 1
        $output = [];
138 1
        foreach ($phrase as $item) {
139 1
            $e = explode($item, $string);
140 1
            array_pop($e);//remove the rest of string entry
141 1
            $output[$item] = $e;
142 1
        }
143
144 1
        return $output;
145
146
    }
147
148
    /**
149
     * Get part of a string after a phrase or array of phrases. Returns an array of results keyed by phrase
150
     * If no instances of the key were found returns false
151
     * The last member of the array will be blank if the token is the last thing in the string
152
     * @param $string
153
     * @param $phrase string|[string]
154
     * @return array
155
     */
156 1 View Code Duplication
    static public function getAfter($string, $phrase)
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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
157
    {
158
159 1
        if (!is_array($phrase)) {
160 1
            $phrase = [$phrase];
161 1
        }
162
163 1
        $output = [];
164 1
        foreach ($phrase as $item) {
165 1
            $e = explode($item, $string);
166 1
            array_shift($e);//remove the first item always
167 1
            $output[$item] = $e;
168 1
        }
169
170 1
        return $output;
171
    }
172
173
}