Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\AbstractClassPass;
class AbstractClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new AbstractClassPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['class A { abstract function a(); }'],
['abstract class B { abstract function b() {} }'],
['abstract class B { abstract function b() { echo "yep"; } }'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['abstract class C { function c() {} }'],
['abstract class D { abstract function d(); }'],
];
}
}

View file

@ -0,0 +1,58 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\AssignThisVariablePass;
class AssignThisVariablePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new AssignThisVariablePass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['$this = 3'],
['strtolower($this = "this")'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['$this'],
['$a = $this'],
['$a = "this"; $$a = 3'],
['$$this = "b"'],
];
}
}

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\CallTimePassByReferencePass;
class CallTimePassByReferencePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new CallTimePassByReferencePass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['f(&$arg)'],
['$object->method($first, &$arg)'],
['$closure($first, &$arg, $last)'],
['A::b(&$arg)'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['array(&$var)'],
['$a = &$b'],
['f(array(&$b))'],
];
}
}

View file

@ -0,0 +1,90 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\CalledClassPass;
class CalledClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new CalledClassPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\ErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['get_class()'],
['get_class(null)'],
['get_called_class()'],
['get_called_class(null)'],
['function foo() { return get_class(); }'],
['function foo() { return get_class(null); }'],
['function foo() { return get_called_class(); }'],
['function foo() { return get_called_class(null); }'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['get_class($foo)'],
['get_class(bar())'],
['get_called_class($foo)'],
['get_called_class(bar())'],
['function foo($bar) { return get_class($bar); }'],
['function foo($bar) { return get_called_class($bar); }'],
['class Foo { function bar() { return get_class(); } }'],
['class Foo { function bar() { return get_class(null); } }'],
['class Foo { function bar() { return get_called_class(); } }'],
['class Foo { function bar() { return get_called_class(null); } }'],
['$foo = function () {}; $foo()'],
];
}
/**
* @dataProvider validTraitStatements
*/
public function testProcessTraitStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validTraitStatements()
{
return [
['trait Foo { function bar() { return get_class(); } }'],
['trait Foo { function bar() { return get_class(null); } }'],
['trait Foo { function bar() { return get_called_class(); } }'],
['trait Foo { function bar() { return get_called_class(null); } }'],
];
}
}

View file

@ -0,0 +1,41 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\CodeCleanerPass;
use Psy\Test\ParserTestCase;
class CodeCleanerTestCase extends ParserTestCase
{
protected $pass;
public function tearDown()
{
$this->pass = null;
parent::tearDown();
}
protected function setPass(CodeCleanerPass $pass)
{
$this->pass = $pass;
if (!isset($this->traverser)) {
$this->traverser = new NodeTraverser();
}
$this->traverser->addVisitor($this->pass);
}
protected function parseAndTraverse($code, $prefix = '<?php ')
{
return $this->traverse($this->parse($code, $prefix));
}
}

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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 [
['exit;', "{$this->expectedExceptionString};"],
['exit();', "{$this->expectedExceptionString};"],
['die;', "{$this->expectedExceptionString};"],
['exit(die(die));', "{$this->expectedExceptionString};"],
['if (true) { exit; }', "if (true) {\n {$this->expectedExceptionString};\n}"],
['if (false) { exit; }', "if (false) {\n {$this->expectedExceptionString};\n}"],
['1 and exit();', "1 and {$this->expectedExceptionString};"],
['foo() or die', "foo() or {$this->expectedExceptionString};"],
['exit and 1;', "{$this->expectedExceptionString} and 1;"],
['if (exit) { echo $wat; }', "if ({$this->expectedExceptionString}) {\n echo \$wat;\n}"],
['exit or die;', "{$this->expectedExceptionString} or {$this->expectedExceptionString};"],
['switch (die) { }', "switch ({$this->expectedExceptionString}) {\n}"],
['for ($i = 1; $i < 10; die) {}', "for (\$i = 1; \$i < 10; {$this->expectedExceptionString}) {\n}"],
];
}
}

View file

@ -0,0 +1,65 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\FinalClassPass;
class FinalClassPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new FinalClassPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
$data = [
['final class A {} class B extends A {}'],
['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?
$data[] = ['class A extends \\Closure {}'];
}
return $data;
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['class A extends \\stdClass {}'],
['final class A extends \\stdClass {}'],
['class A {} class B extends A {}'],
];
}
}

