Conditions | 4 |
Paths | 6 |
Total Lines | 32 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 4 |
Changes | 0 |
1 | <?php |
||
21 | 465 | public static function getTokensFromString($code) { |
|
22 | try { |
||
23 | 465 | $tokens = token_get_all($code, TOKEN_PARSE); |
|
24 | 72 | } catch (\ParseError $e) { |
|
25 | // with TOKEN_PARSE flag, the function throws on invalid code |
||
26 | // let's just ignore the error and tokenize the code without the flag |
||
27 | 72 | $tokens = token_get_all($code); |
|
28 | } |
||
29 | |||
30 | 465 | foreach ($tokens as $index => $tokenData) { |
|
31 | |||
32 | 465 | if (!is_array($tokenData)) { |
|
33 | 444 | $previousIndex = $index - 1; |
|
34 | |||
35 | /** @var Token $previousToken */ |
||
36 | 444 | $previousToken = $tokens[$previousIndex]; |
|
37 | 444 | $line = $previousToken->getLine() + substr_count($previousToken->getValue(), "\n"); |
|
38 | $tokenData = [ |
||
39 | 444 | Token::INVALID_TYPE, |
|
40 | 444 | $tokenData, |
|
41 | 444 | $line, |
|
42 | ]; |
||
43 | } |
||
44 | |||
45 | 465 | $token = new Token($tokenData); |
|
46 | 465 | $token->setIndex($index); |
|
47 | 465 | $tokens[$index] = $token; |
|
48 | |||
49 | } |
||
50 | |||
51 | 465 | return $tokens; |
|
52 | } |
||
53 | |||
70 |