commitall

This commit is contained in:
Sampanna Rimal
2024-07-10 18:28:19 +05:45
parent 140abda4e6
commit 9cd05ef3cb
15723 changed files with 4818733 additions and 0 deletions

View File

@ -0,0 +1 @@
*.php diff=php

View File

@ -0,0 +1,3 @@
/.idea
/composer.lock
/vendor

View File

@ -0,0 +1,26 @@
language: php
php:
- 7.0
- 7.1
- 7.2
- master
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

View File

@ -0,0 +1,19 @@
# Change Log
All notable changes to `sebastianbergmann/php-token-stream` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
## [2.0.2] - 2017-11-27
* 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
[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

33
vendor/phpunit/php-token-stream/LICENSE vendored Normal file
View File

@ -0,0 +1,33 @@
PHP_TokenStream
Copyright (c) 2009-2017, Sebastian Bergmann <sebastian@phpunit.de>.
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.

View File

@ -0,0 +1,14 @@
[![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

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="php-token-stream" default="setup">
<target name="setup" depends="clean,composer"/>
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/vendor"/>
<delete file="${basedir}/composer.lock"/>
</target>
<target name="composer" depends="clean" description="Install dependencies with Composer">
<exec executable="composer" taskname="composer">
<env key="COMPOSER_DISABLE_XDEBUG_WARN" value="1"/>
<arg value="update"/>
<arg value="--no-interaction"/>
<arg value="--no-progress"/>
<arg value="--no-ansi"/>
<arg value="--no-suggest"/>
</exec>
</target>
</project>

View File

@ -0,0 +1,38 @@
{
"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"
},
"require": {
"php": "^7.0",
"ext-tokenizer": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.2.4"
},
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"autoload": {
"classmap": [
"src/"
]
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="tests/bootstrap.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,607 @@
<?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;
}
}
}

View File

@ -0,0 +1,46 @@
<?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 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 = [];
}
}
}

View File

@ -0,0 +1,169 @@
<?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.
*/
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()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_CLASS) {
$this->class = $token;
}
if ($token instanceof PHP_Token_FUNCTION) {
$this->function = $token;
break;
}
}
}
/**
* @covers PHP_Token_CLASS::getKeywords
*/
public function testGetClassKeywords()
{
$this->assertEquals('abstract', $this->class->getKeywords());
}
/**
* @covers PHP_Token_FUNCTION::getKeywords
*/
public function testGetFunctionKeywords()
{
$this->assertEquals('abstract,static', $this->function->getKeywords());
}
/**
* @covers PHP_Token_FUNCTION::getVisibility
*/
public function testGetFunctionVisibility()
{
$this->assertEquals('public', $this->function->getVisibility());
}
public function testIssue19()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php');
foreach ($ts 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()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_named_empty.php');
$classes = $ts->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 testSomething()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'php-code-coverage-issue-424.php');
$classes = $ts->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']);
}
}

View File

@ -0,0 +1,78 @@
<?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.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_ClosureTest extends TestCase
{
/**
* @var PHP_Token_FUNCTION[]
*/
private $functions;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'closure.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_FUNCTION) {
$this->functions[] = $token;
}
}
}
/**
* @covers PHP_Token_FUNCTION::getArguments
*/
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());
}
/**
* @covers PHP_Token_FUNCTION::getName
*/
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());
}
/**
* @covers PHP_Token::getLine
*/
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());
}
/**
* @covers PHP_TokenWithScope::getEndLine
*/
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());
}
}

View File

@ -0,0 +1,139 @@
<?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.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_FunctionTest extends TestCase
{
/**
* @var PHP_Token_FUNCTION[]
*/
private $functions;
protected function setUp()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source.php');
foreach ($ts as $token) {
if ($token instanceof PHP_Token_FUNCTION) {
$this->functions[] = $token;
}
}
}
/**
* @covers PHP_Token_FUNCTION::getArguments
*/
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());
}
/**
* @covers PHP_Token_FUNCTION::getName
*/
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());
}
/**
* @covers PHP_Token::getLine
*/
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());
}
/**
* @covers PHP_TokenWithScope::getEndLine
*/
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());
}
/**
* @covers PHP_Token_FUNCTION::getDocblock
*/
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()
{
$ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
$f = $ts->getFunctions();
$c = $ts->getClasses();
$i = $ts->getInterfaces();
$this->assertEquals(
'foo($a, array $b, array $c = array())',
$f['foo']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$c['c']['methods']['m']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$c['a']['methods']['m']['signature']
);
$this->assertEquals(
'm($a, array $b, array $c = array())',
$i['i']['methods']['m']['signature']
);
}
}

