First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,64 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\AbstractClassPass;
class AbstractClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new AbstractClassPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('class A { abstract function a(); }'),
array('abstract class B { abstract function b() {} }'),
array('abstract class B { abstract function b() { echo "yep"; } }'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('abstract class C { function c() {} }'),
array('abstract class D { abstract function d(); }'),
);
}
}

View file

@ -0,0 +1,65 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\AssignThisVariablePass;
class AssignThisVariablePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new AssignThisVariablePass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('$this = 3'),
array('strtolower($this = "this")'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('$this'),
array('$a = $this'),
array('$a = "this"; $$a = 3'),
array('$$this = "b"'),
);
}
}

View file

@ -0,0 +1,76 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\CallTimePassByReferencePass;
class CallTimePassByReferencePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new CallTimePassByReferencePass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
if (version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped();
}
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('f(&$arg)'),
array('$object->method($first, &$arg)'),
array('$closure($first, &$arg, $last)'),
array('A::b(&$arg)'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
$data = array(
array('array(&$var)'),
array('$a = &$b'),
array('f(array(&$b))'),
);
if (version_compare(PHP_VERSION, '5.4', '<')) {
$data = array_merge($data, $this->invalidStatements());
}
return $data;
}
}

View file

@ -0,0 +1,104 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\CalledClassPass;
class CalledClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new CalledClassPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\ErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('get_class()'),
array('get_class(null)'),
array('get_called_class()'),
array('get_called_class(null)'),
array('function foo() { return get_class(); }'),
array('function foo() { return get_class(null); }'),
array('function foo() { return get_called_class(); }'),
array('function foo() { return get_called_class(null); }'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('get_class($foo)'),
array('get_class(bar())'),
array('get_called_class($foo)'),
array('get_called_class(bar())'),
array('function foo($bar) { return get_class($bar); }'),
array('function foo($bar) { return get_called_class($bar); }'),
array('class Foo { function bar() { return get_class(); } }'),
array('class Foo { function bar() { return get_class(null); } }'),
array('class Foo { function bar() { return get_called_class(); } }'),
array('class Foo { function bar() { return get_called_class(null); } }'),
array('$foo = function () {}; $foo()'),
);
}
/**
* @dataProvider validTraitStatements
*/
public function testProcessTraitStatementPasses($code)
{
if (version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped();
}
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validTraitStatements()
{
return array(
array('trait Foo { function bar() { return get_class(); } }'),
array('trait Foo { function bar() { return get_class(null); } }'),
array('trait Foo { function bar() { return get_called_class(); } }'),
array('trait Foo { function bar() { return get_called_class(null); } }'),
);
}
}

View file

