Completed
Pull Request — master (#162)
by personal
05:46 queued 03:11
created

Searcher::getPositionOfNext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 5
Ratio 55.56 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 9
rs 9.6667
cc 3
eloc 6
nc 3
nop 3
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\OOP\Extractor;
11
use Hal\Component\Token\TokenCollection;
12
13
14
/**
15
 * Tool class
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class Searcher {
20
21
    /**
22
     * Get value under limiters
23
     *
24
     * @param string[] $delimiters
25
     * @param int $n
26
     * @param \Hal\Component\Token\TokenCollection $tokens
27
     * @return null|string
28
     */
29
    public function getUnder(array $delimiters, &$n, TokenCollection $tokens) {
30
        $end = sizeof($tokens, COUNT_NORMAL);
31
        $value = '';
32
        while($n < $end) {
33
            $token = $tokens[$n];
34
            if(in_array($token->getValue(), $delimiters)) {
35
                return $value;
36
            }
37
            $value .= $token->getValue();
38
            $n++;
39
        }
40
        return null;
41
    }
42
43
    /**
44
     * Get non whitespace previous token
45
     *
46
     * @param $n
47
     * @param \Hal\Component\Token\TokenCollection $tokens
48
     * @return null
49
     */
50
    public function getPrevious(&$n, TokenCollection $tokens) {
51
        $p = $n - 1;
52
        for($i = $p ; $i > 0; $i--) {
53
            if(T_WHITESPACE !== $tokens[$i]->getType()) {
54
                return $tokens[$i];
55
            }
56
        }
57
        return null;
58
    }
59
60
    /**
61
     * Get name following token
62
     *
63
     * @param $n
64
     * @param \Hal\Component\Token\TokenCollection $tokens
65
     * @return null|string
66
     */
67
    public function getFollowingName(&$n, TokenCollection $tokens) {
68
        $n = $n + 2;
69
        return $this->getUnder(array('{', ' ', ';', '('), $n, $tokens);
70
    }
71
72
    /**
73
     * Get the position of the brace which closes the next brace
74
     *
75
     * @param integer $n
76
     * @param TokenCollection $tokens
77
     * @return null
78
     */
79
    public function getPositionOfClosingBrace(&$n, TokenCollection $tokens) {
80
        // search the end of the method
81
        $openBrace = 0;
82
        $start = null;
83
        $len = sizeof($tokens);
84
        for($i = $n; $i < $len; $i++) {
85
            $token = $tokens[$i];
86
            if(T_STRING == $token->getType()) {
87
                switch($token->getValue()) {
88
                    case '{':
89
                        $openBrace++;
90
                        if(is_null($start)) {
91
                            $start = $n = $i + 1;
92
                        }
93
                        break;
94
                    case '}':
95
                        $openBrace--;
96
                        if($openBrace <= 0) {
97
                            return $i;
98
                        }
99
                        break;
100
                }
101
            }
102
        }
103
        return null;
104
    }
105
106 View Code Duplication
    public function getExtendPostition(TokenCollection $tokens)
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...
107
    {
108
        $len = sizeof($tokens);
109
        for($i = 0; $i < $len; $i++) {
110
            $token = $tokens[$i];
111
            if ($token->getValue() === 'extends') {
112
                return $i;
113
            }
114
        }
115
        return null;
116
    }
117 View Code Duplication
    public function getClassNamePosition(TokenCollection $tokens)
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...
118
    {
119
        $len = sizeof($tokens);
120
        for($i = 0; $i < $len; $i++) {
121
            $token = $tokens[$i];
122
            if ($token->getValue() === 'class') {
123
                return $i;
124
            }
125
        }
126
        return null;
127
    }
128
129
    public function getPositionOfPrevious($tokenType, $n, TokenCollection $tokens) {
130 View Code Duplication
        for($i = $n; $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...
131
            if($tokenType == $tokens->get($i)->getType()) {
132
                return $i;
133
            }
134
        }
135
        return null;
136
    }
137
138
    public function getPositionOfNext($tokenType, $n, TokenCollection $tokens) {
139
        $len = sizeof($tokens);
140 View Code Duplication
        for($i = $n; $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...
141
            if($tokenType == $tokens->get($i)->getType()) {
142
                return $i;
143
            }
144
        }
145
        return null;
146
    }
147
148
    public function isPrecededBy($tokenType, $n, TokenCollection $tokens, $limit = 2) {
149
        $position = $this->getPositionOfPrevious($tokenType, $n, $tokens);
150
        return ($n - $position <= $limit);
151
    }
152
153
    public function isFollowedBy($tokenType, $n, TokenCollection $tokens, $limit = 2) {
154
        $position = $this->getPositionOfNext($tokenType, $n, $tokens);
155
        return ($position - $n >= $limit);
156
    }
157
};