1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 |
- <?php
- /*
- * This file is part of the PHP CS utility.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace Symfony\CS\Tests\Tokenizer;
- use Symfony\CS\Tokenizer\Token;
- use Symfony\CS\Tokenizer\Tokens;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- * @author Max Voloshin <voloshin.dp@gmail.com>
- * @author Gregor Harlan <gharlan@web.de>
- */
- class TokensTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @param Token[]|null $expected
- * @param Token[]|null $input
- */
- private function assertEqualsTokensArray(array $expected = null, array $input = null)
- {
- if (null === $expected) {
- $this->assertNull($input);
- return;
- }
- $this->assertSame(array_keys($expected), array_keys($input), 'Both arrays need to have same keys.');
- foreach ($expected as $index => $expectedToken) {
- $this->assertTrue(
- $expectedToken->equals($input[$index]),
- sprintf('The token at index %d should be %s, got %s', $index, $expectedToken->toJson(), $input[$index]->toJson())
- );
- }
- }
- public function testGetClassyElements()
- {
- $source = <<<'PHP'
- <?php
- class Foo
- {
- public $prop0;
- protected $prop1;
- private $prop2 = 1;
- var $prop3 = array(1,2,3);
- public function bar4()
- {
- $a = 5;
- return " ({$a})";
- }
- public function bar5($data)
- {
- $message = $data;
- $example = function ($arg) use ($message) {
- echo $arg . ' ' . $message;
- };
- $example('hello');
- }
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- $elements = array_values($tokens->getClassyElements());
- $this->assertCount(6, $elements);
- $this->assertSame('property', $elements[0]['type']);
- $this->assertSame('property', $elements[1]['type']);
- $this->assertSame('property', $elements[2]['type']);
- $this->assertSame('property', $elements[3]['type']);
- $this->assertSame('method', $elements[4]['type']);
- $this->assertSame('method', $elements[5]['type']);
- }
- public function testReadFromCacheAfterClearing()
- {
- $code = '<?php echo 1;';
- $tokens = Tokens::fromCode($code);
- $countBefore = $tokens->count();
- for ($i = 0; $i < $countBefore; ++$i) {
- $tokens[$i]->clear();
- }
- $tokens = Tokens::fromCode($code);
- $this->assertSame($countBefore, $tokens->count());
- }
- /**
- * @dataProvider provideIsLambdaCases
- */
- public function testIsLambda($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isLambda($index));
- }
- }
- public function provideIsLambdaCases()
- {
- return array(
- array(
- '<?php function foo () {}',
- array(1 => false),
- ),
- array(
- '<?php function /** foo */ foo () {}',
- array(1 => false),
- ),
- array(
- '<?php $foo = function () {}',
- array(5 => true),
- ),
- array(
- '<?php $foo = function /** foo */ () {}',
- array(5 => true),
- ),
- array(
- '<?php
- preg_replace_callback(
- "/(^|[a-z])/",
- function (array $matches) {
- return "a";
- },
- $string
- );',
- array(7 => true),
- ),
- array(
- '<?php $foo = function &() {}',
- array(5 => true),
- ),
- );
- }
- /**
- * @dataProvider provideIsShortArrayCases
- */
- public function testIsShortArray($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isShortArray($index));
- }
- }
- public function provideIsShortArrayCases()
- {
- return array(
- array(
- '<?php [];',
- array(1 => true),
- ),
- array(
- '<?php [1, "foo"];',
- array(1 => true),
- ),
- array(
- '<?php [[]];',
- array(1 => true, 2 => true),
- ),
- array(
- '<?php ["foo", ["bar", "baz"]];',
- array(1 => true, 5 => true),
- ),
- array(
- '<?php (array) [1, 2];',
- array(3 => true),
- ),
- array(
- '<?php [1,2][$x];',
- array(1 => true, 6 => false),
- ),
- array(
- '<?php array();',
- array(1 => false),
- ),
- array(
- '<?php $x[] = 1;',
- array(2 => false),
- ),
- array(
- '<?php $x[1];',
- array(2 => false),
- ),
- array(
- '<?php $x [ 1 ];',
- array(3 => false),
- ),
- array(
- '<?php ${"x"}[1];',
- array(5 => false),
- ),
- array(
- '<?php FOO[1];',
- array(2 => false),
- ),
- array(
- '<?php array("foo")[1];',
- array(5 => false),
- ),
- array(
- '<?php foo()[1];',
- array(4 => false),
- ),
- array(
- '<?php "foo"[1];',
- array(2 => false),
- ),
- array(
- '<?php "foo$bar"[1];',
- array(5 => false),
- ),
- );
- }
- /**
- * @dataProvider provideIsUnarySuccessorOperator
- */
- public function testIsUnarySuccessorOperator($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isUnarySuccessorOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnaryPredecessorOperator($index));
- $this->assertFalse($tokens->isBinaryOperator($index));
- }
- }
- }
- public function provideIsUnarySuccessorOperator()
- {
- return array(
- array(
- '<?php $a++;',
- array(2 => true),
- ),
- array(
- '<?php $a--',
- array(2 => true),
- ),
- array(
- '<?php $a ++;',
- array(3 => true),
- ),
- array(
- '<?php $a++ + 1;',
- array(2 => true, 4 => false),
- ),
- array(
- '<?php ${"a"}++',
- array(5 => true),
- ),
- array(
- '<?php $foo->bar++',
- array(4 => true),
- ),
- array(
- '<?php $foo->{"bar"}++',
- array(6 => true),
- ),
- array(
- '<?php $a["foo"]++',
- array(5 => true),
- ),
- );
- }
- /**
- * @dataProvider provideIsUnaryPredecessorOperator
- */
- public function testIsUnaryPredecessorOperator($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isUnaryPredecessorOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnarySuccessorOperator($index));
- $this->assertFalse($tokens->isBinaryOperator($index));
- }
- }
- }
- public function provideIsUnaryPredecessorOperator()
- {
- return array(
- array(
- '<?php ++$a;',
- array(1 => true),
- ),
- array(
- '<?php --$a',
- array(1 => true),
- ),
- array(
- '<?php -- $a;',
- array(1 => true),
- ),
- array(
- '<?php $a + ++$b;',
- array(3 => false, 5 => true),
- ),
- array(
- '<?php !!$a;',
- array(1 => true, 2 => true),
- ),
- array(
- '<?php $a = &$b;',
- array(5 => true),
- ),
- array(
- '<?php function &foo() {}',
- array(3 => true),
- ),
- array(
- '<?php @foo();',
- array(1 => true),
- ),
- array(
- '<?php foo(+ $a, -$b);',
- array(3 => true, 8 => true),
- ),
- array(
- '<?php function foo(&$a, array &$b, Bar &$c) {}',
- array(5 => true, 11 => true, 17 => true),
- ),
- );
- }
- /**
- * @dataProvider provideIsUnaryPredecessorOperator56
- * @requires PHP 5.6
- */
- public function testIsUnaryPredecessorOperator56($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isUnaryPredecessorOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnarySuccessorOperator($index));
- $this->assertFalse($tokens->isBinaryOperator($index));
- }
- }
- }
- public function provideIsUnaryPredecessorOperator56()
- {
- return array(
- array(
- '<?php function foo($a, ...$b);',
- array(8 => true),
- ),
- array(
- '<?php function foo(&...$b);',
- array(5 => true, 6 => true),
- ),
- array(
- '<?php function foo(array ...$b);',
- array(7 => true),
- ),
- array(
- '<?php foo(...$a);',
- array(3 => true),
- ),
- array(
- '<?php foo($a, ...$b);',
- array(6 => true),
- ),
- );
- }
- /**
- * @dataProvider provideIsBinaryOperator
- */
- public function testIsBinaryOperator($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isBinaryOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnarySuccessorOperator($index));
- $this->assertFalse($tokens->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator()
- {
- $cases = array(
- array(
- '<?php $a + $b;',
- array(3 => true),
- ),
- array(
- '<?php 1 + $b;',
- array(3 => true),
- ),
- array(
- '<?php 0.2 + $b;',
- array(3 => true),
- ),
- array(
- '<?php $a[1] + $b;',
- array(6 => true),
- ),
- array(
- '<?php FOO + $b;',
- array(3 => true),
- ),
- array(
- '<?php foo() + $b;',
- array(5 => true),
- ),
- array(
- '<?php ${"foo"} + $b;',
- array(6 => true),
- ),
- array(
- '<?php $a+$b;',
- array(2 => true),
- ),
- array(
- '<?php $a /* foo */ + /* bar */ $b;',
- array(5 => true),
- ),
- array(
- '<?php $a =
- $b;',
- array(3 => true),
- ),
- array(
- '<?php $a
- = $b;',
- array(3 => true),
- ),
- array(
- '<?php $a = array("b" => "c", );',
- array(3 => true, 9 => true, 12 => false),
- ),
- array(
- '<?php $a * -$b;',
- array(3 => true, 5 => false),
- ),
- array(
- '<?php $a = -2 / +5;',
- array(3 => true, 5 => false, 8 => true, 10 => false),
- ),
- array(
- '<?php $a = &$b;',
- array(3 => true, 5 => false),
- ),
- array(
- '<?php $a++ + $b;',
- array(2 => false, 4 => true),
- ),
- array(
- '<?php $a = FOO & $bar;',
- array(7 => true),
- ),
- array(
- '<?php __LINE__ - 1;',
- array(3 => true),
- ),
- array(
- '<?php `echo 1` + 1;',
- array(5 => true),
- ),
- );
- $operators = array(
- '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
- '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
- );
- foreach ($operators as $operator) {
- $cases[] = array(
- '<?php $a '.$operator.' $b;',
- array(3 => true),
- );
- }
- return $cases;
- }
- /**
- * @dataProvider provideIsBinaryOperator56
- * @requires PHP 5.6
- */
- public function testIsBinaryOperator56($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isBinaryOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnarySuccessorOperator($index));
- $this->assertFalse($tokens->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator56()
- {
- return array(
- array(
- '<?php $a ** $b;',
- array(3 => true),
- ),
- array(
- '<?php $a **= $b;',
- array(3 => true),
- ),
- );
- }
- /**
- * @dataProvider provideIsBinaryOperator70
- * @requires PHP 7.0
- */
- public function testIsBinaryOperator70($source, array $expected)
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expected as $index => $expectedValue) {
- $this->assertSame($expectedValue, $tokens->isBinaryOperator($index));
- if ($expectedValue) {
- $this->assertFalse($tokens->isUnarySuccessorOperator($index));
- $this->assertFalse($tokens->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator70()
- {
- return array(
- array(
- '<?php $a <=> $b;',
- array(3 => true),
- ),
- array(
- '<?php $a ?? $b;',
- array(3 => true),
- ),
- );
- }
- /**
- * @dataProvider provideFindSequence
- */
- public function testFindSequence($source, $expected, array $params)
- {
- $tokens = Tokens::fromCode($source);
- $this->assertEqualsTokensArray($expected, call_user_func_array(array($tokens, 'findSequence'), $params));
- }
- public function provideFindSequence()
- {
- return array(
- array(
- '<?php $x = 1;',
- null,
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$y'),
- )),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$x'),
- )),
- ),
- array(
- '<?php $x = 1;',
- array(
- 3 => new Token('='),
- 5 => new Token(array(T_LNUMBER, '1', 1)),
- 6 => new Token(';'),
- ),
- array(array(
- '=',
- array(T_LNUMBER, '1'),
- ';',
- )),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$x'),
- ), 0),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$x'),
- ), 1),
- ),
- array(
- '<?php $x = 1;',
- array(
- 3 => new Token('='),
- 5 => new Token(array(T_LNUMBER, '1', 1)),
- 6 => new Token(';'),
- ),
- array(array(
- '=',
- array(T_LNUMBER, '1'),
- ';',
- ), 3, 6),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- '=',
- array(T_LNUMBER, '1'),
- ';',
- ), 4, 6),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- '=',
- array(T_LNUMBER, '1'),
- ';',
- ), 3, 5),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$x'),
- ), 0, 1, true),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, true),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, array(true, true)),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, false),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, array(true, false)),
- ),
- array(
- '<?php $x = 1;',
- array(
- 0 => new Token(array(T_OPEN_TAG, '<?php ', 1)),
- 1 => new Token(array(T_VARIABLE, '$x', 1)),
- ),
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, array(1 => false)),
- ),
- array(
- '<?php $x = 1;',
- null,
- array(array(
- array(T_OPEN_TAG),
- array(T_VARIABLE, '$X'),
- ), 0, 1, array(2 => false)),
- ),
- );
- }
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider provideFindSequenceExceptions
- */
- public function testFindSequenceException($message, $sequence)
- {
- $tokens = Tokens::fromCode('<?php $x = 1;');
- try {
- $tokens->findSequence($sequence);
- } catch (\InvalidArgumentException $e) {
- $this->assertSame($message, $e->getMessage());
- throw $e;
- }
- }
- public function provideFindSequenceExceptions()
- {
- $emptyToken = new Token('!');
- $emptyToken->clear();
- return array(
- array('Invalid sequence', array()),
- array('Non-meaningful token at position: 0', array(
- array(T_WHITESPACE, ' '),
- )),
- array('Non-meaningful token at position: 1', array(
- '{', array(T_COMMENT, '// Foo'), '}',
- )),
- array('Non-meaningful token at position: 2', array(
- '{', '!', $emptyToken, '}',
- )),
- );
- }
- public function testClearRange()
- {
- $source = <<<'PHP'
- <?php
- class FooBar
- {
- public function foo()
- {
- return 'bar';
- }
- public function bar()
- {
- return 'foo';
- }
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- $publicIndexes = array_keys($tokens->findGivenKind(T_PUBLIC));
- $fooIndex = $publicIndexes[0];
- $barIndex = $publicIndexes[1];
- $tokens->clearRange($fooIndex, $barIndex - 1);
- $newPublicIndexes = array_keys($tokens->findGivenKind(T_PUBLIC));
- $this->assertSame($barIndex, reset($newPublicIndexes));
- for ($i = $fooIndex; $i < $barIndex; ++$i) {
- $this->assertTrue($tokens[$i]->isWhitespace());
- }
- }
- /**
- * @dataProvider provideMonolithicPhpDetection
- *
- * @param string $source
- * @param bool $monolitic
- */
- public function testMonolithicPhpDetection($source, $monolitic)
- {
- $tokens = Tokens::fromCode($source);
- $this->assertSame($monolitic, $tokens->isMonolithicPhp());
- }
- public function provideMonolithicPhpDetection()
- {
- return array(
- array("<?php\n", true),
- array("<?php\n?>", true),
- array('', false),
- array(' ', false),
- array("#!/usr/bin/env php\n<?php\n", false),
- array(" <?php\n", false),
- array("<?php\n?> ", false),
- array("<?php\n?><?php\n", false),
- );
- }
- /**
- * @dataProvider provideShortOpenTagMonolithicPhpDetection
- *
- * @param string $source
- * @param bool $monolitic
- */
- public function testShortOpenTagMonolithicPhpDetection($source, $monolitic)
- {
- /*
- * short_open_tag setting is ignored by HHVM
- * @see https://github.com/facebook/hhvm/issues/4758
- */
- if (!ini_get('short_open_tag') && !defined('HHVM_VERSION')) {
- // Short open tag is parsed as T_INLINE_HTML
- $monolitic = false;
- }
- $tokens = Tokens::fromCode($source);
- $this->assertSame($monolitic, $tokens->isMonolithicPhp());
- }
- public function provideShortOpenTagMonolithicPhpDetection()
- {
- return array(
- array("<?\n", true),
- array("<?\n?>", true),
- array(" <?\n", false),
- array("<?\n?> ", false),
- array("<?\n?><?\n", false),
- array("<?\n?><?php\n", false),
- array("<?\n?><?=' ';\n", false),
- array("<?php\n?><?\n", false),
- array("<?=' '\n?><?\n", false),
- );
- }
- /**
- * @dataProvider provideShortOpenTagEchoMonolithicPhpDetection
- *
- * @param string $source
- * @param bool $monolitic
- */
- public function testShortOpenTagEchoMonolithicPhpDetection($source, $monolitic)
- {
- /*
- * short_open_tag setting is ignored by HHVM
- * @see https://github.com/facebook/hhvm/issues/4758
- */
- if (!ini_get('short_open_tag') && 50400 > PHP_VERSION_ID && !defined('HHVM_VERSION')) {
- // Short open tag echo is parsed as T_INLINE_HTML
- $monolitic = false;
- }
- $tokens = Tokens::fromCode($source);
- $this->assertSame($monolitic, $tokens->isMonolithicPhp());
- }
- public function provideShortOpenTagEchoMonolithicPhpDetection()
- {
- return array(
- array("<?=' ';\n", true),
- array("<?=' '?>", true),
- array(" <?=' ';\n", false),
- array("<?=' '?> ", false),
- array("<?php\n?><?=' ';\n", false),
- array("<?=' '\n?><?php\n", false),
- array("<?=' '\n?><?=' ';\n", false),
- );
- }
- /**
- * @dataProvider provideIsArray
- * @requires PHP 5.4
- */
- public function testIsArray($source, $tokenIndex, $isMultilineArray = false, $isShortArray = false)
- {
- $tokens = Tokens::fromCode($source);
- $this->assertTrue($tokens->isArray($tokenIndex), 'Expected to be an array.');
- $this->assertSame($isMultilineArray, $tokens->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultilineArray ? '' : 'not '));
- $this->assertSame($isShortArray, $tokens->isShortArray($tokenIndex), sprintf('Expected %sto be a short array', $isShortArray ? '' : 'not '));
- }
- public function provideIsArray()
- {
- $cases = array(
- array(
- '<?php
- array("a" => 1);
- ',
- 2,
- ),
- array(
- // short array PHP 5.4 single line
- '<?php
- ["a" => 2];
- ',
- 2, false, true,
- ),
- array(
- '<?php
- array(
- "a" => 3
- );
- ',
- 2, true,
- ),
- array(
- // short array PHP 5.4 multi line
- '<?php
- [
- "a" => 4
- ];
- ',
- 2, true, true,
- ),
- array(
- '<?php
- array(
- "a" => array(5, 6, 7),
- 8 => new \Exception("Ellow")
- );
- ',
- 2, true,
- ),
- array(
- // mix short array syntax
- '<?php
- array(
- "a" => [9, 10, 11],
- 12 => new \Exception("Ellow")
- );
- ',
- 2, true,
- ),
- // Windows/Max EOL testing
- array(
- "<?php\r\narray('a' => 13);\r\n",
- 1,
- ),
- array(
- "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
- 2, true,
- ),
- );
- return $cases;
- }
- /**
- * @dataProvider provideArrayExceptions
- */
- public function testIsNotArray($source, $tokenIndex)
- {
- $tokens = Tokens::fromCode($source);
- $this->assertFalse($tokens->isArray($tokenIndex));
- }
- /**
- * @dataProvider provideArrayExceptions
- */
- public function testIsNotShortArray($source, $tokenIndex)
- {
- $tokens = Tokens::fromCode($source);
- $this->assertFalse($tokens->isShortArray($tokenIndex));
- }
- /**
- * @expectedException \InvalidArgumentException
- * @dataProvider provideArrayExceptions
- */
- public function testIsMultiLineArrayException($source, $tokenIndex)
- {
- $tokens = Tokens::fromCode($source);
- $tokens->isArrayMultiLine($tokenIndex);
- }
- public function provideArrayExceptions()
- {
- $cases = array(
- array('<?php $a;', 1),
- array("<?php\n \$a = (0+1); // [0,1]", 4),
- array('<?php $text = "foo $bbb[0] bar";', 8),
- array('<?php $text = "foo ${aaa[123]} bar";', 9),
- );
- return $cases;
- }
- public function testFindGivenKind()
- {
- $source = <<<'PHP'
- <?php
- class FooBar
- {
- public function foo()
- {
- return 'bar';
- }
- public function bar()
- {
- return 'foo';
- }
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- /** @var Token[] $found */
- $found = $tokens->findGivenKind(T_CLASS);
- $this->assertTrue(is_array($found));
- $this->assertCount(1, $found);
- $this->assertArrayHasKey(1, $found);
- $this->assertSame(T_CLASS, $found[1]->getId());
- /** @var array $found */
- $found = $tokens->findGivenKind(array(T_CLASS, T_FUNCTION));
- $this->assertCount(2, $found);
- $this->assertArrayHasKey(T_CLASS, $found);
- $this->assertTrue(is_array($found[T_CLASS]));
- $this->assertCount(1, $found[T_CLASS]);
- $this->assertArrayHasKey(1, $found[T_CLASS]);
- $this->assertSame(T_CLASS, $found[T_CLASS][1]->getId());
- $this->assertArrayHasKey(T_FUNCTION, $found);
- $this->assertTrue(is_array($found[T_FUNCTION]));
- $this->assertCount(2, $found[T_FUNCTION]);
- $this->assertArrayHasKey(9, $found[T_FUNCTION]);
- $this->assertSame(T_FUNCTION, $found[T_FUNCTION][9]->getId());
- $this->assertArrayHasKey(26, $found[T_FUNCTION]);
- $this->assertSame(T_FUNCTION, $found[T_FUNCTION][26]->getId());
- // test offset and limits of the search
- $found = $tokens->findGivenKind(array(T_CLASS, T_FUNCTION), 10);
- $this->assertCount(0, $found[T_CLASS]);
- $this->assertCount(1, $found[T_FUNCTION]);
- $this->assertArrayHasKey(26, $found[T_FUNCTION]);
- $found = $tokens->findGivenKind(array(T_CLASS, T_FUNCTION), 2, 10);
- $this->assertCount(0, $found[T_CLASS]);
- $this->assertCount(1, $found[T_FUNCTION]);
- $this->assertArrayHasKey(9, $found[T_FUNCTION]);
- }
- public function testIsMethodNameIsMagic()
- {
- $this->assertTrue(Tokens::isMethodNameIsMagic('__construct'));
- $this->assertFalse(Tokens::isMethodNameIsMagic('testIsMethodNameIsMagic'));
- }
- }
|