View file

@ -0,0 +1,20 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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-2018 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,20 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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;
trait TraitWithStatic
{
public static function doStuff()
{
// Don't actually do stuff.
}
}

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\FunctionContextPass;
class FunctionContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new FunctionContextPass());
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['function foo() { yield; }'],
['if (function(){ yield; })'],
];
}
/**
* @dataProvider invalidYieldStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testInvalidYield($code)
{
$this->parseAndTraverse($code);
}
public function invalidYieldStatements()
{
return [
['yield'],
['if (yield)'],
];
}
}

View file

@ -0,0 +1,91 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\FunctionReturnInWriteContextPass;
use Psy\Exception\FatalErrorException;
class FunctionReturnInWriteContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new FunctionReturnInWriteContextPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
* @expectedExceptionMessage Can't use function return value in write context
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['f(&g())'],
['array(& $object->method())'],
['$a->method(& $closure())'],
['array(& A::b())'],
['f() = 5'],
['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"))'));
}
/**
* @dataProvider validStatements
*/
public function testValidStatements($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['isset($foo)'],
];
}
}

View file

@ -0,0 +1,112 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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()
{
$data = [
['4', 'return 4;'],
['foo()', 'return foo();'],
['return 1', 'return 1;'],
['', 'return new \Psy\CodeCleaner\NoReturnValue();'],
];
$from = 'echo "foo";';
$to = <<<'EOS'
echo "foo";
return new \Psy\CodeCleaner\NoReturnValue();
EOS;
$data[] = [$from, $to];
$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;
$data[] = [$from, $to];
$from = 'class A {}';
$to = <<<'EOS'
class A
{
}
return new \Psy\CodeCleaner\NoReturnValue();
EOS;
$data[] = [$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;
$data[] = [$from, $to];
$from = <<<'EOS'
namespace Foo {
1 + 1;
}
EOS;
$to = <<<'EOS'
namespace Foo;
return 1 + 1;
EOS;
$data[] = [$from, $to];
$data[] = ['exit()', 'exit;'];
return $data;
}
}

View file

@ -0,0 +1,72 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['null instanceof stdClass'],
['true instanceof stdClass'],
['9 instanceof stdClass'],
['1.0 instanceof stdClass'],
['"foo" instanceof stdClass'],
['__DIR__ instanceof stdClass'],
['PHP_SAPI instanceof stdClass'],
['1+1 instanceof stdClass'],
['true && false instanceof stdClass'],
['"a"."b" instanceof stdClass'],
['!5 instanceof stdClass'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
$data = [
['$a instanceof stdClass'],
['strtolower("foo") instanceof stdClass'],
['array(1) instanceof stdClass'],
['(string) "foo" instanceof stdClass'],
['(1+1) instanceof stdClass'],
['"foo ${foo} $bar" instanceof stdClass'],
['DateTime::ISO8601 instanceof stdClass'],
];
return $data;
}
}

View file

@ -0,0 +1,69 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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);
$this->assertTrue(true);
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['array_merge()'],
['__psysh__()'],
['$this'],
['$psysh'],
['$__psysh'],
['$banana'],
];
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\RuntimeException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['$__psysh__'],
['var_dump($__psysh__)'],
['$__psysh__ = "your mom"'],
['$__psysh__->fakeFunctionCall()'],
];
}
}

View file

@ -0,0 +1,76 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
if (\version_compare(PHP_VERSION, '5.5', '>=')) {
return [
['empty()'],
];
}
return [
['empty()'],
['empty(null)'],
['empty(PHP_EOL)'],
['empty("wat")'],
['empty(1.1)'],
['empty(Foo::$bar)'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
if (\version_compare(PHP_VERSION, '5.5', '<')) {
return [
['empty($foo)'],
];
}
return [
['empty($foo)'],
['empty(null)'],
['empty(PHP_EOL)'],
['empty("wat")'],
['empty(1.1)'],
['empty(Foo::$bar)'],
];
}
}

View file

