Completed
Push — php7 ( b7b193...5ca92b )
by personal
03:30
created

Searcher::getExtendPosition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4286
cc 3
eloc 6
nc 3
nop 2
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
    public function getExtendPosition(TokenCollection $tokens, $start)
107
    {
108
        for($i = $start; $i > 0; $i--) {
109
            $token = $tokens[$i];
110
            if ($token->getValue() === 'extends') {
111
                return $i;
112
            }
113
        }
114
        return null;
115
    }
116
    public function getClassNamePosition(TokenCollection $tokens)
117
    {
118
        $len = sizeof($tokens);
119
        for($i = 0; $i < $len; $i++) {
120
            $token = $tokens[$i];
121
            if ($token->getValue() === 'class') {
122
                return $i;
123
            }
124
        }
125
        return null;
126
    }
127
128
    public function getPositionOfPrevious($tokenType, $n, TokenCollection $tokens) {
129 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...
130
            if($tokenType == $tokens->get($i)->getType()) {
131
                return $i;
132
            }
133
        }
134
        return null;
135
    }
136
137
    public function getPositionOfNext($tokenType, $n, TokenCollection $tokens) {
138
        $len = sizeof($tokens);
139 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...
140
            if($tokenType == $tokens->get($i)->getType()) {
141
                return $i;
142
            }
143
        }
144
        return null;
145
    }
146
147
    public function isPrecededBy($tokenType, $n, TokenCollection $tokens, $limit = 2) {
148
        $position = $this->getPositionOfPrevious($tokenType, $n, $tokens);
149
        return ($n - $position <= $limit);
150
    }
151
152
    public function isFollowedBy($tokenType, $n, TokenCollection $tokens, $limit = 2) {
153
        $position = $this->getPositionOfNext($tokenType, $n, $tokens);
154
        return ($position - $n >= $limit);
155
    }
156
};