Completed
Pull Request — master (#221)
by personal
04:09
created

Searcher   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 10
loc 65
rs 10
c 1
b 0
f 0
wmc 12
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNext() 5 10 3
A getPrevious() 5 9 3
B getPositionOfClosingBrace() 0 23 6

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
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Parser;
11
12
use Hal\Component\Token\Token;
13
14
class Searcher
15
{
16
17
    /**
18
     * @param $tokens
19
     * @param $current
20
     * @param $required
21
     * @return bool
22
     */
23
    public function getNext($tokens, $current, $required)
24
    {
25
        $len = sizeof($tokens);
26 View Code Duplication
        for ($i = $current; $i < $len; $i++) {
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...
27
            if ($required === $tokens[$i]) {
28
                return $i;
29
            }
30
        }
31
        return false;
32
    }
33
34
    /**
35
     * @param $tokens
36
     * @param $current
37
     * @param $required
38
     * @return bool
39
     */
40
    public function getPrevious($tokens, $current, $required)
41
    {
42 View Code Duplication
        for ($i = $current; $i >= 0; $i--) {
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...
43
            if ($required === $tokens[$i]) {
44
                return $i;
45
            }
46
        }
47
        return false;
48
    }
49
50
    /**
51
     * @param $tokens
52
     * @param $startingToken
53
     * @return null
54
     */
55
    public function getPositionOfClosingBrace($tokens, $startingToken)
56
    {
57
        $openBrace = 0;
58
        $start = null;
59
        $len = sizeof($tokens);
60
        for ($i = $startingToken; $i < $len; $i++) {
61
            $token = $tokens[$i];
62
            if (Token::T_BRACE_CLOSE === $token) {
63
                $openBrace--;
64
                if ($openBrace <= 0) {
65
                    return $i;
66
                }
67
            }
68
            if (Token::T_BRACE_OPEN === $token) {
69
                $openBrace++;
70
                if (is_null($start)) {
71
                    $start = $startingToken = $i + 1;
0 ignored issues
show
Unused Code introduced by
$startingToken is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
                }
73
            }
74
        }
75
76
        return false;
77
    }
78
}