@ -0,0 +1,97 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use PhpParser\PrettyPrinter\Standard as Printer;
use Psy\CodeCleaner\CodeCleanerPass;
use Psy\Exception\ParseErrorException;
use Psy\ParserFactory;
class CodeCleanerTestCase extends \PHPUnit\Framework\TestCase
{
protected $pass;
protected $traverser;
private $parser;
private $printer;
protected function setPass(CodeCleanerPass $pass)
{
$this->pass = $pass;
if (!isset($this->traverser)) {
$this->traverser = new NodeTraverser();
}
$this->traverser->addVisitor($this->pass);
}
protected function parse($code, $prefix = '<?php ')
{
$code = $prefix . $code;
try {
return $this->getParser()->parse($code);
} catch (\PhpParser\Error $e) {
if (!$this->parseErrorIsEOF($e)) {
throw ParseErrorException::fromParseError($e);
}
try {
// Unexpected EOF, try again with an implicit semicolon
return $this->getParser()->parse($code . ';');
} catch (\PhpParser\Error $e) {
return false;
}
}
}
protected function traverse(array $stmts)
{
return $this->traverser->traverse($stmts);
}
protected function prettyPrint(array $stmts)
{
return $this->getPrinter()->prettyPrint($stmts);
}
protected function assertProcessesAs($from, $to)
{
$stmts = $this->parse($from);
$stmts = $this->traverse($stmts);
$this->assertEquals($to, $this->prettyPrint($stmts));
}
private function getParser()
{
if (!isset($this->parser)) {
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->createParser();
}
return $this->parser;
}
private function getPrinter()
{
if (!isset($this->printer)) {
$this->printer = new Printer();
}
return $this->printer;
}
private function parseErrorIsEOF(\PhpParser\Error $e)
{
$msg = $e->getRawMessage();
return ($msg === 'Unexpected token EOF') || (strpos($msg, 'Syntax error, unexpected EOF') !== false);
}
}

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\ExitPass;
class ExitPassTest extends CodeCleanerTestCase
{
/**
* @var string
*/
private $expectedExceptionString = '\\Psy\\Exception\\BreakException::exitShell()';
public function setUp()
{
$this->setPass(new ExitPass());
}
/**
* @dataProvider dataProviderExitStatement
*/
public function testExitStatement($from, $to)
{
$this->assertProcessesAs($from, $to);
}
/**
* Data provider for testExitStatement.
*
* @return array
*/
public function dataProviderExitStatement()
{
return array(
array('exit;', "{$this->expectedExceptionString};"),
array('exit();', "{$this->expectedExceptionString};"),
array('die;', "{$this->expectedExceptionString};"),
array('exit(die(die));', "{$this->expectedExceptionString};"),
array('if (true) { exit; }', "if (true) {\n {$this->expectedExceptionString};\n}"),
array('if (false) { exit; }', "if (false) {\n {$this->expectedExceptionString};\n}"),
array('1 and exit();', "1 and {$this->expectedExceptionString};"),
array('foo() or die', "foo() or {$this->expectedExceptionString};"),
array('exit and 1;', "{$this->expectedExceptionString} and 1;"),
array('if (exit) { echo $wat; }', "if ({$this->expectedExceptionString}) {\n echo \$wat;\n}"),
array('exit or die;', "{$this->expectedExceptionString} or {$this->expectedExceptionString};"),
array('switch (die) { }', "switch ({$this->expectedExceptionString}) {\n}"),
array('for ($i = 1; $i < 10; die) {}', "for (\$i = 1; \$i < 10; {$this->expectedExceptionString}) {\n}"),
);
}
}

View file

@ -0,0 +1,72 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\FinalClassPass;
class FinalClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new FinalClassPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
$stmts = array(
array('final class A {} class B extends A {}'),
array('class A {} final class B extends A {} class C extends B {}'),
// array('namespace A { final class B {} } namespace C { class D extends \\A\\B {} }'),
);
if (!defined('HHVM_VERSION')) {
// For some reason Closure isn't final in HHVM?
$stmts[] = array('class A extends \\Closure {}');
}
return $stmts;
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('class A extends \\stdClass {}'),
array('final class A extends \\stdClass {}'),
array('class A {} class B extends A {}'),
);
}
}

View file

@ -0,0 +1,20 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner\Fixtures;
class ClassWithCallStatic
{
public static function __callStatic($name, $arguments)
{
// wheee!
}
}

View file

@ -0,0 +1,20 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner\Fixtures;
class ClassWithStatic
{
public static function doStuff()
{
// Don't actually do stuff.
}
}

View file

@ -0,0 +1,67 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\FunctionContextPass;
class FunctionContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new FunctionContextPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('function foo() { yield; }'),
array('if (function(){ yield; })'),
);
}
/**
* @dataProvider invalidYieldStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testInvalidYield($code)
{
if (version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped();
}
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidYieldStatements()
{
return array(
array('yield'),
array('if (yield)'),
);
}
}

View file

@ -0,0 +1,79 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\FunctionReturnInWriteContextPass;
use Psy\Exception\FatalErrorException;
class FunctionReturnInWriteContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new FunctionReturnInWriteContextPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
* @expectedExceptionMessage Can't use function return value in write context
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('f(&g())'),
array('array(& $object->method())'),
array('$a->method(& $closure())'),
array('array(& A::b())'),
array('f() = 5'),
array('unset(h())'),
);
}
public function testIsset()
{
try {
$this->traverser->traverse($this->parse('isset(strtolower("A"))'));
$this->fail();
} catch (FatalErrorException $e) {
if (version_compare(PHP_VERSION, '5.5', '>=')) {
$this->assertContains(
'Cannot use isset() on the result of a function call (you can use "null !== func()" instead)',
$e->getMessage()
);
} else {
$this->assertContains("Can't use function return value in write context", $e->getMessage());
}
}
}
/**
* @expectedException \Psy\Exception\FatalErrorException
* @expectedExceptionMessage Can't use function return value in write context
*/
public function testEmpty()
{
if (version_compare(PHP_VERSION, '5.5', '>=')) {
$this->markTestSkipped();
}
$this->traverser->traverse($this->parse('empty(strtolower("A"))'));
}
}