@ -0,0 +1,115 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\ListPass;
class ListPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ListPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\ParseErrorException
*/
public function testProcessInvalidStatement($code, $expectedMessage)
{
if (\method_exists($this, 'setExpectedException')) {
$this->setExpectedException('Psy\Exception\ParseErrorException', $expectedMessage);
} else {
$this->expectExceptionMessage($expectedMessage);
}
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
}
public function invalidStatements()
{
// Not typo. It is ambiguous whether "Syntax" or "syntax".
$errorShortListAssign = "yntax error, unexpected '='";
$errorEmptyList = 'Cannot use empty list';
$errorAssocListAssign = 'Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting \',\' or \')\'';
$errorNonVariableAssign = 'Assignments can only happen to writable values';
$errorPhpParserSyntax = 'PHP Parse error: Syntax error, unexpected';
$invalidExpr = [
['list() = array()', $errorEmptyList],
['list("a") = array(1)', $errorPhpParserSyntax],
];
if (\version_compare(PHP_VERSION, '7.1', '<')) {
return \array_merge($invalidExpr, [
['list("a" => _) = array("a" => 1)', $errorPhpParserSyntax],
['[] = []', $errorShortListAssign],
['[$a] = [1]', $errorShortListAssign],
['list("a" => $a) = array("a" => 1)', $errorAssocListAssign],
['[$a[0], $a[1]] = [1, 2]', $errorShortListAssign],
['[$a->b, $a->c] = [1, 2]', $errorShortListAssign],
]);
}
return \array_merge($invalidExpr, [
['list("a" => _) = array("a" => 1)', $errorPhpParserSyntax],
['["a"] = [1]', $errorNonVariableAssign],
['[] = []', $errorEmptyList],
['[,] = [1,2]', $errorEmptyList],
['[,,] = [1,2,3]', $errorEmptyList],
]);
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$stmts = $this->parse($code);
$this->traverser->traverse($stmts);
$this->assertTrue(true);
}
public function validStatements()
{
$validExpr = [
['list($a) = array(1)'],
['list($x, $y) = array(1, 2)'],
];
if (\version_compare(PHP_VERSION, '7.1', '>=')) {
return \array_merge($validExpr, [
['[$a] = array(1)'],
['list($b) = [2]'],
['[$x, $y] = array(1, 2)'],
['[$a] = [1]'],
['[$x, $y] = [1, 2]'],
['["_" => $v] = ["_" => 1]'],
['[$a,] = [1,2,3]'],
['[,$b] = [1,2,3]'],
['[$a,,$c] = [1,2,3]'],
['[$a,,,] = [1,2,3]'],
['[$a[0], $a[1]] = [1, 2]'],
['[$a[0][0][0], $a[0][0][1]] = [1, 2]'],
['[$a->b, $a->c] = [1, 2]'],
['[$a->b[0], $a->c[1]] = [1, 2]'],
['[$a[0]->b[0], $a[0]->c[1]] = [1, 2]'],
['[$a[$b->c + $b->d]] = [1]'],
['[$a->c()->d, $a->c()->e] = [1, 2]'],
['[x()->a, x()->b] = [1, 2]'],
]);
}
return $validExpr;
}
}

View file