View File

@ -0,0 +1,65 @@
<?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.
*/
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');
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludes()
{
$this->assertSame(
['test4.php', 'test3.php', 'test2.php', 'test1.php'],
$this->ts->getIncludes()
);
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludesCategorized()
{
$this->assertSame(
[
'require_once' => ['test4.php'],
'require' => ['test3.php'],
'include_once' => ['test2.php'],
'include' => ['test1.php']
],
$this->ts->getIncludes(true)
);
}
/**
* @covers PHP_Token_Includes::getName
* @covers PHP_Token_Includes::getType
*/
public function testGetIncludesCategory()
{
$this->assertSame(
['test4.php'],
$this->ts->getIncludes(true, 'require_once')
);
}
}

View File

@ -0,0 +1,195 @@
<?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.
*/
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++;
}
}
}
/**
* @covers PHP_Token_INTERFACE::getName
*/
public function testGetName()
{
$this->assertEquals(
'iTemplate', $this->interfaces[0]->getName()
);
}
/**
* @covers PHP_Token_INTERFACE::getParent
*/
public function testGetParentNotExists()
{
$this->assertFalse(
$this->interfaces[0]->getParent()
);
}
/**
* @covers PHP_Token_INTERFACE::hasParent
*/
public function testHasParentNotExists()
{
$this->assertFalse(
$this->interfaces[0]->hasParent()
);
}
/**
* @covers PHP_Token_INTERFACE::getParent
*/
public function testGetParentExists()
{
$this->assertEquals(
'a', $this->interfaces[2]->getParent()
);
}
/**
* @covers PHP_Token_INTERFACE::hasParent
*/
public function testHasParentExists()
{
$this->assertTrue(
$this->interfaces[2]->hasParent()
);
}
/**
* @covers PHP_Token_INTERFACE::getInterfaces
*/
public function testGetInterfacesExists()
{
$this->assertEquals(
['b'],
$this->class->getInterfaces()
);
}
/**
* @covers PHP_Token_INTERFACE::hasInterfaces
*/
public function testHasInterfacesExists()
{
$this->assertTrue(
$this->class->hasInterfaces()
);
}
/**
* @covers PHP_Token_INTERFACE::getPackage
*/
public function testGetPackageNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream 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
* @covers PHP_Token_INTERFACE::getPackage
*/
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('Seachring for 2 classes failed');
}
public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces()
{
foreach ($this->interfaces as $token) {
$package = $token->getPackage();
$this->assertSame('', $package['namespace']);
}
}
/**
* @covers PHP_Token_INTERFACE::getPackage
*/
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');
}
}

View File

@ -0,0 +1,69 @@
<?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.
*/
use PHPUnit\Framework\TestCase;
class PHP_Token_NamespaceTest extends TestCase
{
/**
* @covers PHP_Token_NAMESPACE::getName
*/
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()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getLine());
}
}
}
public function testGetEndLineWithUnscopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getEndLine());
}
}
}
public function testGetStartLineWithScopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(2, $token->getLine());
}
}
}
public function testGetEndLineWithScopedNamespace()
{
$tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInScopedNamespace.php');
foreach ($tokenStream as $token) {
if ($token instanceof PHP_Token_NAMESPACE) {
$this->assertSame(8, $token->getEndLine());
}
}
}
}

View File