View file

@ -0,0 +1,96 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\ImplicitReturnPass;
class ImplicitReturnPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ImplicitReturnPass());
}
/**
* @dataProvider implicitReturns
*/
public function testProcess($from, $to)
{
$this->assertProcessesAs($from, $to);
}
public function implicitReturns()
{
$values = array(
array('4', 'return 4;'),
array('foo()', 'return foo();'),
array('return 1', 'return 1;'),
);
$from = 'if (true) { 1; } elseif (true) { 2; } else { 3; }';
$to = <<<'EOS'
if (true) {
return 1;
} elseif (true) {
return 2;
} else {
return 3;
}
return new \Psy\CodeCleaner\NoReturnValue();
EOS;
$values[] = array($from, $to);
$from = 'class A {}';
$to = <<<'EOS'
class A
{
}
return new \Psy\CodeCleaner\NoReturnValue();
EOS;
$values[] = array($from, $to);
$from = <<<'EOS'
switch (false) {
case 0:
0;
case 1:
1;
break;
case 2:
2;
return;
}
EOS;
$to = <<<'EOS'
switch (false) {
case 0:
0;
case 1:
return 1;
break;
case 2:
2;
return;
}
return new \Psy\CodeCleaner\NoReturnValue();
EOS;
$values[] = array($from, $to);
if (version_compare(PHP_VERSION, '5.4', '<')) {
$values[] = array('exit()', 'die;');
} else {
$values[] = array('exit()', 'exit;');
}
return $values;
}
}

View file

@ -0,0 +1,76 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\InstanceOfPass;
class InstanceOfPassTest extends CodeCleanerTestCase
{
protected function setUp()
{
$this->setPass(new InstanceOfPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('null instanceof stdClass'),
array('true instanceof stdClass'),
array('9 instanceof stdClass'),
array('1.0 instanceof stdClass'),
array('"foo" instanceof stdClass'),
array('__DIR__ instanceof stdClass'),
array('PHP_SAPI instanceof stdClass'),
array('1+1 instanceof stdClass'),
array('true && false instanceof stdClass'),
array('"a"."b" instanceof stdClass'),
array('!5 instanceof stdClass'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
$data = array(
array('$a instanceof stdClass'),
array('strtolower("foo") instanceof stdClass'),
array('array(1) instanceof stdClass'),
array('(string) "foo" instanceof stdClass'),
array('(1+1) instanceof stdClass'),
array('"foo ${foo} $bar" instanceof stdClass'),
array('DateTime::ISO8601 instanceof stdClass'),
);
return $data;
}
}

View file

@ -0,0 +1,75 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\LeavePsyshAlonePass;
class LeavePsyshAlonePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new LeavePsyshAlonePass());
}
public function testPassesInlineHtmlThroughJustFine()
{
$inline = $this->parse('not php at all!', '');
$this->traverse($inline);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('array_merge()'),
array('__psysh__()'),
array('$this'),
array('$psysh'),
array('$__psysh'),
array('$banana'),
);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\RuntimeException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('$__psysh__'),
array('var_dump($__psysh__)'),
array('$__psysh__ = "your mom"'),
array('$__psysh__->fakeFunctionCall()'),
);
}
}

View file

@ -0,0 +1,80 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\LegacyEmptyPass;
class LegacyEmptyPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new LegacyEmptyPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\ParseErrorException
*/
public function testProcessInvalidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
if (version_compare(PHP_VERSION, '5.5', '>=')) {
return array(
array('empty()'),
);
}
return array(
array('empty()'),
array('empty(null)'),
array('empty(PHP_EOL)'),
array('empty("wat")'),
array('empty(1.1)'),
array('empty(Foo::$bar)'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
if (version_compare(PHP_VERSION, '5.5', '<')) {
return array(
array('empty($foo)'),
);
}
return array(
array('empty($foo)'),
array('empty(null)'),
array('empty(PHP_EOL)'),
array('empty("wat")'),
array('empty(1.1)'),
array('empty(Foo::$bar)'),
);
}
}

View file

