123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- <?php
- /*
- * This file is part of php-token-stream.
- *
- * (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.
- */
- class PHP_Token_Stream implements ArrayAccess, Countable, SeekableIterator
- {
- /**
- * @var array
- */
- protected static $customTokens = [
- '(' => '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'
- ];
- /**
- * @var string
- */
- protected $filename;
- /**
- * @var array
- */
- protected $tokens = [];
- /**
- * @var int
- */
- protected $position = 0;
- /**
- * @var array
- */
- protected $linesOfCode = ['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;
- /**
- * @var array
- */
- protected $lineToFunctionMap = [];
- /**
- * 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 = [];
- }
- /**
- * @return string
- */
- public function __toString()
- {
- $buffer = '';
- foreach ($this as $token) {
- $buffer .= $token;
- }
- return $buffer;
- }
- /**
- * @return string
- */
- 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)
- {
- $id = 0;
- $line = 1;
- $tokens = token_get_all($sourceCode);
- $numTokens = count($tokens);
- $lastNonWhitespaceTokenWasDoubleColon = false;
- for ($i = 0; $i < $numTokens; ++$i) {
- $token = $tokens[$i];
- $skip = 0;
- if (is_array($token)) {
- $name = substr(token_name($token[0]), 2);
- $text = $token[1];
- if ($lastNonWhitespaceTokenWasDoubleColon && $name == 'CLASS') {
- $name = 'CLASS_NAME_CONSTANT';
- } elseif ($name == 'USE' && isset($tokens[$i + 2][0]) && $tokens[$i + 2][0] == T_FUNCTION) {
- $name = 'USE_FUNCTION';
- $text .= $tokens[$i + 1][1] . $tokens[$i + 2][1];
- $skip = 2;
- }
- $tokenClass = 'PHP_Token_' . $name;
- } else {
- $text = $token;
- $tokenClass = self::$customTokens[$token];
- }
- $this->tokens[] = new $tokenClass($text, $line, $this, $id++);
- $lines = substr_count($text, "\n");
- $line += $lines;
- if ($tokenClass == 'PHP_Token_HALT_COMPILER') {
- break;
- } elseif ($tokenClass == 'PHP_Token_COMMENT' ||
- $tokenClass == 'PHP_Token_DOC_COMMENT') {
- $this->linesOfCode['cloc'] += $lines + 1;
- }
- if ($name == 'DOUBLE_COLON') {
- $lastNonWhitespaceTokenWasDoubleColon = true;
- } elseif ($name != 'WHITESPACE') {
- $lastNonWhitespaceTokenWasDoubleColon = false;
- }
- $i += $skip;
- }
- $this->linesOfCode['loc'] = substr_count($sourceCode, "\n");
- $this->linesOfCode['ncloc'] = $this->linesOfCode['loc'] -
- $this->linesOfCode['cloc'];
- }
- /**
- * @return int
- */
- public function count()
- {
- return count($this->tokens);
- }
- /**
- * @return PHP_Token[]
- */
- public function tokens()
- {
- return $this->tokens;
- }
- /**
- * @return array
- */
- public function getClasses()
- {
- if ($this->classes !== null) {
- return $this->classes;
- }
- $this->parse();
- return $this->classes;
- }
- /**
- * @return array
- */
- public function getFunctions()
- {
- if ($this->functions !== null) {
- return $this->functions;
- }
- $this->parse();
- return $this->functions;
- }
- /**
- * @return array
- */
- public function getInterfaces()
- {
- if ($this->interfaces !== null) {
- return $this->interfaces;
- }
- $this->parse();
- return $this->interfaces;
- }
- /**
- * @return array
- */
- public function getTraits()
- {
- 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
- */
- public function getIncludes($categorize = false, $category = null)
- {
- if ($this->includes === null) {
- $this->includes = [
- 'require_once' => [],
- 'require' => [],
- 'include_once' => [],
- 'include' => []
- ];
- foreach ($this->tokens as $token) {
- switch (get_class($token)) {
- case 'PHP_Token_REQUIRE_ONCE':
- case 'PHP_Token_REQUIRE':
- case 'PHP_Token_INCLUDE_ONCE':
- case 'PHP_Token_INCLUDE':
- $this->includes[$token->getType()][] = $token->getName();
- break;
- }
- }
- }
- if (isset($this->includes[$category])) {
- $includes = $this->includes[$category];
- } elseif ($categorize === false) {
- $includes = array_merge(
- $this->includes['require_once'],
- $this->includes['require'],
- $this->includes['include_once'],
- $this->includes['include']
- );
- } else {
- $includes = $this->includes;
- }
- return $includes;
- }
- /**
- * 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
- */
- public function getFunctionForLine($line)
- {
- $this->parse();
- if (isset($this->lineToFunctionMap[$line])) {
- return $this->lineToFunctionMap[$line];
- }
- }
- protected function parse()
- {
- $this->interfaces = [];
- $this->classes = [];
- $this->traits = [];
- $this->functions = [];
- $class = [];
- $classEndLine = [];
- $trait = false;
- $traitEndLine = false;
- $interface = false;
- $interfaceEndLine = false;
- foreach ($this->tokens as $token) {
- switch (get_class($token)) {
- case 'PHP_Token_HALT_COMPILER':
- return;
- case 'PHP_Token_INTERFACE':
- $interface = $token->getName();
- $interfaceEndLine = $token->getEndLine();
- $this->interfaces[$interface] = [
- 'methods' => [],
- 'parent' => $token->getParent(),
- 'keywords' => $token->getKeywords(),
- 'docblock' => $token->getDocblock(),
- 'startLine' => $token->getLine(),
- 'endLine' => $interfaceEndLine,
- 'package' => $token->getPackage(),
- 'file' => $this->filename
- ];
- break;
- case 'PHP_Token_CLASS':
- case 'PHP_Token_TRAIT':
- $tmp = [
- 'methods' => [],
- '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();
- $this->classes[$class[count($class) - 1]] = $tmp;
- } else {
- $trait = $token->getName();
- $traitEndLine = $token->getEndLine();
- $this->traits[$trait] = $tmp;
- }
- break;
- case 'PHP_Token_FUNCTION':
- $name = $token->getName();
- $tmp = [
- 'docblock' => $token->getDocblock(),
- 'keywords' => $token->getKeywords(),
- 'visibility'=> $token->getVisibility(),
- 'signature' => $token->getSignature(),
- 'startLine' => $token->getLine(),
- 'endLine' => $token->getEndLine(),
- 'ccn' => $token->getCCN(),
- 'file' => $this->filename
- ];
- if (empty($class) &&
- $trait === false &&
- $interface === false) {
- $this->functions[$name] = $tmp;
- $this->addFunctionToMap(
- $name,
- $tmp['startLine'],
- $tmp['endLine']
- );
- } elseif (!empty($class)) {
- $this->classes[$class[count($class) - 1]]['methods'][$name] = $tmp;
- $this->addFunctionToMap(
- $class[count($class) - 1] . '::' . $name,
- $tmp['startLine'],
- $tmp['endLine']
- );
- } elseif ($trait !== false) {
- $this->traits[$trait]['methods'][$name] = $tmp;
- $this->addFunctionToMap(
- $trait . '::' . $name,
- $tmp['startLine'],
- $tmp['endLine']
- );
- } else {
- $this->interfaces[$interface]['methods'][$name] = $tmp;
- }
- break;
- case 'PHP_Token_CLOSE_CURLY':
- if (!empty($classEndLine) &&
- $classEndLine[count($classEndLine) - 1] == $token->getLine()) {
- array_pop($classEndLine);
- array_pop($class);
- } elseif ($traitEndLine !== false &&
- $traitEndLine == $token->getLine()) {
- $trait = false;
- $traitEndLine = false;
- } elseif ($interfaceEndLine !== false &&
- $interfaceEndLine == $token->getLine()) {
- $interface = false;
- $interfaceEndLine = false;
- }
- break;
- }
- }
- }
- /**
- * @return array
- */
- public function getLinesOfCode()
- {
- return $this->linesOfCode;
- }
- /**
- */
- public function rewind()
- {
- $this->position = 0;
- }
- /**
- * @return bool
- */
- public function valid()
- {
- return isset($this->tokens[$this->position]);
- }
- /**
- * @return int
- */
- public function key()
- {
- return $this->position;
- }
- /**
- * @return PHP_Token
- */
- public function current()
- {
- return $this->tokens[$this->position];
- }
- /**
- */
- public function next()
- {
- $this->position++;
- }
- /**
- * @param int $offset
- *
- * @return bool
- */
- public function offsetExists($offset)
- {
- return isset($this->tokens[$offset]);
- }
- /**
- * @param int $offset
- *
- * @return mixed
- *
- * @throws OutOfBoundsException
- */
- public function offsetGet($offset)
- {
- if (!$this->offsetExists($offset)) {
- throw new OutOfBoundsException(
- sprintf(
- 'No token at position "%s"',
- $offset
- )
- );
- }
- return $this->tokens[$offset];
- }
- /**
- * @param int $offset
- * @param mixed $value
- */
- public function offsetSet($offset, $value)
- {
- $this->tokens[$offset] = $value;
- }
- /**
- * @param int $offset
- *
- * @throws OutOfBoundsException
- */
- public function offsetUnset($offset)
- {
- if (!$this->offsetExists($offset)) {
- throw new OutOfBoundsException(
- sprintf(
- 'No token at position "%s"',
- $offset
- )
- );
- }
- unset($this->tokens[$offset]);
- }
- /**
- * Seek to an absolute position.
- *
- * @param int $position
- *
- * @throws OutOfBoundsException
- */
- public function seek($position)
- {
- $this->position = $position;
- if (!$this->valid()) {
- throw new OutOfBoundsException(
- sprintf(
- 'No token at position "%s"',
- $this->position
- )
- );
- }
- }
- /**
- * @param string $name
- * @param int $startLine
- * @param int $endLine
- */
- private function addFunctionToMap($name, $startLine, $endLine)
- {
- for ($line = $startLine; $line <= $endLine; $line++) {
- $this->lineToFunctionMap[$line] = $name;
- }
- }
- }
|