@ -0,0 +1,32 @@
<?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.
*/
use PHPUnit\Framework\TestCase;
class PHP_TokenTest extends TestCase
{
/**
* @covers PHP_Token::__construct
* @covers PHP_Token::__toString
*/
public function testToString()
{
$this->markTestIncomplete();
}
/**
* @covers PHP_Token::__construct
* @covers PHP_Token::getLine
*/
public function testGetLine()
{
$this->markTestIncomplete();
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Foo\Bar;
class Baz {}
namespace Other\Space;
class Extender extends \Foo\Bar\Baz {}

View File

@ -0,0 +1,6 @@
<?php
namespace Foo\Bar;
class TestClass
{
}

View File

@ -0,0 +1,9 @@
<?php
namespace Foo\BarScoped {
class TestClass {
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace foo;
use function bar\baz;
class Foo
{
}

View File

@ -0,0 +1,7 @@
<?php
class class_with_method_named_empty
{
public function empty(): void
{
}
}

View File

@ -0,0 +1,15 @@
<?php
interface foo {
}
class class_with_method_that_declares_anonymous_class
{
public function method()
{
$o = new class { public function foo() {} };
$o = new class{public function foo(){}};
$o = new class extends stdClass {};
$o = new class extends stdClass {};
$o = new class implements foo {};
}
}

View File

@ -0,0 +1,16 @@
<?php
class Test {
public function methodOne() {
$foo = new class {
public function method_in_anonymous_class() {
return true;
}
};
return $foo->method_in_anonymous_class();
}
public function methodTwo() {
return false;
}
}

View File

@ -0,0 +1,26 @@
<?php
class class_with_multiple_anonymous_classes_and_functions
{
public function m()
{
$c = new class {
public function n() {
return true;
}
};
$d = new class {
public function o() {
return false;
}
};
$f = function ($a, $b) {
return $a + $b;
};
$g = function ($a, $b) {
return $a - $b;
};
}
}

View File

@ -0,0 +1,7 @@
<?php
$function1 = function($foo, $bar) use ($var) {};
$function2 = function(Foo $foo, $bar) use ($var) {};
$function3 = function ($foo, $bar, $baz) {};
$function4 = function (Foo $foo, $bar, $baz) {};
$function5 = function () {};
$function6 = function() {};

View File

@ -0,0 +1,3 @@
<?php
class TestClass {
}

View File

@ -0,0 +1,8 @@
<?php
class Foo
{
public function bar()
{
return Foo::CLASS;
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Foo\Bar;
class TestClassInBar
{
}
namespace Foo\Baz;
class TestClassInBaz
{
}

View File

@ -0,0 +1,14 @@
<?php
namespace Foo\Bar
{
class TestClassInBar
{
}
}
namespace Foo\Baz
{
class TestClassInBaz
{
}
}

View File

@ -0,0 +1,13 @@
<?php
class Example
{
public function even($numbers)
{
$numbers = array_filter($numbers, function($number) {
return $number % 2 === 0;
});
return array_merge($numbers);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Some comment
*/
class Foo{function foo(){}
/**
* @param Baz $baz
*/
public function bar(Baz $baz)
{
}
/**
* @param Foobar $foobar
*/
static public function foobar(Foobar $foobar)
{
}
public function barfoo(Barfoo $barfoo)
{
}
/**
* This docblock does not belong to the baz function
*/
public function baz()
{
}
public function blaz($x, $y)
{
}
}

View File

@ -0,0 +1,6 @@
<?php
// short desc
abstract class A {
/* abst meth: */
public static abstract function method();
}

View File

@ -0,0 +1,14 @@
<?php
// This file is example#1
// from http://www.php.net/manual/en/function.get-included-files.php
include 'test1.php';
include_once 'test2.php';
require 'test3.php';
require_once 'test4.php';
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo "$filename\n";
}

View File

@ -0,0 +1,30 @@
<?php
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function
getHtml($template);
}
interface a
{
public function foo();
}
interface b extends a
{
public function baz(Baz $baz);
}
// short desc for class that implement a unique interface
class c implements b
{
public function foo()
{
}
public function baz(Baz $baz)
{
}
}

View File

@ -0,0 +1,5 @@
<?php
function foo($a, array $b, array $c = array()) {}
interface i { public function m($a, array $b, array $c = array()); }
abstract class a { abstract public function m($a, array $b, array $c = array()); }
class c { public function m($a, array $b, array $c = array()) {} }

View File

@ -0,0 +1,15 @@
<?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.
*/
require __DIR__ . '/../vendor/autoload.php';
define(
'TEST_FILES_PATH',
__DIR__ . DIRECTORY_SEPARATOR . '_fixture' . DIRECTORY_SEPARATOR
);