@ -0,0 +1,137 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\LoopContextPass;
class LoopContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new LoopContextPass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('continue'),
array('break'),
array('if (true) { continue; }'),
array('if (true) { break; }'),
array('if (false) { continue; }'),
array('if (false) { break; }'),
array('function foo() { break; }'),
array('function foo() { continue; }'),
// actually enforce break/continue depth argument
array('do { break 2; } while (true)'),
array('do { continue 2; } while (true)'),
array('for ($a; $b; $c) { break 2; }'),
array('for ($a; $b; $c) { continue 2; }'),
array('foreach ($a as $b) { break 2; }'),
array('foreach ($a as $b) { continue 2; }'),
array('switch (true) { default: break 2; }'),
array('switch (true) { default: continue 2; }'),
array('while (true) { break 2; }'),
array('while (true) { continue 2; }'),
// invalid in 5.4+ because they're floats
// ... in 5.3 because the number is too big
array('while (true) { break 2.0; }'),
array('while (true) { continue 2.0; }'),
// and once with nested loops, just for good measure
array('while (true) { while (true) { break 3; } }'),
array('while (true) { while (true) { continue 3; } }'),
);
}
/**
* @dataProvider invalidPHP54Statements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testPHP54ProcessStatementFails($code)
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped();
}
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidPHP54Statements()
{
return array(
// In PHP 5.4+, only positive literal integers are allowed
array('while (true) { break $n; }'),
array('while (true) { continue $n; }'),
array('while (true) { break N; }'),
array('while (true) { continue N; }'),
array('while (true) { break 0; }'),
array('while (true) { continue 0; }'),
array('while (true) { break -1; }'),
array('while (true) { continue -1; }'),
array('while (true) { break 1.0; }'),
array('while (true) { continue 1.0; }'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('do { break; } while (true)'),
array('do { continue; } while (true)'),
array('for ($a; $b; $c) { break; }'),
array('for ($a; $b; $c) { continue; }'),
array('foreach ($a as $b) { break; }'),
array('foreach ($a as $b) { continue; }'),
array('switch (true) { default: break; }'),
array('switch (true) { default: continue; }'),
array('while (true) { break; }'),
array('while (true) { continue; }'),
// `break 1` is redundant, but not invalid
array('while (true) { break 1; }'),
array('while (true) { continue 1; }'),
// and once with nested loops just for good measure
array('while (true) { while (true) { break 2; } }'),
array('while (true) { while (true) { continue 2; } }'),
);
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\MagicConstantsPass;
class MagicConstantsPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new MagicConstantsPass());
}
/**
* @dataProvider magicConstants
*/
public function testProcess($from, $to)
{
$this->assertProcessesAs($from, $to);
}
public function magicConstants()
{
return array(
array('__DIR__;', 'getcwd();'),
array('__FILE__;', "'';"),
array('___FILE___;', '___FILE___;'),
);
}
}

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner;
use Psy\CodeCleaner\NamespacePass;
class NamespacePassTest extends CodeCleanerTestCase
{
private $cleaner;
public function setUp()
{
$this->cleaner = new CodeCleaner();
$this->setPass(new NamespacePass($this->cleaner));
}
public function testProcess()
{
$this->process('array_merge()');
$this->assertNull($this->cleaner->getNamespace());
// A non-block namespace statement should set the current namespace.
$this->process('namespace Alpha');
$this->assertEquals(array('Alpha'), $this->cleaner->getNamespace());
// A new non-block namespace statement should override the current namespace.
$this->process('namespace Beta; class B {}');
$this->assertEquals(array('Beta'), $this->cleaner->getNamespace());
// @todo Figure out if we can detect when the last namespace block is
// bracketed or unbracketed, because this should really clear the
// namespace at the end...
$this->process('namespace Gamma { array_merge(); }');
$this->assertEquals(array('Gamma'), $this->cleaner->getNamespace());
// A null namespace clears out the current namespace.
$this->process('namespace { array_merge(); }');
$this->assertNull($this->cleaner->getNamespace());
}
private function process($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
}
}

View file

