Completed
Push — master ( 4e8869...451c37 )
by personal
15:58 queued 10:08
created

Searcher::getClassNamePosition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 11
loc 11
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
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
};