pax_global_header00006660000000000000000000000064135400757160014522gustar00rootroot0000000000000052 comment=995192df77f63a59e47f025390d2d1fdf8f425ff php-token-stream-3.1.1/000077500000000000000000000000001354007571600147225ustar00rootroot00000000000000php-token-stream-3.1.1/.gitattributes000066400000000000000000000000171354007571600176130ustar00rootroot00000000000000*.php diff=php php-token-stream-3.1.1/.github/000077500000000000000000000000001354007571600162625ustar00rootroot00000000000000php-token-stream-3.1.1/.github/FUNDING.yml000066400000000000000000000000241354007571600200730ustar00rootroot00000000000000patreon: s_bergmann php-token-stream-3.1.1/.gitignore000066400000000000000000000000361354007571600167110ustar00rootroot00000000000000/.idea /composer.lock /vendor php-token-stream-3.1.1/.travis.yml000066400000000000000000000006121354007571600170320ustar00rootroot00000000000000language: php php: - 7.1 - 7.2 - 7.3 - 7.4snapshot sudo: false before_install: - composer self-update - composer clear-cache install: - travis_retry composer update --no-interaction --no-ansi --no-progress --no-suggest script: - ./vendor/bin/phpunit --coverage-clover=coverage.xml after_success: - bash <(curl -s https://codecov.io/bash) notifications: email: false php-token-stream-3.1.1/ChangeLog.md000066400000000000000000000042111354007571600170710ustar00rootroot00000000000000# Change Log All notable changes to `sebastianbergmann/php-token-stream` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. ## [3.1.1] - 2019-09-17 ### Fixed * Fixed [#84](https://github.com/sebastianbergmann/php-token-stream/issues/84): Methods named `class` are not handled correctly ## [3.1.0] - 2019-07-25 ### Added * Added support for `FN` and `COALESCE_EQUAL` tokens introduced in PHP 7.4 ## [3.0.2] - 2019-07-08 ### Changed * Implemented [#82](https://github.com/sebastianbergmann/php-token-stream/issues/82): Make sure this component works when its classes are prefixed using php-scoper ## [3.0.1] - 2018-10-30 ### Fixed * Fixed [#78](https://github.com/sebastianbergmann/php-token-stream/pull/78): `getEndTokenId()` does not handle string-dollar (`"${var}"`) interpolation ## [3.0.0] - 2018-02-01 ### Removed * Implemented [#71](https://github.com/sebastianbergmann/php-token-stream/issues/71): Remove code specific to Hack language constructs * Implemented [#72](https://github.com/sebastianbergmann/php-token-stream/issues/72): Drop support for PHP 7.0 ## [2.0.2] - 2017-11-27 ### Fixed * Fixed [#69](https://github.com/sebastianbergmann/php-token-stream/issues/69): `PHP_Token_USE_FUNCTION` does not serialize correctly ## [2.0.1] - 2017-08-20 ### Fixed * Fixed [#68](https://github.com/sebastianbergmann/php-token-stream/issues/68): Method with name `empty` wrongly recognized as anonymous function ## [2.0.0] - 2017-08-03 [3.1.1]: https://github.com/sebastianbergmann/php-token-stream/compare/3.1.0...3.1.1 [3.1.0]: https://github.com/sebastianbergmann/php-token-stream/compare/3.0.2...3.1.0 [3.0.2]: https://github.com/sebastianbergmann/php-token-stream/compare/3.0.1...3.0.2 [3.0.1]: https://github.com/sebastianbergmann/php-token-stream/compare/3.0.0...3.0.1 [3.0.0]: https://github.com/sebastianbergmann/php-token-stream/compare/2.0...3.0.0 [2.0.2]: https://github.com/sebastianbergmann/php-token-stream/compare/2.0.1...2.0.2 [2.0.1]: https://github.com/sebastianbergmann/php-token-stream/compare/2.0.0...2.0.1 [2.0.0]: https://github.com/sebastianbergmann/php-token-stream/compare/1.4.11...2.0.0 php-token-stream-3.1.1/LICENSE000066400000000000000000000030171354007571600157300ustar00rootroot00000000000000php-token-stream Copyright (c) 2009-2019, Sebastian Bergmann . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Sebastian Bergmann nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. php-token-stream-3.1.1/README.md000066400000000000000000000010661354007571600162040ustar00rootroot00000000000000[![Build Status](https://travis-ci.org/sebastianbergmann/php-token-stream.svg?branch=master)](https://travis-ci.org/sebastianbergmann/php-token-stream) # php-token-stream ## Installation You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): composer require phpunit/php-token-stream If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: composer require --dev phpunit/php-token-stream php-token-stream-3.1.1/build.xml000066400000000000000000000013761354007571600165520ustar00rootroot00000000000000 php-token-stream-3.1.1/composer.json000066400000000000000000000016361354007571600174520ustar00rootroot00000000000000{ "name": "phpunit/php-token-stream", "description": "Wrapper around PHP's tokenizer extension.", "type": "library", "keywords": ["tokenizer"], "homepage": "https://github.com/sebastianbergmann/php-token-stream/", "license": "BSD-3-Clause", "authors": [ { "name": "Sebastian Bergmann", "email": "sebastian@phpunit.de" } ], "support": { "issues": "https://github.com/sebastianbergmann/php-token-stream/issues" }, "prefer-stable": true, "require": { "php": "^7.1", "ext-tokenizer": "*" }, "require-dev": { "phpunit/phpunit": "^7.0" }, "config": { "optimize-autoloader": true, "sort-packages": true }, "autoload": { "classmap": [ "src/" ] }, "extra": { "branch-alias": { "dev-master": "3.1-dev" } } } php-token-stream-3.1.1/phpunit.xml000066400000000000000000000013641354007571600171370ustar00rootroot00000000000000 tests src php-token-stream-3.1.1/src/000077500000000000000000000000001354007571600155115ustar00rootroot00000000000000php-token-stream-3.1.1/src/Token.php000066400000000000000000000607511354007571600173130ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * A PHP token. */ abstract class PHP_Token { /** * @var string */ protected $text; /** * @var int */ protected $line; /** * @var PHP_Token_Stream */ protected $tokenStream; /** * @var int */ protected $id; /** * @param string $text * @param int $line * @param PHP_Token_Stream $tokenStream * @param int $id */ public function __construct($text, $line, PHP_Token_Stream $tokenStream, $id) { $this->text = $text; $this->line = $line; $this->tokenStream = $tokenStream; $this->id = $id; } /** * @return string */ public function __toString() { return $this->text; } /** * @return int */ public function getLine() { return $this->line; } /** * @return int */ public function getId() { return $this->id; } } abstract class PHP_TokenWithScope extends PHP_Token { /** * @var int */ protected $endTokenId; /** * Get the docblock for this token * * This method will fetch the docblock belonging to the current token. The * docblock must be placed on the line directly above the token to be * recognized. * * @return string|null Returns the docblock as a string if found */ public function getDocblock() { $tokens = $this->tokenStream->tokens(); $currentLineNumber = $tokens[$this->id]->getLine(); $prevLineNumber = $currentLineNumber - 1; for ($i = $this->id - 1; $i; $i--) { if (!isset($tokens[$i])) { return; } if ($tokens[$i] instanceof PHP_Token_FUNCTION || $tokens[$i] instanceof PHP_Token_CLASS || $tokens[$i] instanceof PHP_Token_TRAIT) { // Some other trait, class or function, no docblock can be // used for the current token break; } $line = $tokens[$i]->getLine(); if ($line == $currentLineNumber || ($line == $prevLineNumber && $tokens[$i] instanceof PHP_Token_WHITESPACE)) { continue; } if ($line < $currentLineNumber && !$tokens[$i] instanceof PHP_Token_DOC_COMMENT) { break; } return (string) $tokens[$i]; } } /** * @return int */ public function getEndTokenId() { $block = 0; $i = $this->id; $tokens = $this->tokenStream->tokens(); while ($this->endTokenId === null && isset($tokens[$i])) { if ($tokens[$i] instanceof PHP_Token_OPEN_CURLY || $tokens[$i] instanceof PHP_Token_DOLLAR_OPEN_CURLY_BRACES || $tokens[$i] instanceof PHP_Token_CURLY_OPEN) { $block++; } elseif ($tokens[$i] instanceof PHP_Token_CLOSE_CURLY) { $block--; if ($block === 0) { $this->endTokenId = $i; } } elseif (($this instanceof PHP_Token_FUNCTION || $this instanceof PHP_Token_NAMESPACE) && $tokens[$i] instanceof PHP_Token_SEMICOLON) { if ($block === 0) { $this->endTokenId = $i; } } $i++; } if ($this->endTokenId === null) { $this->endTokenId = $this->id; } return $this->endTokenId; } /** * @return int */ public function getEndLine() { return $this->tokenStream[$this->getEndTokenId()]->getLine(); } } abstract class PHP_TokenWithScopeAndVisibility extends PHP_TokenWithScope { /** * @return string */ public function getVisibility() { $tokens = $this->tokenStream->tokens(); for ($i = $this->id - 2; $i > $this->id - 7; $i -= 2) { if (isset($tokens[$i]) && ($tokens[$i] instanceof PHP_Token_PRIVATE || $tokens[$i] instanceof PHP_Token_PROTECTED || $tokens[$i] instanceof PHP_Token_PUBLIC)) { return strtolower( str_replace('PHP_Token_', '', PHP_Token_Util::getClass($tokens[$i])) ); } if (isset($tokens[$i]) && !($tokens[$i] instanceof PHP_Token_STATIC || $tokens[$i] instanceof PHP_Token_FINAL || $tokens[$i] instanceof PHP_Token_ABSTRACT)) { // no keywords; stop visibility search break; } } } /** * @return string */ public function getKeywords() { $keywords = []; $tokens = $this->tokenStream->tokens(); for ($i = $this->id - 2; $i > $this->id - 7; $i -= 2) { if (isset($tokens[$i]) && ($tokens[$i] instanceof PHP_Token_PRIVATE || $tokens[$i] instanceof PHP_Token_PROTECTED || $tokens[$i] instanceof PHP_Token_PUBLIC)) { continue; } if (isset($tokens[$i]) && ($tokens[$i] instanceof PHP_Token_STATIC || $tokens[$i] instanceof PHP_Token_FINAL || $tokens[$i] instanceof PHP_Token_ABSTRACT)) { $keywords[] = strtolower( str_replace('PHP_Token_', '', PHP_Token_Util::getClass($tokens[$i])) ); } } return implode(',', $keywords); } } abstract class PHP_Token_Includes extends PHP_Token { /** * @var string */ protected $name; /** * @var string */ protected $type; /** * @return string */ public function getName() { if ($this->name === null) { $this->process(); } return $this->name; } /** * @return string */ public function getType() { if ($this->type === null) { $this->process(); } return $this->type; } private function process() { $tokens = $this->tokenStream->tokens(); if ($tokens[$this->id + 2] instanceof PHP_Token_CONSTANT_ENCAPSED_STRING) { $this->name = trim($tokens[$this->id + 2], "'\""); $this->type = strtolower( str_replace('PHP_Token_', '', PHP_Token_Util::getClass($tokens[$this->id])) ); } } } class PHP_Token_FUNCTION extends PHP_TokenWithScopeAndVisibility { /** * @var array */ protected $arguments; /** * @var int */ protected $ccn; /** * @var string */ protected $name; /** * @var string */ protected $signature; /** * @var bool */ private $anonymous = false; /** * @return array */ public function getArguments() { if ($this->arguments !== null) { return $this->arguments; } $this->arguments = []; $tokens = $this->tokenStream->tokens(); $typeDeclaration = null; // Search for first token inside brackets $i = $this->id + 2; while (!$tokens[$i - 1] instanceof PHP_Token_OPEN_BRACKET) { $i++; } while (!$tokens[$i] instanceof PHP_Token_CLOSE_BRACKET) { if ($tokens[$i] instanceof PHP_Token_STRING) { $typeDeclaration = (string) $tokens[$i]; } elseif ($tokens[$i] instanceof PHP_Token_VARIABLE) { $this->arguments[(string) $tokens[$i]] = $typeDeclaration; $typeDeclaration = null; } $i++; } return $this->arguments; } /** * @return string */ public function getName() { if ($this->name !== null) { return $this->name; } $tokens = $this->tokenStream->tokens(); $i = $this->id + 1; if ($tokens[$i] instanceof PHP_Token_WHITESPACE) { $i++; } if ($tokens[$i] instanceof PHP_Token_AMPERSAND) { $i++; } if ($tokens[$i + 1] instanceof PHP_Token_OPEN_BRACKET) { $this->name = (string) $tokens[$i]; } elseif ($tokens[$i + 1] instanceof PHP_Token_WHITESPACE && $tokens[$i + 2] instanceof PHP_Token_OPEN_BRACKET) { $this->name = (string) $tokens[$i]; } else { $this->anonymous = true; $this->name = sprintf( 'anonymousFunction:%s#%s', $this->getLine(), $this->getId() ); } if (!$this->isAnonymous()) { for ($i = $this->id; $i; --$i) { if ($tokens[$i] instanceof PHP_Token_NAMESPACE) { $this->name = $tokens[$i]->getName() . '\\' . $this->name; break; } if ($tokens[$i] instanceof PHP_Token_INTERFACE) { break; } } } return $this->name; } /** * @return int */ public function getCCN() { if ($this->ccn !== null) { return $this->ccn; } $this->ccn = 1; $end = $this->getEndTokenId(); $tokens = $this->tokenStream->tokens(); for ($i = $this->id; $i <= $end; $i++) { switch (PHP_Token_Util::getClass($tokens[$i])) { case 'PHP_Token_IF': case 'PHP_Token_ELSEIF': case 'PHP_Token_FOR': case 'PHP_Token_FOREACH': case 'PHP_Token_WHILE': case 'PHP_Token_CASE': case 'PHP_Token_CATCH': case 'PHP_Token_BOOLEAN_AND': case 'PHP_Token_LOGICAL_AND': case 'PHP_Token_BOOLEAN_OR': case 'PHP_Token_LOGICAL_OR': case 'PHP_Token_QUESTION_MARK': $this->ccn++; break; } } return $this->ccn; } /** * @return string */ public function getSignature() { if ($this->signature !== null) { return $this->signature; } if ($this->isAnonymous()) { $this->signature = 'anonymousFunction'; $i = $this->id + 1; } else { $this->signature = ''; $i = $this->id + 2; } $tokens = $this->tokenStream->tokens(); while (isset($tokens[$i]) && !$tokens[$i] instanceof PHP_Token_OPEN_CURLY && !$tokens[$i] instanceof PHP_Token_SEMICOLON) { $this->signature .= $tokens[$i++]; } $this->signature = trim($this->signature); return $this->signature; } /** * @return bool */ public function isAnonymous() { return $this->anonymous; } } class PHP_Token_INTERFACE extends PHP_TokenWithScopeAndVisibility { /** * @var array */ protected $interfaces; /** * @return string */ public function getName() { return (string) $this->tokenStream[$this->id + 2]; } /** * @return bool */ public function hasParent() { return $this->tokenStream[$this->id + 4] instanceof PHP_Token_EXTENDS; } /** * @return array */ public function getPackage() { $className = $this->getName(); $docComment = $this->getDocblock(); $result = [ 'namespace' => '', 'fullPackage' => '', 'category' => '', 'package' => '', 'subpackage' => '' ]; for ($i = $this->id; $i; --$i) { if ($this->tokenStream[$i] instanceof PHP_Token_NAMESPACE) { $result['namespace'] = $this->tokenStream[$i]->getName(); break; } } if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) { $result['category'] = $matches[1]; } if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) { $result['package'] = $matches[1]; $result['fullPackage'] = $matches[1]; } if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) { $result['subpackage'] = $matches[1]; $result['fullPackage'] .= '.' . $matches[1]; } if (empty($result['fullPackage'])) { $result['fullPackage'] = $this->arrayToName( explode('_', str_replace('\\', '_', $className)), '.' ); } return $result; } /** * @param array $parts * @param string $join * * @return string */ protected function arrayToName(array $parts, $join = '\\') { $result = ''; if (count($parts) > 1) { array_pop($parts); $result = implode($join, $parts); } return $result; } /** * @return bool|string */ public function getParent() { if (!$this->hasParent()) { return false; } $i = $this->id + 6; $tokens = $this->tokenStream->tokens(); $className = (string) $tokens[$i]; while (isset($tokens[$i + 1]) && !$tokens[$i + 1] instanceof PHP_Token_WHITESPACE) { $className .= (string) $tokens[++$i]; } return $className; } /** * @return bool */ public function hasInterfaces() { return (isset($this->tokenStream[$this->id + 4]) && $this->tokenStream[$this->id + 4] instanceof PHP_Token_IMPLEMENTS) || (isset($this->tokenStream[$this->id + 8]) && $this->tokenStream[$this->id + 8] instanceof PHP_Token_IMPLEMENTS); } /** * @return array|bool */ public function getInterfaces() { if ($this->interfaces !== null) { return $this->interfaces; } if (!$this->hasInterfaces()) { return ($this->interfaces = false); } if ($this->tokenStream[$this->id + 4] instanceof PHP_Token_IMPLEMENTS) { $i = $this->id + 3; } else { $i = $this->id + 7; } $tokens = $this->tokenStream->tokens(); while (!$tokens[$i + 1] instanceof PHP_Token_OPEN_CURLY) { $i++; if ($tokens[$i] instanceof PHP_Token_STRING) { $this->interfaces[] = (string) $tokens[$i]; } } return $this->interfaces; } } class PHP_Token_ABSTRACT extends PHP_Token { } class PHP_Token_AMPERSAND extends PHP_Token { } class PHP_Token_AND_EQUAL extends PHP_Token { } class PHP_Token_ARRAY extends PHP_Token { } class PHP_Token_ARRAY_CAST extends PHP_Token { } class PHP_Token_AS extends PHP_Token { } class PHP_Token_AT extends PHP_Token { } class PHP_Token_BACKTICK extends PHP_Token { } class PHP_Token_BAD_CHARACTER extends PHP_Token { } class PHP_Token_BOOLEAN_AND extends PHP_Token { } class PHP_Token_BOOLEAN_OR extends PHP_Token { } class PHP_Token_BOOL_CAST extends PHP_Token { } class PHP_Token_BREAK extends PHP_Token { } class PHP_Token_CARET extends PHP_Token { } class PHP_Token_CASE extends PHP_Token { } class PHP_Token_CATCH extends PHP_Token { } class PHP_Token_CHARACTER extends PHP_Token { } class PHP_Token_CLASS extends PHP_Token_INTERFACE { /** * @var bool */ private $anonymous = false; /** * @var string */ private $name; /** * @return string */ public function getName() { if ($this->name !== null) { return $this->name; } $next = $this->tokenStream[$this->id + 1]; if ($next instanceof PHP_Token_WHITESPACE) { $next = $this->tokenStream[$this->id + 2]; } if ($next instanceof PHP_Token_STRING) { $this->name =(string) $next; return $this->name; } if ($next instanceof PHP_Token_OPEN_CURLY || $next instanceof PHP_Token_EXTENDS || $next instanceof PHP_Token_IMPLEMENTS) { $this->name = sprintf( 'AnonymousClass:%s#%s', $this->getLine(), $this->getId() ); $this->anonymous = true; return $this->name; } } public function isAnonymous() { return $this->anonymous; } } class PHP_Token_CLASS_C extends PHP_Token { } class PHP_Token_CLASS_NAME_CONSTANT extends PHP_Token { } class PHP_Token_CLONE extends PHP_Token { } class PHP_Token_CLOSE_BRACKET extends PHP_Token { } class PHP_Token_CLOSE_CURLY extends PHP_Token { } class PHP_Token_CLOSE_SQUARE extends PHP_Token { } class PHP_Token_CLOSE_TAG extends PHP_Token { } class PHP_Token_COLON extends PHP_Token { } class PHP_Token_COMMA extends PHP_Token { } class PHP_Token_COMMENT extends PHP_Token { } class PHP_Token_CONCAT_EQUAL extends PHP_Token { } class PHP_Token_CONST extends PHP_Token { } class PHP_Token_CONSTANT_ENCAPSED_STRING extends PHP_Token { } class PHP_Token_CONTINUE extends PHP_Token { } class PHP_Token_CURLY_OPEN extends PHP_Token { } class PHP_Token_DEC extends PHP_Token { } class PHP_Token_DECLARE extends PHP_Token { } class PHP_Token_DEFAULT extends PHP_Token { } class PHP_Token_DIV extends PHP_Token { } class PHP_Token_DIV_EQUAL extends PHP_Token { } class PHP_Token_DNUMBER extends PHP_Token { } class PHP_Token_DO extends PHP_Token { } class PHP_Token_DOC_COMMENT extends PHP_Token { } class PHP_Token_DOLLAR extends PHP_Token { } class PHP_Token_DOLLAR_OPEN_CURLY_BRACES extends PHP_Token { } class PHP_Token_DOT extends PHP_Token { } class PHP_Token_DOUBLE_ARROW extends PHP_Token { } class PHP_Token_DOUBLE_CAST extends PHP_Token { } class PHP_Token_DOUBLE_COLON extends PHP_Token { } class PHP_Token_DOUBLE_QUOTES extends PHP_Token { } class PHP_Token_ECHO extends PHP_Token { } class PHP_Token_ELSE extends PHP_Token { } class PHP_Token_ELSEIF extends PHP_Token { } class PHP_Token_EMPTY extends PHP_Token { } class PHP_Token_ENCAPSED_AND_WHITESPACE extends PHP_Token { } class PHP_Token_ENDDECLARE extends PHP_Token { } class PHP_Token_ENDFOR extends PHP_Token { } class PHP_Token_ENDFOREACH extends PHP_Token { } class PHP_Token_ENDIF extends PHP_Token { } class PHP_Token_ENDSWITCH extends PHP_Token { } class PHP_Token_ENDWHILE extends PHP_Token { } class PHP_Token_END_HEREDOC extends PHP_Token { } class PHP_Token_EQUAL extends PHP_Token { } class PHP_Token_EVAL extends PHP_Token { } class PHP_Token_EXCLAMATION_MARK extends PHP_Token { } class PHP_Token_EXIT extends PHP_Token { } class PHP_Token_EXTENDS extends PHP_Token { } class PHP_Token_FILE extends PHP_Token { } class PHP_Token_FINAL extends PHP_Token { } class PHP_Token_FOR extends PHP_Token { } class PHP_Token_FOREACH extends PHP_Token { } class PHP_Token_FUNC_C extends PHP_Token { } class PHP_Token_GLOBAL extends PHP_Token { } class PHP_Token_GT extends PHP_Token { } class PHP_Token_IF extends PHP_Token { } class PHP_Token_IMPLEMENTS extends PHP_Token { } class PHP_Token_INC extends PHP_Token { } class PHP_Token_INCLUDE extends PHP_Token_Includes { } class PHP_Token_INCLUDE_ONCE extends PHP_Token_Includes { } class PHP_Token_INLINE_HTML extends PHP_Token { } class PHP_Token_INSTANCEOF extends PHP_Token { } class PHP_Token_INT_CAST extends PHP_Token { } class PHP_Token_ISSET extends PHP_Token { } class PHP_Token_IS_EQUAL extends PHP_Token { } class PHP_Token_IS_GREATER_OR_EQUAL extends PHP_Token { } class PHP_Token_IS_IDENTICAL extends PHP_Token { } class PHP_Token_IS_NOT_EQUAL extends PHP_Token { } class PHP_Token_IS_NOT_IDENTICAL extends PHP_Token { } class PHP_Token_IS_SMALLER_OR_EQUAL extends PHP_Token { } class PHP_Token_LINE extends PHP_Token { } class PHP_Token_LIST extends PHP_Token { } class PHP_Token_LNUMBER extends PHP_Token { } class PHP_Token_LOGICAL_AND extends PHP_Token { } class PHP_Token_LOGICAL_OR extends PHP_Token { } class PHP_Token_LOGICAL_XOR extends PHP_Token { } class PHP_Token_LT extends PHP_Token { } class PHP_Token_METHOD_C extends PHP_Token { } class PHP_Token_MINUS extends PHP_Token { } class PHP_Token_MINUS_EQUAL extends PHP_Token { } class PHP_Token_MOD_EQUAL extends PHP_Token { } class PHP_Token_MULT extends PHP_Token { } class PHP_Token_MUL_EQUAL extends PHP_Token { } class PHP_Token_NEW extends PHP_Token { } class PHP_Token_NUM_STRING extends PHP_Token { } class PHP_Token_OBJECT_CAST extends PHP_Token { } class PHP_Token_OBJECT_OPERATOR extends PHP_Token { } class PHP_Token_OPEN_BRACKET extends PHP_Token { } class PHP_Token_OPEN_CURLY extends PHP_Token { } class PHP_Token_OPEN_SQUARE extends PHP_Token { } class PHP_Token_OPEN_TAG extends PHP_Token { } class PHP_Token_OPEN_TAG_WITH_ECHO extends PHP_Token { } class PHP_Token_OR_EQUAL extends PHP_Token { } class PHP_Token_PAAMAYIM_NEKUDOTAYIM extends PHP_Token { } class PHP_Token_PERCENT extends PHP_Token { } class PHP_Token_PIPE extends PHP_Token { } class PHP_Token_PLUS extends PHP_Token { } class PHP_Token_PLUS_EQUAL extends PHP_Token { } class PHP_Token_PRINT extends PHP_Token { } class PHP_Token_PRIVATE extends PHP_Token { } class PHP_Token_PROTECTED extends PHP_Token { } class PHP_Token_PUBLIC extends PHP_Token { } class PHP_Token_QUESTION_MARK extends PHP_Token { } class PHP_Token_REQUIRE extends PHP_Token_Includes { } class PHP_Token_REQUIRE_ONCE extends PHP_Token_Includes { } class PHP_Token_RETURN extends PHP_Token { } class PHP_Token_SEMICOLON extends PHP_Token { } class PHP_Token_SL extends PHP_Token { } class PHP_Token_SL_EQUAL extends PHP_Token { } class PHP_Token_SR extends PHP_Token { } class PHP_Token_SR_EQUAL extends PHP_Token { } class PHP_Token_START_HEREDOC extends PHP_Token { } class PHP_Token_STATIC extends PHP_Token { } class PHP_Token_STRING extends PHP_Token { } class PHP_Token_STRING_CAST extends PHP_Token { } class PHP_Token_STRING_VARNAME extends PHP_Token { } class PHP_Token_SWITCH extends PHP_Token { } class PHP_Token_THROW extends PHP_Token { } class PHP_Token_TILDE extends PHP_Token { } class PHP_Token_TRY extends PHP_Token { } class PHP_Token_UNSET extends PHP_Token { } class PHP_Token_UNSET_CAST extends PHP_Token { } class PHP_Token_USE extends PHP_Token { } class PHP_Token_USE_FUNCTION extends PHP_Token { } class PHP_Token_VAR extends PHP_Token { } class PHP_Token_VARIABLE extends PHP_Token { } class PHP_Token_WHILE extends PHP_Token { } class PHP_Token_WHITESPACE extends PHP_Token { } class PHP_Token_XOR_EQUAL extends PHP_Token { } // Tokens introduced in PHP 5.1 class PHP_Token_HALT_COMPILER extends PHP_Token { } // Tokens introduced in PHP 5.3 class PHP_Token_DIR extends PHP_Token { } class PHP_Token_GOTO extends PHP_Token { } class PHP_Token_NAMESPACE extends PHP_TokenWithScope { /** * @return string */ public function getName() { $tokens = $this->tokenStream->tokens(); $namespace = (string) $tokens[$this->id + 2]; for ($i = $this->id + 3;; $i += 2) { if (isset($tokens[$i]) && $tokens[$i] instanceof PHP_Token_NS_SEPARATOR) { $namespace .= '\\' . $tokens[$i + 1]; } else { break; } } return $namespace; } } class PHP_Token_NS_C extends PHP_Token { } class PHP_Token_NS_SEPARATOR extends PHP_Token { } // Tokens introduced in PHP 5.4 class PHP_Token_CALLABLE extends PHP_Token { } class PHP_Token_INSTEADOF extends PHP_Token { } class PHP_Token_TRAIT extends PHP_Token_INTERFACE { } class PHP_Token_TRAIT_C extends PHP_Token { } // Tokens introduced in PHP 5.5 class PHP_Token_FINALLY extends PHP_Token { } class PHP_Token_YIELD extends PHP_Token { } // Tokens introduced in PHP 5.6 class PHP_Token_ELLIPSIS extends PHP_Token { } class PHP_Token_POW extends PHP_Token { } class PHP_Token_POW_EQUAL extends PHP_Token { } // Tokens introduced in PHP 7.0 class PHP_Token_COALESCE extends PHP_Token { } class PHP_Token_SPACESHIP extends PHP_Token { } class PHP_Token_YIELD_FROM extends PHP_Token { } // Tokens introduced in PHP 7.4 class PHP_Token_COALESCE_EQUAL extends PHP_Token { } class PHP_Token_FN extends PHP_Token { } php-token-stream-3.1.1/src/Token/000077500000000000000000000000001354007571600165715ustar00rootroot00000000000000php-token-stream-3.1.1/src/Token/Stream.php000066400000000000000000000376001354007571600205430ustar00rootroot00000000000000 * * 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 (PHP_Token_Util::getClass($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 (PHP_Token_Util::getClass($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->getName() !== null) { 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; } } } php-token-stream-3.1.1/src/Token/Stream/000077500000000000000000000000001354007571600200245ustar00rootroot00000000000000php-token-stream-3.1.1/src/Token/Stream/CachingFactory.php000066400000000000000000000017021354007571600234210ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * A caching factory for token stream objects. */ class PHP_Token_Stream_CachingFactory { /** * @var array */ protected static $cache = []; /** * @param string $filename * * @return PHP_Token_Stream */ public static function get($filename) { if (!isset(self::$cache[$filename])) { self::$cache[$filename] = new PHP_Token_Stream($filename); } return self::$cache[$filename]; } /** * @param string $filename */ public static function clear($filename = null) { if (is_string($filename)) { unset(self::$cache[$filename]); } else { self::$cache = []; } } } php-token-stream-3.1.1/src/Token/Util.php000066400000000000000000000006751354007571600202270ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ final class PHP_Token_Util { public static function getClass($object): string { $parts = explode('\\', get_class($object)); return array_pop($parts); } }php-token-stream-3.1.1/tests/000077500000000000000000000000001354007571600160645ustar00rootroot00000000000000php-token-stream-3.1.1/tests/Token/000077500000000000000000000000001354007571600171445ustar00rootroot00000000000000php-token-stream-3.1.1/tests/Token/ClassTest.php000066400000000000000000000121021354007571600215560ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_ClassTest extends TestCase { /** * @var PHP_Token_CLASS */ private $class; /** * @var PHP_Token_FUNCTION */ private $function; protected function setUp() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php') as $token) { if ($token instanceof PHP_Token_CLASS) { $this->class = $token; } if ($token instanceof PHP_Token_FUNCTION) { $this->function = $token; break; } } } public function testGetClassKeywords() { $this->assertEquals('abstract', $this->class->getKeywords()); } public function testGetFunctionKeywords() { $this->assertEquals('abstract,static', $this->function->getKeywords()); } public function testGetFunctionVisibility() { $this->assertEquals('public', $this->function->getVisibility()); } public function testIssue19() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php') as $token) { if ($token instanceof PHP_Token_CLASS) { $this->assertFalse($token->hasInterfaces()); } } } public function testIssue30() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php'); $this->assertCount(1, $ts->getClasses()); } public function testAnonymousClassesAreHandledCorrectly() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php'); $classes = $ts->getClasses(); $this->assertEquals( [ 'class_with_method_that_declares_anonymous_class', 'AnonymousClass:9#31', 'AnonymousClass:10#55', 'AnonymousClass:11#75', 'AnonymousClass:12#91', 'AnonymousClass:13#107' ], array_keys($classes) ); } /** * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52 */ public function testAnonymousClassesAreHandledCorrectly2() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php'); $classes = $ts->getClasses(); $this->assertEquals(['Test', 'AnonymousClass:4#23'], array_keys($classes)); $this->assertEquals(['methodOne', 'methodTwo'], array_keys($classes['Test']['methods'])); $this->assertEmpty($ts->getFunctions()); } public function testImportedFunctionsAreHandledCorrectly() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php'); $this->assertEmpty($ts->getFunctions()); $this->assertCount(1, $ts->getClasses()); } /** * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/543 */ public function testClassWithMultipleAnonymousClassesAndFunctionsIsHandledCorrectly() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_multiple_anonymous_classes_and_functions.php'); $classes = $ts->getClasses(); $this->assertArrayHasKey('class_with_multiple_anonymous_classes_and_functions', $classes); $this->assertArrayHasKey('AnonymousClass:6#23', $classes); $this->assertArrayHasKey('AnonymousClass:12#53', $classes); $this->assertArrayHasKey('m', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']); $this->assertArrayHasKey('anonymousFunction:18#81', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']); $this->assertArrayHasKey('anonymousFunction:22#108', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']); } /** * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/68 */ public function testClassWithMethodNamedEmptyIsHandledCorrectly() { $classes = (new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_named_empty.php'))->getClasses(); $this->assertArrayHasKey('class_with_method_named_empty', $classes); $this->assertArrayHasKey('empty', $classes['class_with_method_named_empty']['methods']); } /** * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/424 */ public function testAnonymousFunctionDoesNotAffectStartAndEndLineOfMethod() { $classes = (new PHP_Token_Stream(TEST_FILES_PATH . 'php-code-coverage-issue-424.php'))->getClasses(); $this->assertSame(5, $classes['Example']['methods']['even']['startLine']); $this->assertSame(12, $classes['Example']['methods']['even']['endLine']); $this->assertSame(7, $classes['Example']['methods']['anonymousFunction:7#28']['startLine']); $this->assertSame(9, $classes['Example']['methods']['anonymousFunction:7#28']['endLine']); } } php-token-stream-3.1.1/tests/Token/ClosureTest.php000066400000000000000000000046221354007571600221350ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_ClosureTest extends TestCase { /** * @var PHP_Token_FUNCTION[] */ private $functions; protected function setUp() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'closure.php') as $token) { if ($token instanceof PHP_Token_FUNCTION) { $this->functions[] = $token; } } } public function testGetArguments() { $this->assertEquals(['$foo' => null, '$bar' => null], $this->functions[0]->getArguments()); $this->assertEquals(['$foo' => 'Foo', '$bar' => null], $this->functions[1]->getArguments()); $this->assertEquals(['$foo' => null, '$bar' => null, '$baz' => null], $this->functions[2]->getArguments()); $this->assertEquals(['$foo' => 'Foo', '$bar' => null, '$baz' => null], $this->functions[3]->getArguments()); $this->assertEquals([], $this->functions[4]->getArguments()); $this->assertEquals([], $this->functions[5]->getArguments()); } public function testGetName() { $this->assertEquals('anonymousFunction:2#5', $this->functions[0]->getName()); $this->assertEquals('anonymousFunction:3#27', $this->functions[1]->getName()); $this->assertEquals('anonymousFunction:4#51', $this->functions[2]->getName()); $this->assertEquals('anonymousFunction:5#71', $this->functions[3]->getName()); $this->assertEquals('anonymousFunction:6#93', $this->functions[4]->getName()); $this->assertEquals('anonymousFunction:7#106', $this->functions[5]->getName()); } public function testGetLine() { $this->assertEquals(2, $this->functions[0]->getLine()); $this->assertEquals(3, $this->functions[1]->getLine()); $this->assertEquals(4, $this->functions[2]->getLine()); $this->assertEquals(5, $this->functions[3]->getLine()); } public function testGetEndLine() { $this->assertEquals(2, $this->functions[0]->getLine()); $this->assertEquals(3, $this->functions[1]->getLine()); $this->assertEquals(4, $this->functions[2]->getLine()); $this->assertEquals(5, $this->functions[3]->getLine()); } } php-token-stream-3.1.1/tests/Token/FunctionTest.php000066400000000000000000000075361354007571600223150ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_FunctionTest extends TestCase { /** * @var PHP_Token_FUNCTION[] */ private $functions; protected function setUp() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'source.php') as $token) { if ($token instanceof PHP_Token_FUNCTION) { $this->functions[] = $token; } } } public function testGetArguments() { $this->assertEquals([], $this->functions[0]->getArguments()); $this->assertEquals( ['$baz' => 'Baz'], $this->functions[1]->getArguments() ); $this->assertEquals( ['$foobar' => 'Foobar'], $this->functions[2]->getArguments() ); $this->assertEquals( ['$barfoo' => 'Barfoo'], $this->functions[3]->getArguments() ); $this->assertEquals([], $this->functions[4]->getArguments()); $this->assertEquals(['$x' => null, '$y' => null], $this->functions[5]->getArguments()); } public function testGetName() { $this->assertEquals('foo', $this->functions[0]->getName()); $this->assertEquals('bar', $this->functions[1]->getName()); $this->assertEquals('foobar', $this->functions[2]->getName()); $this->assertEquals('barfoo', $this->functions[3]->getName()); $this->assertEquals('baz', $this->functions[4]->getName()); } public function testGetLine() { $this->assertEquals(5, $this->functions[0]->getLine()); $this->assertEquals(10, $this->functions[1]->getLine()); $this->assertEquals(17, $this->functions[2]->getLine()); $this->assertEquals(21, $this->functions[3]->getLine()); $this->assertEquals(29, $this->functions[4]->getLine()); $this->assertEquals(37, $this->functions[6]->getLine()); } public function testGetEndLine() { $this->assertEquals(5, $this->functions[0]->getEndLine()); $this->assertEquals(12, $this->functions[1]->getEndLine()); $this->assertEquals(19, $this->functions[2]->getEndLine()); $this->assertEquals(23, $this->functions[3]->getEndLine()); $this->assertEquals(31, $this->functions[4]->getEndLine()); $this->assertEquals(41, $this->functions[6]->getEndLine()); } public function testGetDocblock() { $this->assertNull($this->functions[0]->getDocblock()); $this->assertEquals( "/**\n * @param Baz \$baz\n */", $this->functions[1]->getDocblock() ); $this->assertEquals( "/**\n * @param Foobar \$foobar\n */", $this->functions[2]->getDocblock() ); $this->assertNull($this->functions[3]->getDocblock()); $this->assertNull($this->functions[4]->getDocblock()); } public function testSignature() { $tokens = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php'); $functions = $tokens->getFunctions(); $classes = $tokens->getClasses(); $interfaces = $tokens->getInterfaces(); $this->assertEquals( 'foo($a, array $b, array $c = array())', $functions['foo']['signature'] ); $this->assertEquals( 'm($a, array $b, array $c = array())', $classes['c']['methods']['m']['signature'] ); $this->assertEquals( 'm($a, array $b, array $c = array())', $classes['a']['methods']['m']['signature'] ); $this->assertEquals( 'm($a, array $b, array $c = array())', $interfaces['i']['methods']['m']['signature'] ); } } php-token-stream-3.1.1/tests/Token/IncludeTest.php000066400000000000000000000023271354007571600221040ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_IncludeTest extends TestCase { /** * @var PHP_Token_Stream */ private $ts; protected function setUp() { $this->ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source3.php'); } public function testGetIncludes() { $this->assertSame( ['test4.php', 'test3.php', 'test2.php', 'test1.php'], $this->ts->getIncludes() ); } public function testGetIncludesCategorized() { $this->assertSame( [ 'require_once' => ['test4.php'], 'require' => ['test3.php'], 'include_once' => ['test2.php'], 'include' => ['test1.php'] ], $this->ts->getIncludes(true) ); } public function testGetIncludesCategory() { $this->assertSame( ['test4.php'], $this->ts->getIncludes(true, 'require_once') ); } } php-token-stream-3.1.1/tests/Token/InterfaceTest.php000066400000000000000000000113231354007571600224150ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_InterfaceTest extends TestCase { /** * @var PHP_Token_CLASS */ private $class; /** * @var PHP_Token_INTERFACE[] */ private $interfaces; protected function setUp() { $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php'); $i = 0; foreach ($ts as $token) { if ($token instanceof PHP_Token_CLASS) { $this->class = $token; } elseif ($token instanceof PHP_Token_INTERFACE) { $this->interfaces[$i] = $token; $i++; } } } public function testGetName() { $this->assertEquals( 'iTemplate', $this->interfaces[0]->getName() ); } public function testGetParentNotExists() { $this->assertFalse( $this->interfaces[0]->getParent() ); } public function testHasParentNotExists() { $this->assertFalse( $this->interfaces[0]->hasParent() ); } public function testGetParentExists() { $this->assertEquals( 'a', $this->interfaces[2]->getParent() ); } public function testHasParentExists() { $this->assertTrue( $this->interfaces[2]->hasParent() ); } public function testGetInterfacesExists() { $this->assertEquals( ['b'], $this->class->getInterfaces() ); } public function testHasInterfacesExists() { $this->assertTrue( $this->class->hasInterfaces() ); } public function testGetPackageNamespace() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php') as $token) { if ($token instanceof PHP_Token_INTERFACE) { $package = $token->getPackage(); $this->assertSame('Foo\\Bar', $package['namespace']); } } } public function provideFilesWithClassesWithinMultipleNamespaces() { return [ [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'], [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'], ]; } /** * @dataProvider provideFilesWithClassesWithinMultipleNamespaces */ public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath) { $tokenStream = new PHP_Token_Stream($filepath); $firstClassFound = false; foreach ($tokenStream as $token) { if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) { $package = $token->getPackage(); $this->assertSame('TestClassInBar', $token->getName()); $this->assertSame('Foo\\Bar', $package['namespace']); $firstClassFound = true; continue; } // Secound class if ($token instanceof PHP_Token_INTERFACE) { $package = $token->getPackage(); $this->assertSame('TestClassInBaz', $token->getName()); $this->assertSame('Foo\\Baz', $package['namespace']); return; } } $this->fail('Searching for 2 classes failed'); } public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces() { foreach ($this->interfaces as $token) { $package = $token->getPackage(); $this->assertSame('', $package['namespace']); } } public function testGetPackageNamespaceWhenExtentingFromNamespaceClass() { $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php'); $firstClassFound = false; foreach ($tokenStream as $token) { if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) { $package = $token->getPackage(); $this->assertSame('Baz', $token->getName()); $this->assertSame('Foo\\Bar', $package['namespace']); $firstClassFound = true; continue; } if ($token instanceof PHP_Token_INTERFACE) { $package = $token->getPackage(); $this->assertSame('Extender', $token->getName()); $this->assertSame('Other\\Space', $package['namespace']); return; } } $this->fail('Searching for 2 classes failed'); } } php-token-stream-3.1.1/tests/Token/NamespaceTest.php000066400000000000000000000035431354007571600224160ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; class PHP_Token_NamespaceTest extends TestCase { public function testGetName() { $tokenStream = new PHP_Token_Stream( TEST_FILES_PATH . 'classInNamespace.php' ); foreach ($tokenStream as $token) { if ($token instanceof PHP_Token_NAMESPACE) { $this->assertSame('Foo\\Bar', $token->getName()); } } } public function testGetStartLineWithUnscopedNamespace() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php') as $token) { if ($token instanceof PHP_Token_NAMESPACE) { $this->assertSame(2, $token->getLine()); } } } public function testGetEndLineWithUnscopedNamespace() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php') as $token) { if ($token instanceof PHP_Token_NAMESPACE) { $this->assertSame(2, $token->getEndLine()); } } } public function testGetStartLineWithScopedNamespace() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php') as $token) { if ($token instanceof PHP_Token_NAMESPACE) { $this->assertSame(2, $token->getLine()); } } } public function testGetEndLineWithScopedNamespace() { foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php') as $token) { if ($token instanceof PHP_Token_NAMESPACE) { $this->assertSame(8, $token->getEndLine()); } } } } php-token-stream-3.1.1/tests/_fixture/000077500000000000000000000000001354007571600177115ustar00rootroot00000000000000php-token-stream-3.1.1/tests/_fixture/classExtendsNamespacedClass.php000066400000000000000000000001511354007571600260260ustar00rootroot00000000000000method_in_anonymous_class(); } public function methodTwo() { return false; } } php-token-stream-3.1.1/tests/_fixture/class_with_multiple_anonymous_classes_and_functions.php000066400000000000000000000007351354007571600332410ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require __DIR__ . '/../vendor/autoload.php'; define( 'TEST_FILES_PATH', __DIR__ . DIRECTORY_SEPARATOR . '_fixture' . DIRECTORY_SEPARATOR );