@ -0,0 +1,115 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use PhpParser\NodeTraverser;
use Psy\CodeCleaner\PassableByReferencePass;
class PassableByReferencePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->pass = new PassableByReferencePass();
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this->pass);
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
return array(
array('array_pop(array())'),
array('array_pop(array($foo))'),
array('array_shift(array())'),
);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
return array(
array('array_pop(json_decode("[]"))'),
array('array_pop($foo)'),
array('array_pop($foo->bar)'),
array('array_pop($foo::baz)'),
array('array_pop(Foo::qux)'),
);
}
/**
* @dataProvider validArrayMultisort
*/
public function testArrayMultisort($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validArrayMultisort()
{
return array(
array('array_multisort($a)'),
array('array_multisort($a, $b)'),
array('array_multisort($a, SORT_NATURAL, $b)'),
array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b)'),
array('array_multisort($a, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $b)'),
array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, SORT_ASC, $b)'),
array('array_multisort($a, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'),
array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'),
array('array_multisort($a, 1, $b)'),
array('array_multisort($a, 1 + 2, $b)'),
array('array_multisort($a, getMultisortFlags(), $b)'),
);
}
/**
* @dataProvider invalidArrayMultisort
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testInvalidArrayMultisort($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidArrayMultisort()
{
return array(
array('array_multisort(1)'),
array('array_multisort(array(1, 2, 3))'),
array('array_multisort($a, SORT_NATURAL, SORT_ASC, SORT_NATURAL, $b)'),
);
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\RequirePass;
class RequirePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new RequirePass());
}
/**
* @dataProvider exitStatements
*/
public function testExitStatement($from, $to)
{
$this->assertProcessesAs($from, $to);
}
public function exitStatements()
{
$resolve = '\\Psy\\CodeCleaner\\RequirePass::resolve';
if (version_compare(PHP_VERSION, '5.4', '<')) {
return array(
array('require $foo', "require $resolve(\$foo, 1);"),
array('$bar = require $baz', "\$bar = (require $resolve(\$baz, 1));"),
);
}
return array(
// The basics
array('require "a"', "require $resolve(\"a\", 1);"),
array('require "b.php"', "require $resolve(\"b.php\", 1);"),
array('require_once "c"', "require_once $resolve(\"c\", 1);"),
array('require_once "d.php"', "require_once $resolve(\"d.php\", 1);"),
// Ensure that line numbers work correctly
array("null;\nrequire \"e.php\"", "null;\nrequire $resolve(\"e.php\", 2);"),
array("null;\nrequire_once \"f.php\"", "null;\nrequire_once $resolve(\"f.php\", 2);"),
// Things with expressions
array('require $foo', "require $resolve(\$foo, 1);"),
array('require_once $foo', "require_once $resolve(\$foo, 1);"),
array('require ($bar = "g.php")', "require $resolve(\$bar = \"g.php\", 1);"),
array('require_once ($bar = "h.php")', "require_once $resolve(\$bar = \"h.php\", 1);"),
array('$bar = require ($baz = "i.php")', "\$bar = (require $resolve(\$baz = \"i.php\", 1));"),
array('$bar = require_once ($baz = "j.php")', "\$bar = (require_once $resolve(\$baz = \"j.php\", 1));"),
);
}
/**
* @expectedException \Psy\Exception\FatalErrorException
* @expectedExceptionMessage Failed opening required 'not a file name' in eval()'d code on line 2
*/
public function testResolve()
{
RequirePass::resolve('not a file name', 2);
}
/**
* @dataProvider emptyWarnings
*
* @expectedException \Psy\Exception\ErrorException
* @expectedExceptionMessage Filename cannot be empty on line 1
*/
public function testResolveEmptyWarnings($file)
{
if (!E_WARNING & error_reporting()) {
$this->markTestSkipped();
}
RequirePass::resolve($file, 1);
}
public function emptyWarnings()
{
return array(
array(null),
array(false),
array(''),
);
}
}

View file

