Newer
Older
/*
* This file is part of the PHP_TokenStream package.
* (c) Sebastian Bergmann <sebastian@phpunit.de>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* A stream of PHP tokens.
*
Angie Byron
committed
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://github.com/sebastianbergmann/php-token-stream/tree
* @since Class available since Release 1.0.0
*/
class PHP_Token_Stream implements ArrayAccess, Countable, SeekableIterator
{
/**
* @var array
*/
protected static $customTokens = array(
Angie Byron
committed
'(' => 'PHP_Token_OPEN_BRACKET',
')' => 'PHP_Token_CLOSE_BRACKET',
'[' => 'PHP_Token_OPEN_SQUARE',
']' => 'PHP_Token_CLOSE_SQUARE',
'{' => 'PHP_Token_OPEN_CURLY',
'}' => 'PHP_Token_CLOSE_CURLY',
';' => 'PHP_Token_SEMICOLON',
'.' => 'PHP_Token_DOT',
',' => 'PHP_Token_COMMA',
'=' => 'PHP_Token_EQUAL',
'<' => 'PHP_Token_LT',
'>' => 'PHP_Token_GT',
'+' => 'PHP_Token_PLUS',
'-' => 'PHP_Token_MINUS',
'*' => 'PHP_Token_MULT',
'/' => 'PHP_Token_DIV',
'?' => 'PHP_Token_QUESTION_MARK',
'!' => 'PHP_Token_EXCLAMATION_MARK',
':' => 'PHP_Token_COLON',
'"' => 'PHP_Token_DOUBLE_QUOTES',
'@' => 'PHP_Token_AT',
'&' => 'PHP_Token_AMPERSAND',
'%' => 'PHP_Token_PERCENT',
'|' => 'PHP_Token_PIPE',
'$' => 'PHP_Token_DOLLAR',
'^' => 'PHP_Token_CARET',
'~' => 'PHP_Token_TILDE',
'`' => 'PHP_Token_BACKTICK'
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
);
/**
* @var string
*/
protected $filename;
/**
* @var array
*/
protected $tokens = array();
/**
* @var integer
*/
protected $position = 0;
/**
* @var array
*/
protected $linesOfCode = array('loc' => 0, 'cloc' => 0, 'ncloc' => 0);
/**
* @var array
*/
protected $classes;
/**
* @var array
*/
protected $functions;
/**
* @var array
*/
protected $includes;
/**
* @var array
*/
protected $interfaces;
/**
* @var array
*/
protected $traits;
Angie Byron
committed
/**
* @var array
*/
protected $lineToFunctionMap = array();
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Constructor.
*
* @param string $sourceCode
*/
public function __construct($sourceCode)
{
if (is_file($sourceCode)) {
$this->filename = $sourceCode;
$sourceCode = file_get_contents($sourceCode);
}
$this->scan($sourceCode);
}
/**
* Destructor.
*/
public function __destruct()
{
$this->tokens = array();
}
/**
* @return string
*/
public function __toString()
{
$buffer = '';
foreach ($this as $token) {
$buffer .= $token;
}
return $buffer;
}
/**
* @return string
* @since Method available since Release 1.1.0
*/
public function getFilename()
{
return $this->filename;
}
/**
* Scans the source for sequences of characters and converts them into a
* stream of tokens.
*
* @param string $sourceCode
*/
protected function scan($sourceCode)
{
$line = 1;
$tokens = token_get_all($sourceCode);
$numTokens = count($tokens);
Angie Byron
committed
$lastNonWhitespaceTokenWasDoubleColon = false;
Angie Byron
committed
for ($i = 0; $i < $numTokens; ++$i) {
$token = $tokens[$i];
unset($tokens[$i]);
if (is_array($token)) {
Angie Byron
committed
$name = substr(token_name($token[0]), 2);
$text = $token[1];
if ($lastNonWhitespaceTokenWasDoubleColon && $name == 'CLASS') {
$name = 'CLASS_NAME_CONSTANT';
}
$tokenClass = 'PHP_Token_' . $name;
} else {
$text = $token;
$tokenClass = self::$customTokens[$token];
}
$this->tokens[] = new $tokenClass($text, $line, $this, $i);
$lines = substr_count($text, "\n");
$line += $lines;
if ($tokenClass == 'PHP_Token_HALT_COMPILER') {
break;
Angie Byron
committed
} elseif ($tokenClass == 'PHP_Token_COMMENT' ||
$tokenClass == 'PHP_Token_DOC_COMMENT') {
$this->linesOfCode['cloc'] += $lines + 1;
}
Angie Byron
committed
if ($name == 'DOUBLE_COLON') {
Angie Byron
committed
$lastNonWhitespaceTokenWasDoubleColon = true;
} elseif ($name != 'WHITESPACE') {
$lastNonWhitespaceTokenWasDoubleColon = false;
Angie Byron
committed
}
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
}
$this->linesOfCode['loc'] = substr_count($sourceCode, "\n");
$this->linesOfCode['ncloc'] = $this->linesOfCode['loc'] -
$this->linesOfCode['cloc'];
}
/**
* @return integer
*/
public function count()
{
return count($this->tokens);
}
/**
* @return PHP_Token[]
*/
public function tokens()
{
return $this->tokens;
}
/**
* @return array
*/
public function getClasses()
{
Angie Byron
committed
if ($this->classes !== null) {
return $this->classes;
}
$this->parse();
return $this->classes;
}
/**
* @return array
*/
public function getFunctions()
{
Angie Byron
committed
if ($this->functions !== null) {
return $this->functions;
}
$this->parse();
return $this->functions;
}
/**
* @return array
*/
public function getInterfaces()
{
Angie Byron
committed
if ($this->interfaces !== null) {
return $this->interfaces;
}
$this->parse();
return $this->interfaces;
}
/**
* @return array
* @since Method available since Release 1.1.0
*/
public function getTraits()
{
Angie Byron
committed
if ($this->traits !== null) {
return $this->traits;
}
$this->parse();
return $this->traits;
}
/**
* Gets the names of all files that have been included
* using include(), include_once(), require() or require_once().
*
* Parameter $categorize set to TRUE causing this function to return a
* multi-dimensional array with categories in the keys of the first dimension
* and constants and their values in the second dimension.
*
* Parameter $category allow to filter following specific inclusion type
*
* @param bool $categorize OPTIONAL
* @param string $category OPTIONAL Either 'require_once', 'require',
* 'include_once', 'include'.
* @return array
* @since Method available since Release 1.1.0
*/
Angie Byron
committed
public function getIncludes($categorize = false, $category = null)
Angie Byron
committed
if ($this->includes === null) {
$this->includes = array(
'require_once' => array(),
'require' => array(),
'include_once' => array(),
'include' => array()
);
foreach ($this->tokens as $token) {
switch (get_class($token)) {
case 'PHP_Token_REQUIRE_ONCE':
case 'PHP_Token_REQUIRE':
case 'PHP_Token_INCLUDE_ONCE':
Angie Byron
committed
case 'PHP_Token_INCLUDE':
$this->includes[$token->getType()][] = $token->getName();
Angie Byron
committed
break;
}
}
}
if (isset($this->includes[$category])) {
$includes = $this->includes[$category];
Angie Byron
committed
} elseif ($categorize === false) {
$includes = array_merge(
Angie Byron
committed
$this->includes['require_once'],
$this->includes['require'],
$this->includes['include_once'],
$this->includes['include']
);
} else {
$includes = $this->includes;
}
return $includes;
}
Angie Byron
committed
/**
* Returns the name of the function or method a line belongs to.
*
* @return string or null if the line is not in a function or method
* @since Method available since Release 1.2.0
*/
public function getFunctionForLine($line)
{
$this->parse();
if (isset($this->lineToFunctionMap[$line])) {
return $this->lineToFunctionMap[$line];
}
}
protected function parse()
{
$this->interfaces = array();
$this->classes = array();
$this->traits = array();
$this->functions = array();
Angie Byron
committed
$class = false;
$classEndLine = false;
$trait = false;
$traitEndLine = false;
$interface = false;
$interfaceEndLine = false;
foreach ($this->tokens as $token) {
switch (get_class($token)) {
Angie Byron
committed
case 'PHP_Token_HALT_COMPILER':
return;
Angie Byron
committed
case 'PHP_Token_INTERFACE':
$interface = $token->getName();
$interfaceEndLine = $token->getEndLine();
$this->interfaces[$interface] = array(
'methods' => array(),
'parent' => $token->getParent(),
'keywords' => $token->getKeywords(),
'docblock' => $token->getDocblock(),
'startLine' => $token->getLine(),
'endLine' => $interfaceEndLine,
'package' => $token->getPackage(),
'file' => $this->filename
);
Angie Byron
committed
break;
case 'PHP_Token_CLASS':
Angie Byron
committed
case 'PHP_Token_TRAIT':
$tmp = array(
'methods' => array(),
'parent' => $token->getParent(),
'interfaces'=> $token->getInterfaces(),
'keywords' => $token->getKeywords(),
'docblock' => $token->getDocblock(),
'startLine' => $token->getLine(),
'endLine' => $token->getEndLine(),
'package' => $token->getPackage(),
'file' => $this->filename
);
if ($token instanceof PHP_Token_CLASS) {
$class = $token->getName();
$classEndLine = $token->getEndLine();
Angie Byron
committed
if ($class != 'anonymous class') {
$this->classes[$class] = $tmp;
}
} else {
$trait = $token->getName();
$traitEndLine = $token->getEndLine();
$this->traits[$trait] = $tmp;
}
Angie Byron
committed
break;
Angie Byron
committed
case 'PHP_Token_FUNCTION':
$name = $token->getName();
$tmp = array(
'docblock' => $token->getDocblock(),
'keywords' => $token->getKeywords(),
'visibility'=> $token->getVisibility(),
'signature' => $token->getSignature(),
'startLine' => $token->getLine(),
'endLine' => $token->getEndLine(),
'ccn' => $token->getCCN(),
'file' => $this->filename
);
Angie Byron
committed
if ($class === false &&
$trait === false &&
$interface === false) {
$this->functions[$name] = $tmp;
Angie Byron
committed
$this->addFunctionToMap(
Angie Byron
committed
$name,
$tmp['startLine'],
$tmp['endLine']
Angie Byron
committed
);
Angie Byron
committed
} elseif ($class !== false && $class != 'anonymous class') {
$this->classes[$class]['methods'][$name] = $tmp;
Angie Byron
committed
$this->addFunctionToMap(
Angie Byron
committed
$class . '::' . $name,
$tmp['startLine'],
$tmp['endLine']
Angie Byron
committed
);
Angie Byron
committed
} elseif ($trait !== false) {
$this->traits[$trait]['methods'][$name] = $tmp;
Angie Byron
committed
$this->addFunctionToMap(
Angie Byron
committed
$trait . '::' . $name,
$tmp['startLine'],
$tmp['endLine']
Angie Byron
committed
);
Angie Byron
committed
} else {
$this->interfaces[$interface]['methods'][$name] = $tmp;
}
Angie Byron
committed
break;
Angie Byron
committed
case 'PHP_Token_CLOSE_CURLY':
if ($classEndLine !== false &&
$classEndLine == $token->getLine()) {
Angie Byron
committed
$class = false;
$classEndLine = false;
} elseif ($traitEndLine !== false &&
$traitEndLine == $token->getLine()) {
Angie Byron
committed
$trait = false;
$traitEndLine = false;
} elseif ($interfaceEndLine !== false &&
$interfaceEndLine == $token->getLine()) {
Angie Byron
committed
$interface = false;
$interfaceEndLine = false;
Angie Byron
committed
break;
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
}
}
}
/**
* @return array
*/
public function getLinesOfCode()
{
return $this->linesOfCode;
}
/**
*/
public function rewind()
{
$this->position = 0;
}
/**
* @return boolean
*/
public function valid()
{
return isset($this->tokens[$this->position]);
}
/**
* @return integer
*/
public function key()
{
return $this->position;
}
/**
* @return PHP_Token
*/
public function current()
{
return $this->tokens[$this->position];
}
/**
*/
public function next()
{
$this->position++;
}
/**
Angie Byron
committed
* @param integer $offset
* @return boolean
*/
public function offsetExists($offset)
{
return isset($this->tokens[$offset]);
}
/**
Angie Byron
committed
* @param integer $offset
* @return mixed
Angie Byron
committed
* @throws OutOfBoundsException
*/
public function offsetGet($offset)
{
Angie Byron
committed
if (!$this->offsetExists($offset)) {
throw new OutOfBoundsException(
sprintf(
'No token at position "%s"',
$offset
)
);
}
return $this->tokens[$offset];
}
/**
Angie Byron
committed
* @param integer $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->tokens[$offset] = $value;
}
/**
Angie Byron
committed
* @param integer $offset
* @throws OutOfBoundsException
*/
public function offsetUnset($offset)
{
Angie Byron
committed
if (!$this->offsetExists($offset)) {
throw new OutOfBoundsException(
sprintf(
'No token at position "%s"',
$offset
)
);
}
unset($this->tokens[$offset]);
}
/**
* Seek to an absolute position.
*
* @param integer $position
* @throws OutOfBoundsException
*/
public function seek($position)
{
$this->position = $position;
if (!$this->valid()) {
Angie Byron
committed
throw new OutOfBoundsException(
sprintf(
'No token at position "%s"',
$this->position
)
);
Angie Byron
committed
Angie Byron
committed
/**
* @param string $name
* @param integer $startLine
* @param integer $endLine
*/
Angie Byron
committed
private function addFunctionToMap($name, $startLine, $endLine)
{
for ($line = $startLine; $line <= $endLine; $line++) {
$this->lineToFunctionMap[$line] = $name;
}
}