@ -0,0 +1,108 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\LoopContextPass;
class LoopContextPassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new LoopContextPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['continue'],
['break'],
['if (true) { continue; }'],
['if (true) { break; }'],
['if (false) { continue; }'],
['if (false) { break; }'],
['function foo() { break; }'],
['function foo() { continue; }'],
// actually enforce break/continue depth argument
['do { break 2; } while (true)'],
['do { continue 2; } while (true)'],
['for ($a; $b; $c) { break 2; }'],
['for ($a; $b; $c) { continue 2; }'],
['foreach ($a as $b) { break 2; }'],
['foreach ($a as $b) { continue 2; }'],
['switch (true) { default: break 2; }'],
['switch (true) { default: continue 2; }'],
['while (true) { break 2; }'],
['while (true) { continue 2; }'],
// In PHP 5.4+, only positive literal integers are allowed
['while (true) { break $n; }'],
['while (true) { continue $n; }'],
['while (true) { break N; }'],
['while (true) { continue N; }'],
['while (true) { break 0; }'],
['while (true) { continue 0; }'],
['while (true) { break -1; }'],
['while (true) { continue -1; }'],
['while (true) { break 1.0; }'],
['while (true) { continue 1.0; }'],
['while (true) { break 2.0; }'],
['while (true) { continue 2.0; }'],
// and once with nested loops, just for good measure
['while (true) { while (true) { break 3; } }'],
['while (true) { while (true) { continue 3; } }'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['do { break; } while (true)'],
['do { continue; } while (true)'],
['for ($a; $b; $c) { break; }'],
['for ($a; $b; $c) { continue; }'],
['foreach ($a as $b) { break; }'],
['foreach ($a as $b) { continue; }'],
['switch (true) { default: break; }'],
['switch (true) { default: continue; }'],
['while (true) { break; }'],
['while (true) { continue; }'],
// `break 1` is redundant, but not invalid
['while (true) { break 1; }'],
['while (true) { continue 1; }'],
// and once with nested loops just for good measure
['while (true) { while (true) { break 2; } }'],
['while (true) { while (true) { continue 2; } }'],
];
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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 [
['__DIR__;', 'getcwd();'],
['__FILE__;', "'';"],
['___FILE___;', '___FILE___;'],
];
}
}

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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->parseAndTraverse('');
$this->assertNull($this->cleaner->getNamespace());
$this->parseAndTraverse('array_merge()');
$this->assertNull($this->cleaner->getNamespace());
// A non-block namespace statement should set the current namespace.
$this->parseAndTraverse('namespace Alpha');
$this->assertSame(['Alpha'], $this->cleaner->getNamespace());
// A new non-block namespace statement should override the current namespace.
$this->parseAndTraverse('namespace Beta; class B {}');
$this->assertSame(['Beta'], $this->cleaner->getNamespace());
// A new block namespace clears out the current namespace...
$this->parseAndTraverse('namespace Gamma { array_merge(); }');
if (\defined('PhpParser\\Node\\Stmt\\Namespace_::KIND_SEMICOLON')) {
$this->assertNull($this->cleaner->getNamespace());
} else {
// But not for PHP-Parser < v3.1.2 :(
$this->assertSame(['Gamma'], $this->cleaner->getNamespace());
}
$this->parseAndTraverse('namespace Delta');
// A null namespace clears out the current namespace.
$this->parseAndTraverse('namespace { array_merge(); }');
$this->assertNull($this->cleaner->getNamespace());
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\Node\Stmt\Expression;
use Psy\CodeCleaner\NoReturnValue;
use Psy\Test\ParserTestCase;
class NoReturnValueTest extends ParserTestCase
{
public function testCreate()
{
$stmt = NoReturnValue::create();
if (\class_exists('PhpParser\Node\Stmt\Expression')) {
$stmt = new Expression($stmt);
}
$this->assertSame(
$this->prettyPrint($this->parse('new \\Psy\CodeCleaner\\NoReturnValue()')),
$this->prettyPrint([$stmt])
);
}
}

View file

