Tokenizer::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Whitelist\Tokenizer;
10
11
use Maslosoft\Whitelist\Interfaces\TokenInterface;
12
use Maslosoft\Whitelist\Tokenizer\Collectors\FunctionCallsCollector;
13
use Maslosoft\Whitelist\Tokenizer\Collectors\MethodCallsCollector;
14
use Maslosoft\Whitelist\Tokenizer\Collectors\StaticMethodCallsCollector;
15
use Maslosoft\Whitelist\Tokenizer\Composite\FunctionCall;
16
use Maslosoft\Whitelist\Tokenizer\Tokens\SimpleToken;
17
use Maslosoft\Whitelist\Tokenizer\Tokens\Token;
18
19
/**
20
 * Tokenizer
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class Tokenizer
25
{
26
27
	private $rawTokens = [];
28
29
	/**
30
	 *
31
	 * @var TokenInterface[]
32
	 */
33
	private $tokens = [];
34
35
	public function __construct($code)
36
	{
37
		$this->rawTokens = token_get_all($code);
38
		foreach ($this->rawTokens as $index => $token)
39
		{
40
			if (is_array($token))
41
			{
42
				$this->tokens[$index] = new Token($token, $this->tokens, $index);
43
			}
44
			else
45
			{
46
				$this->tokens[$index] = new SimpleToken($token, $this->tokens, $index);
47
			}
48
			$this->tokens[$index]->name = token_name($this->tokens[$index]->type);
0 ignored issues
show
Bug introduced by
$this->tokens[$index]->type of type string is incompatible with the type integer expected by parameter $token of token_name(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
			$this->tokens[$index]->name = token_name(/** @scrutinizer ignore-type */ $this->tokens[$index]->type);
Loading history...
49
		}
50
	}
51
52
	/**
53
	 *
54
	 * @return TokenInterface[];
55
	 */
56
	public function getTokens()
57
	{
58
		return $this->tokens;
59
	}
60
61
	/**
62
	 * Get function tokens. This includes:
63
	 *
64
	 * * Simple functions
65
	 * * eval
66
	 * * echo
67
	 * * print
68
	 *
69
	 *  This does not include:
70
	 *
71
	 * * Method calls
72
	 * * Variable name function calls
73
	 * * Requires and includes
74
	 *
75
	 * @return TokenInterface[]|Token[]|SimpleToken[]
76
	 */
77
	public function getFunctions()
78
	{
79
		return (new FunctionCallsCollector)->collect($this->tokens);
80
	}
81
82
	/**
83
	 * Get static method calls
84
	 * @return array|TokenInterface[]
85
	 */
86
	public function getStaticMethodCalls()
87
	{
88
		return (new StaticMethodCallsCollector)->collect($this->tokens);
89
	}
90
91
	public function getMethodCalls()
92
	{
93
		return (new MethodCallsCollector)->collect($this->tokens);
94
	}
95
}
96