Passed
Push — master ( af1281...14bd9e )
by Sebastian
02:37
created

Str::startsEndsWith()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 2
crap 4
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Helper;
13
14
/**
15
 * String Helper.
16
 */
17
class Str
18
{
19
    /**
20
     * Determine if a given string starts with a given substring.
21
     *
22
     * @param string $haystack
23
     * @param array  $needles  Array of string needles
24
     *
25
     * @return bool
26
     */
27 32 View Code Duplication
    public static function startsWith(string $haystack, array $needles = []) : bool
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...
28
    {
29 32
        foreach ($needles as $needle) {
30 32
            if (strpos($haystack, (string) $needle) === 0) {
31 3
                return true;
32
            }
33
        }
34
        
35 29
        return false;
36
    }
37
    
38
    /**
39
     * Determine if a given string ends with a given substring.
40
     *
41
     * @param string $haystack
42
     * @param array  $needles  Array of string needles
43
     *
44
     * @return bool
45
     */
46 32 View Code Duplication
    public static function endsWith(string $haystack, array $needles = []) : bool
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...
47
    {
48 32
        foreach ($needles as $needle) {
49 32
            if (strpos(strrev($haystack), (string) $needle) === 0) {
50 3
                return true;
51
            }
52
        }
53
        
54 29
        return false;
55
    }
56
    
57
    /**
58
     * Determine if a given string starts and ends with a given substring.
59
     *
60
     * @param string $haystack
61
     * @param array $needles
62
     * @return bool
63
     */
64 20
    public static function startsEndsWith(string $haystack, array $needles = []) : bool
65
    {
66 20
        foreach ($needles as $needle) {
67 20
            if (strpos($haystack, (string) $needle) === 0 && strpos(strrev($haystack), (string) $needle) === 0) {
68 5
                return true;
69
            }
70
        }
71
        
72 15
        return false;
73
    }
74
}
75