@ -0,0 +1,104 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\PassableByReferencePass;
class PassableByReferencePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new PassableByReferencePass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessStatementFails($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
return [
['array_pop(array())'],
['array_pop(array($foo))'],
['array_shift(array())'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessStatementPasses($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
return [
['array_pop(json_decode("[]"))'],
['array_pop($foo)'],
['array_pop($foo->bar)'],
['array_pop($foo::baz)'],
['array_pop(Foo::qux)'],
];
}
/**
* @dataProvider validArrayMultisort
*/
public function testArrayMultisort($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validArrayMultisort()
{
return [
['array_multisort($a)'],
['array_multisort($a, $b)'],
['array_multisort($a, SORT_NATURAL, $b)'],
['array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b)'],
['array_multisort($a, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $b)'],
['array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, SORT_ASC, $b)'],
['array_multisort($a, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'],
['array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'],
['array_multisort($a, 1, $b)'],
['array_multisort($a, 1 + 2, $b)'],
['array_multisort($a, getMultisortFlags(), $b)'],
];
}
/**
* @dataProvider invalidArrayMultisort
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testInvalidArrayMultisort($code)
{
$this->parseAndTraverse($code);
}
public function invalidArrayMultisort()
{
return [
['array_multisort(1)'],
['array_multisort(array(1, 2, 3))'],
['array_multisort($a, SORT_NATURAL, SORT_ASC, SORT_NATURAL, $b)'],
];
}
}

View file

@ -0,0 +1,93 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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';
return [
// The basics
['require "a"', "require $resolve(\"a\", 1);"],
['require "b.php"', "require $resolve(\"b.php\", 1);"],
['require_once "c"', "require_once $resolve(\"c\", 1);"],
['require_once "d.php"', "require_once $resolve(\"d.php\", 1);"],
// Ensure that line numbers work correctly
["null;\nrequire \"e.php\"", "null;\nrequire $resolve(\"e.php\", 2);"],
["null;\nrequire_once \"f.php\"", "null;\nrequire_once $resolve(\"f.php\", 2);"],
// Things with expressions
['require $foo', "require $resolve(\$foo, 1);"],
['require_once $foo', "require_once $resolve(\$foo, 1);"],
['require ($bar = "g.php")', "require $resolve(\$bar = \"g.php\", 1);"],
['require_once ($bar = "h.php")', "require_once $resolve(\$bar = \"h.php\", 1);"],
['$bar = require ($baz = "i.php")', "\$bar = (require $resolve(\$baz = \"i.php\", 1));"],
['$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 [
[null],
[false],
[''],
];
}
public function testResolveWorks()
{
$this->assertEquals(__FILE__, RequirePass::resolve(__FILE__, 3));
}
}

View file

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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($code)
{
$this->parseAndTraverse($code);
}
public function invalidDeclarations()
{
return [
['declare(strict_types=-1)'],
['declare(strict_types=2)'],
['declare(strict_types="foo")'],
];
}
}

View file

@ -0,0 +1,102 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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 [
[
"use StdClass as NotSoStd;\n\$std = new NotSoStd();",
'$std = new \\StdClass();',
],
[
"namespace Foo;\n\nuse StdClass as S;\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();",
],
[
"namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();",
],
[
"use Foo\\Bar as fb;\n\$baz = new fb\\Baz();",
'$baz = new \\Foo\\Bar\\Baz();',
],
[
"use Foo\\Bar;\n\$baz = new Bar\\Baz();",
'$baz = new \\Foo\\Bar\\Baz();',
],
[
"namespace Foo;\nuse Bar;\n\$baz = new Bar\\Baz();",
"namespace Foo;\n\n\$baz = new \\Bar\\Baz();",
],
[
"namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();\nnamespace Foo;\n\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();\nnamespace Foo;\n\n\$std = new \\StdClass();",
],
[
"namespace Foo;\n\nuse \\StdClass as S;\n\$std = new S();\nnamespace Bar;\n\n\$std = new S();",
"namespace Foo;\n\n\$std = new \\StdClass();\nnamespace Bar;\n\n\$std = new S();",
],
[
"use Foo\\Bar as fb, Qux as Q;\n\$baz = new fb\\Baz();\n\$qux = new Q();",
"\$baz = new \\Foo\\Bar\\Baz();\n\$qux = new \\Qux();",
],
];
}
/**
* @dataProvider groupUseStatements
*/
public function testGroupUseProcess($from, $to)
{
$this->assertProcessesAs($from, $to);
}
public function groupUseStatements()
{
if (\version_compare(PHP_VERSION, '7.0', '<')) {
$this->markTestSkipped();
}
return [
[
"use Foo\\{Bar, Baz, Qux as Q};\n\$bar = new Bar();\n\$baz = new Baz();\n\$qux = new Q();",
"\$bar = new \\Foo\\Bar();\n\$baz = new \\Foo\\Baz();\n\$qux = new \\Foo\\Qux();",
],
[
"use X\\{Foo, Bar as B};\n\$foo = new Foo();\n\$baz = new B\\Baz();",
"\$foo = new \\X\\Foo();\n\$baz = new \\X\\Bar\\Baz();",
],
[
"use X\\{Foo, Bar as B};\n\$foo = new Foo();\n\$bar = new Bar();\n\$baz = new B\\Baz();",
"\$foo = new \\X\\Foo();\n\$bar = new Bar();\n\$baz = new \\X\\Bar\\Baz();",
],
];
}
}

View file

@ -0,0 +1,325 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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;
class ValidClassNamePassTest extends CodeCleanerTestCase
{
public function setUp()
{
$this->setPass(new ValidClassNamePass());
}
/**
* @dataProvider getInvalid
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalid($code)
{
$this->parseAndTraverse($code);
}
public function getInvalid()
{
// class declarations
return [
// core class
['class stdClass {}'],
// capitalization
['class stdClass {}'],
// collisions with interfaces and traits
['interface stdClass {}'],
['trait stdClass {}'],
// collisions inside the same code snippet
['
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
trait Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
interface Psy_Test_CodeCleaner_ValidClassNamePass_Alpha {}
'],
// namespaced collisions
['
namespace Psy\\Test\\CodeCleaner {
class ValidClassNamePassTest {}
}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Beta {}
}
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Beta {}
}
'],
// extends and implements
['class ValidClassNamePassTest extends NotAClass {}'],
['class ValidClassNamePassTest extends ArrayAccess {}'],
['class ValidClassNamePassTest implements stdClass {}'],
['class ValidClassNamePassTest implements ArrayAccess, stdClass {}'],
['interface ValidClassNamePassTest extends stdClass {}'],
['interface ValidClassNamePassTest extends ArrayAccess, stdClass {}'],
// class instantiations
['new Psy_Test_CodeCleaner_ValidClassNamePass_Gamma();'],
['
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
new Psy_Test_CodeCleaner_ValidClassNamePass_Delta();
}
'],
// class constant fetch
['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::FOO'],
// static call
['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::foo()'],
['Psy\\Test\\CodeCleaner\\ValidClassNamePass\\NotAClass::$foo()'],
['Psy\\Test\\CodeCleaner\\ValidClassNamePassTest::notAMethod()'],
];
}
/**
* @dataProvider getValid
*/
public function testProcessValid($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function getValid()
{
$valid = [
// class declarations
['class Psy_Test_CodeCleaner_ValidClassNamePass_Epsilon {}'],
['namespace Psy\Test\CodeCleaner\ValidClassNamePass; class Zeta {}'],
['
namespace { class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}; }
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Psy_Test_CodeCleaner_ValidClassNamePass_Eta {}
}
'],
['namespace Psy\Test\CodeCleaner\ValidClassNamePass { class stdClass {} }'],
// class instantiations
['new stdClass();'],
['new stdClass();'],
['
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Theta {}
}
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
new Theta();
}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Iota {}
new Iota();
}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidClassNamePass {
class Kappa {}
}
namespace {
new \\Psy\\Test\\CodeCleaner\\ValidClassNamePass\\Kappa();
}
'],
// Class constant fetch (ValidConstantPassTest validates the actual constant)
['class A {} A::FOO'],
['$a = new DateTime; $a::ATOM'],
['interface A { const B = 1; } A::B'],
// static call
['DateTime::createFromFormat()'],
['DateTime::$someMethod()'],
['Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'],
['Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'],
['Psy\Test\CodeCleaner\Fixtures\TraitWithStatic::doStuff()'],
// Allow `self` and `static` as class names.
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new self();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new SELF();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new self;
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new static();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new Static();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function getInstance() {
return new static;
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return parent::bar();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return self::bar();
}
}
'],
['
class Psy_Test_CodeCleaner_ValidClassNamePass_ClassWithStatic {
public static function foo() {
return static::bar();
}
}
'],
['class A { static function b() { return new A; } }'],
['
class A {
const B = 123;
function c() {
return A::B;
}
}
'],
['class A {} class B { function c() { return new A; } }'],
// recursion
['class A { function a() { A::a(); } }'],
// conditionally defined classes
['
class A {}
if (false) {
class A {}
}
'],
['
class A {}
if (true) {
class A {}
} else if (false) {
class A {}
} else {
class A {}
}
'],
// ewww
['
class A {}
if (true):
class A {}
elseif (false):
class A {}
else:
class A {}
endif;
'],
['
class A {}
while (false) { class A {} }
'],
['
class A {}
do { class A {} } while (false);
'],
['
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[] = ['$obj = new class() {}'];
}
if (\version_compare(PHP_VERSION, '5.5', '>=')) {
$valid[] = ['interface A {} A::class'];
$valid[] = ['interface A {} A::CLASS'];
$valid[] = ['class A {} A::class'];
$valid[] = ['class A {} A::CLASS'];
$valid[] = ['A::class'];
$valid[] = ['A::CLASS'];
}
return $valid;
}
}

View file

@ -0,0 +1,65 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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)
{
$this->parseAndTraverse($code);
}
public function getInvalidReferences()
{
return [
['Foo\BAR'],
// class constant fetch
['Psy\Test\CodeCleaner\ValidConstantPassTest::FOO'],
['DateTime::BACON'],
];
}
/**
* @dataProvider getValidReferences
*/
public function testProcessValidConstantReferences($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function getValidReferences()
{
return [
['PHP_EOL'],
// class constant fetch
['NotAClass::FOO'],
['DateTime::ATOM'],
['$a = new DateTime; $a::ATOM'],
['DateTime::class'],
['$a = new DateTime; $a::class'],
];
}
}

View file

@ -0,0 +1,93 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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\ValidConstructorPass;
class ValidConstructorPassTest extends CodeCleanerTestCase
{
protected function setUp()
{
$this->setPass(new ValidConstructorPass());
}
/**
* @dataProvider invalidStatements
* @expectedException \Psy\Exception\FatalErrorException
*/
public function testProcessInvalidStatement($code)
{
$this->parseAndTraverse($code);
}
/**
* @dataProvider invalidParserStatements
* @expectedException \Psy\Exception\ParseErrorException
*/
public function testProcessInvalidStatementCatchedByParser($code)
{
$this->parseAndTraverse($code);
}
public function invalidStatements()
{
$data = [
['class A { public static function A() {}}'],
['class A { public static function a() {}}'],
['class A { private static function A() {}}'],
['class A { private static function a() {}}'],
];
if (\version_compare(PHP_VERSION, '7.0', '>=')) {
$data[] = ['class A { public function A(): ?array {}}'];
$data[] = ['class A { public function a(): ?array {}}'];
}
return $data;
}
public function invalidParserStatements()
{
return [
['class A { public static function __construct() {}}'],
['class A { private static function __construct() {}}'],
['class A { private static function __construct() {} public function A() {}}'],
['namespace B; class A { private static function __construct() {}}'],
];
}
/**
* @dataProvider validStatements
*/
public function testProcessValidStatement($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function validStatements()
{
$data = [
['class A { public static function A() {} public function __construct() {}}'],
['class A { private function __construct() {} public static function A() {}}'],
['namespace B; class A { private static function A() {}}'],
];
if (\version_compare(PHP_VERSION, '7.0', '>=')) {
$data[] = ['class A { public static function A() {} public function __construct() {}}'];
$data[] = ['class A { private function __construct() {} public static function A(): ?array {}}'];
$data[] = ['namespace B; class A { private static function A(): ?array {}}'];
}
return $data;
}
}

View file

@ -0,0 +1,180 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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)
{
$this->parseAndTraverse($code);
}
public function getInvalidFunctions()
{
return [
// function declarations
['function array_merge() {}'],
['function Array_Merge() {}'],
['
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
function psy_test_codecleaner_validfunctionnamepass_alpha() {}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function beta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function beta() {}
}
'],
// function calls
['psy_test_codecleaner_validfunctionnamepass_gamma()'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
delta();
}
'],
// recursion
['function a() { a(); } function a() {}'],
];
}
/**
* @dataProvider getValidFunctions
*/
public function testProcessValidFunctionCallsAndDeclarations($code)
{
$this->parseAndTraverse($code);
$this->assertTrue(true);
}
public function getValidFunctions()
{
return [
['function psy_test_codecleaner_validfunctionnamepass_epsilon() {}'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function zeta() {}
}
'],
['
namespace {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
namespace {
function psy_test_codecleaner_validfunctionnamepass_eta() {}
}
'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function array_merge() {}
}
'],
// function calls
['array_merge();'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function theta() {}
}
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
theta();
}
'],
// closures
['$test = function(){};$test()'],
['
namespace Psy\\Test\\CodeCleaner\\ValidFunctionNamePass {
function theta() {}
}
namespace {
Psy\\Test\\CodeCleaner\\ValidFunctionNamePass\\theta();
}
'],
// recursion
['function a() { a(); }'],
// conditionally defined functions
['
function a() {}
if (false) {
function a() {}
}
'],
['
function a() {}
if (true) {
function a() {}
} else if (false) {
function a() {}
} else {
function a() {}
}
'],
// ewww
['
function a() {}
if (true):
function a() {}
elseif (false):
function a() {}
else:
function a() {}
endif;
'],
['
function a() {}
while (false) { function a() {} }
'],
['
function a() {}
do { function a() {} } while (false);
'],
['
function a() {}
switch (1) {
case 0:
function a() {}
break;
case 1:
function a() {}
break;
case 2:
function a() {}
break;
}
'],
];
}
}