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

Str   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 34.48 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 20
loc 58
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startsWith() 10 10 3
A endsWith() 10 10 3
A startsEndsWith() 0 10 4

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
/**
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