@ -0,0 +1,94 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\StaticConstructorPass;
class StaticConstructorPassTest extends CodeCleanerTestCase
{
protected function setUp()
{
$this->setPass(new StaticConstructorPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
/**
* @dataProvider invalidParserStatements
* @expectedException \Psy\Exception\ParseErrorException
*/
public function testProcessInvalidStatementCatchedByParser($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
$statements = array(
array('class A { public static function A() {}}'),
array('class A { private static function A() {}}'),
);
if (version_compare(PHP_VERSION, '5.3.3', '<')) {
$statements[] = array('namespace B; class A { private static function A() {}}');
}
return $statements;
}
public function invalidParserStatements()
{
$statements = array(
array('class A { public static function __construct() {}}'),
array('class A { private static function __construct() {}}'),
array('class A { private static function __construct() {} public function A() {}}'),
array('namespace B; class A { private static function __construct() {}}'),
);
return $statements;
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function validStatements()
{
$statements = array(
array('class A { public static function A() {} public function __construct() {}}'),
array('class A { private function __construct() {} public static function A() {}}'),
);
if (version_compare(PHP_VERSION, '5.3.3', '>=')) {
$statements[] = array('namespace B; class A { private static function A() {}}');
}
return $statements;
}
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\StrictTypesPass;
class StrictTypesPassTest extends CodeCleanerTestCase
{
public function setUp()
{
if (version_compare(PHP_VERSION, '7.0', '<')) {
$this->markTestSkipped();
}
$this->setPass(new StrictTypesPass());
}
public function testProcess()
{
$this->assertProcessesAs('declare(strict_types=1)', 'declare (strict_types=1);');
$this->assertProcessesAs('null', "declare (strict_types=1);\nnull;");
$this->assertProcessesAs('declare(strict_types=0)', 'declare (strict_types=0);');
$this->assertProcessesAs('null', 'null;');
}
/**
* @dataProvider invalidDeclarations
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testInvalidDeclarations($declaration)
{
$stmts = $this->parse($declaration);
$this->traverser->traverse($stmts);
}
public function invalidDeclarations()
{
return array(
array('declare(strict_types=-1)'),
array('declare(strict_types=2)'),
array('declare(strict_types="foo")'),
);
}
}

View file

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\UseStatementPass;
class UseStatementPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new UseStatementPass());
}
/**
* @dataProvider useStatements
*/
public function testProcess($from, $to)
{
$this->assertProcessesAs($from, $to);
}
public function useStatements()
{
return array(
array(
"use StdClass as NotSoStd;\n\$std = new NotSoStd();",
'$std = new \\StdClass();',
),
array(
"namespace Foo;\n\nuse StdClass as S;\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();",
),
array(
"namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();",
),
array(
"use Foo\\Bar as fb;\n\$baz = new fb\\Baz();",
'$baz = new \\Foo\\Bar\\Baz();',
),
);
}
}

View file

@ -0,0 +1,336 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\ValidClassNamePass;
use Psy\Exception\Exception;
class ValidClassNamePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ValidClassNamePass());
}
/**
* @dataProvider getInvalid
*/
public function testProcessInvalid($code, $php54 = false)
{
try {
$stmts = $this->parse($code);
$this->traverse($stmts);
$this->fail();
} catch (Exception $e) {
if ($php54 && version_compare(PHP_VERSION, '5.4', '<')) {
$this->assertInstanceOf('Psy\Exception\ParseErrorException', $e);
} else {
$this->assertInstanceOf('Psy\Exception\FatalErrorException', $e);
}
}
}
public function getInvalid()
{
// class declarations
return array(
// core class
array('class stdClass {}'),
// capitalization
array('class stdClass {}'),
// collisions with interfaces and traits
array('interface stdClass {}'),
array('trait stdClass {}', true),
// collisions inside the same code snippet
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
', true),
array('
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
', true),
array('
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
', true),
array('
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
', true),
array('
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'),
// namespaced collisions
array('
namespace Psy\\Test\\CodeCleaner {
class ValidClassNamePassTest {}
}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Beta {}
}
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Beta {}
}
'),
// extends and implements
array('class ValidClassNamePassTest extends NotAClass {}'),
array('class ValidClassNamePassTest extends ArrayAccess {}'),
array('class ValidClassNamePassTest implements stdClass {}'),
array('class ValidClassNamePassTest implements ArrayAccess, stdClass {}'),
array('interface ValidClassNamePassTest extends stdClass {}'),
array('interface ValidClassNamePassTest extends ArrayAccess, stdClass {}'),
// class instantiations
array('new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
}
'),
// class constant fetch
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'),
// static call
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'),
array('Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'),
);
}
/**
* @dataProvider getValid
*/
public function testProcessValid($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function getValid()
{
$valid = array(
// class declarations
array('class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'),
array('namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'),
array('
namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
}
'),
array('namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'),
// class instantiations
array('new stdClass();'),
array('new stdClass();'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Theta {}
}
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
new Theta();
}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Iota {}
new Iota();
}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Kappa {}
}
namespace {
new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
}
'),
// Class constant fetch (ValidConstantPassTest validates the actual constant)
array('class A {} A::FOO'),
array('$a = new DateTime; $a::ATOM'),
array('interface A { const B = 1; } A::B'),
// static call
array('DateTime::createFromFormat()'),
array('DateTime::$someMethod()'),
array('Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'),
array('Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'),
// Allow `self` and `static` as class names.
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new self();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new SELF();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new self;
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new static();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new Static();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new static;
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return parent::bar();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return self::bar();
}
}
'),
array('
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return static::bar();
}
}
'),
array('class A { static function b() { return new A; } }'),
array('
class A {
const B = 123;
function c() {
return A::B;
}
}
'),
array('class A {} class B { function c() { return new A; } }'),
// recursion
array('class A { function a() { A::a(); } }'),
// conditionally defined classes
array('
class A {}
if (false) {
class A {}
}
'),
array('
class A {}
if (true) {
class A {}
} else if (false) {
class A {}
} else {
class A {}
}
'),
// ewww
array('
class A {}
if (true):
class A {}
elseif (false):
class A {}
else:
class A {}
endif;
'),
array('
class A {}
while (false) { class A {} }
'),
array('
class A {}
do { class A {} } while (false);
'),
array('
class A {}
switch (1) {
case 0:
class A {}
break;
case 1:
class A {}
break;
case 2:
class A {}
break;
}
'),
);
// Ugh. There's gotta be a better way to test for this.
if (class_exists('PhpParser\ParserFactory')) {
// PHP 7.0 anonymous classes, only supported by PHP Parser v2.x
$valid[] = array('$obj = new class() {}');
}
if (version_compare(PHP_VERSION, '5.5', '>=')) {
$valid[] = array('interface A {} A::class');
$valid[] = array('interface A {} A::CLASS');
$valid[] = array('class A {} A::class');
$valid[] = array('class A {} A::CLASS');
$valid[] = array('A::class');
$valid[] = array('A::CLASS');
}
return $valid;
}
}

View file

@ -0,0 +1,69 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\ValidConstantPass;
class ValidConstantPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ValidConstantPass());
}
/**
* @dataProvider getInvalidReferences
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalidConstantReferences($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
}
public function getInvalidReferences()
{
return array(
array('Foo\BAR'),
// class constant fetch
array('Psy\Test\CodeCleaner\ValidConstantPassTest::FOO'),
array('DateTime::BACON'),
);
}
/**
* @dataProvider getValidReferences
*/
public function testProcessValidConstantReferences($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function getValidReferences()
{
return array(
array('PHP_EOL'),
// class constant fetch
array('NotAClass::FOO'),
array('DateTime::ATOM'),
array('$a = new DateTime; $a::ATOM'),
array('DateTime::class'),
array('$a = new DateTime; $a::class'),
);
}
}

View file

@ -0,0 +1,184 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2017 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\CodeCleaner;
use Psy\CodeCleaner\ValidFunctionNamePass;
class ValidFunctionNamePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ValidFunctionNamePass());
}
/**
* @dataProvider getInvalidFunctions
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalidFunctionCallsAndDeclarations($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
}
public function getInvalidFunctions()
{
return array(
// function declarations
array('function array_merge() {}'),
array('function Array_Merge() {}'),
array('
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function beta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function beta() {}
}
'),
// function calls
array('psy_test_codecleaner_validfunctionnamepass_gamma()'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
delta();
}
'),
// recursion
array('function a() { a(); } function a() {}'),
);
}
/**
* @dataProvider getValidFunctions
*/
public function testProcessValidFunctionCallsAndDeclarations($code)
{
$stmts = $this->parse($code);
$this->traverse($stmts);
// @todo a better thing to assert here?
$this->assertTrue(true);
}
public function getValidFunctions()
{
return array(
array('function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function zeta() {}
}
'),
array('
namespace {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
namespace {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function array_merge() {}
}
'),
// function calls
array('array_merge();'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function theta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
theta();
}
'),
// closures
array('$test = function(){};$test()'),
array('
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function theta() {}
}
namespace {
Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
}
'),
// recursion
array('function a() { a(); }'),
// conditionally defined functions
array('
function a() {}
if (false) {
function a() {}
}
'),
array('
function a() {}
if (true) {
function a() {}
} else if (false) {
function a() {}
} else {
function a() {}
}
'),
// ewww
array('
function a() {}
if (true):
function a() {}
elseif (false):
function a() {}
else:
function a() {}
endif;
'),
array('
function a() {}
while (false) { function a() {} }
'),
array('
function a() {}
do { function a() {} } while (false);
'),
array('
function a() {}
switch (1) {
case 0:
function a() {}
break;
case 1:
function a() {}
break;
case 2:
function a() {}
break;
}
'),
);
}
}