[CS] Use ::class notation instead of string

Conflicts:
	test/PhpParser/ParserFactoryTest.php
	test/PhpParser/ParserTest.php
This commit is contained in:
Nikita Popov 2018-01-13 16:08:17 +01:00
parent f6617e6d25
commit c7ada124d0
10 changed files with 57 additions and 55 deletions

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
if (!class_exists('PhpParser\Autoloader')) {
if (!class_exists(PhpParser\Autoloader::class)) {
require __DIR__ . '/PhpParser/Autoloader.php';
}
PhpParser\Autoloader::register();

View File

@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
class AutoloaderTest extends TestCase
{
public function testClassExists() {
$this->assertTrue(class_exists('PhpParser\NodeVisitorAbstract'));
$this->assertTrue(class_exists(NodeVisitorAbstract::class));
$this->assertFalse(class_exists('PHPParser_NodeVisitor_NameResolver'));
$this->assertFalse(class_exists('PhpParser\FooBar'));

View File

@ -24,7 +24,7 @@ class InterfaceTest extends TestCase
public function testEmpty() {
$contract = $this->builder->getNode();
$this->assertInstanceOf('PhpParser\Node\Stmt\Interface_', $contract);
$this->assertInstanceOf(Stmt\Interface_::class, $contract);
$this->assertEquals(new Node\Identifier('Contract'), $contract->name);
}
@ -65,8 +65,8 @@ class InterfaceTest extends TestCase
->getNode()
;
$this->assertInstanceOf('PhpParser\Node\Stmt\ClassConst', $contract->stmts[0]);
$this->assertInstanceOf('PhpParser\Node\Stmt\ClassMethod', $contract->stmts[1]);
$this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
$this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
}
public function testDocComment() {
@ -103,4 +103,3 @@ class InterfaceTest extends TestCase
$this->assertTrue(interface_exists('Contract', false));
}
}

View File

@ -2,6 +2,7 @@
namespace PhpParser;
use PhpParser\Builder;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
@ -20,15 +21,15 @@ class BuilderFactoryTest extends TestCase
public function provideTestFactory() {
return [
['namespace', 'PhpParser\Builder\Namespace_'],
['class', 'PhpParser\Builder\Class_'],
['interface', 'PhpParser\Builder\Interface_'],
['trait', 'PhpParser\Builder\Trait_'],
['method', 'PhpParser\Builder\Method'],
['function', 'PhpParser\Builder\Function_'],
['property', 'PhpParser\Builder\Property'],
['param', 'PhpParser\Builder\Param'],
['use', 'PhpParser\Builder\Use_'],
['namespace', Builder\Namespace_::class],
['class', Builder\Class_::class],
['interface', Builder\Interface_::class],
['trait', Builder\Trait_::class],
['method', Builder\Method::class],
['function', Builder\Function_::class],
['property', Builder\Property::class],
['param', Builder\Param::class],
['use', Builder\Use_::class],
];
}

View File

@ -157,7 +157,7 @@ class NodeAbstractTest extends TestCase
public function testAttributes() {
/** @var $node Node */
$node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
$node = $this->getMockForAbstractClass(NodeAbstract::class);
$this->assertEmpty($node->getAttributes());

View File

@ -4,6 +4,7 @@ namespace PhpParser;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitor;
use PHPUnit\Framework\TestCase;
class NodeTraverserTest extends TestCase
@ -14,7 +15,7 @@ class NodeTraverserTest extends TestCase
$echoNode = new Node\Stmt\Echo_([$str1Node, $str2Node]);
$stmts = [$echoNode];
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(0))->method('beforeTraverse')->with($stmts);
$visitor->expects($this->at(1))->method('enterNode')->with($echoNode);
@ -37,8 +38,8 @@ class NodeTraverserTest extends TestCase
$printNode = new Expr\Print_($str1Node);
// first visitor changes the node, second verifies the change
$visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
// replace empty statements with string1 node
$visitor1->expects($this->at(0))->method('beforeTraverse')->with([])
@ -82,7 +83,7 @@ class NodeTraverserTest extends TestCase
$str1Node = new String_('Foo');
$str2Node = new String_('Bar');
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
// remove the string1 node, leave the string2 node
$visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
@ -101,7 +102,7 @@ class NodeTraverserTest extends TestCase
$strR1 = new String_('Replacement 1');
$strR2 = new String_('Replacement 2');
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
// replace strMiddle with strR1 and strR2 by merge
$visitor->expects($this->at(4))->method('leaveNode')->with($strMiddle)
@ -136,8 +137,8 @@ class NodeTraverserTest extends TestCase
$negNode = new Expr\UnaryMinus($mulNode);
$stmts = [$printNode, $negNode];
$visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor1->expects($this->at(1))->method('enterNode')->with($printNode)
->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CHILDREN));
@ -175,7 +176,7 @@ class NodeTraverserTest extends TestCase
$stmts = [$mulNode, $printNode];
// From enterNode() with array parent
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(1))->method('enterNode')->with($mulNode)
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
$visitor->expects($this->at(2))->method('afterTraverse');
@ -184,7 +185,7 @@ class NodeTraverserTest extends TestCase
$this->assertEquals($stmts, $traverser->traverse($stmts));
// From enterNode with Node parent
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(2))->method('enterNode')->with($varNode1)
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
$visitor->expects($this->at(3))->method('afterTraverse');
@ -193,7 +194,7 @@ class NodeTraverserTest extends TestCase
$this->assertEquals($stmts, $traverser->traverse($stmts));
// From leaveNode with Node parent
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(3))->method('leaveNode')->with($varNode1)
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
$visitor->expects($this->at(4))->method('afterTraverse');
@ -202,7 +203,7 @@ class NodeTraverserTest extends TestCase
$this->assertEquals($stmts, $traverser->traverse($stmts));
// From leaveNode with array parent
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
$visitor->expects($this->at(7))->method('afterTraverse');
@ -211,7 +212,7 @@ class NodeTraverserTest extends TestCase
$this->assertEquals($stmts, $traverser->traverse($stmts));
// Check that pending array modifications are still carried out
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
->will($this->returnValue(NodeTraverser::REMOVE_NODE));
$visitor->expects($this->at(7))->method('enterNode')->with($printNode)
@ -224,9 +225,9 @@ class NodeTraverserTest extends TestCase
}
public function testRemovingVisitor() {
$visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor3 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor3 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$traverser = new NodeTraverser;
$traverser->addVisitor($visitor1);
@ -265,27 +266,27 @@ class NodeTraverserTest extends TestCase
}
public function provideTestInvalidReturn() {
$visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor1->expects($this->at(1))->method('enterNode')
->will($this->returnValue('foobar'));
$visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor2->expects($this->at(2))->method('enterNode')
->will($this->returnValue('foobar'));
$visitor3 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor3 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor3->expects($this->at(3))->method('leaveNode')
->will($this->returnValue('foobar'));
$visitor4 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor4 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor4->expects($this->at(4))->method('leaveNode')
->will($this->returnValue('foobar'));
$visitor5 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor5 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor5->expects($this->at(3))->method('leaveNode')
->willReturn([new Node\Scalar\DNumber(42.0)]);
$visitor6 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor6 = $this->getMockBuilder(NodeVisitor::class)->getMock();
$visitor6->expects($this->at(4))->method('leaveNode')
->willReturn(false);

View File

@ -347,7 +347,7 @@ EOC;
* @dataProvider provideTestError
*/
public function testError(Node $stmt, $errorMsg) {
$this->expectException('PhpParser\Error');
$this->expectException(\PhpParser\Error::class);
$this->expectExceptionMessage($errorMsg);
$traverser = new PhpParser\NodeTraverser;

View File

@ -79,14 +79,14 @@ class MultipleTest extends ParserTest
}
public function testThrownError() {
$this->expectException('PhpParser\Error');
$this->expectException(Error::class);
$this->expectExceptionMessage('FAIL A');
$parserA = $this->getMockBuilder('PhpParser\Parser')->getMock();
$parserA = $this->getMockBuilder(\PhpParser\Parser::class)->getMock();
$parserA->expects($this->at(0))
->method('parse')->will($this->throwException(new Error('FAIL A')));
$parserB = $this->getMockBuilder('PhpParser\Parser')->getMock();
$parserB = $this->getMockBuilder(\PhpParser\Parser::class)->getMock();
$parserB->expects($this->at(0))
->method('parse')->will($this->throwException(new Error('FAIL B')));

View File

@ -18,19 +18,19 @@ class ParserFactoryTest extends TestCase
return [
[
ParserFactory::PREFER_PHP7, $lexer,
'PhpParser\Parser\Multiple'
Parser\Multiple::class
],
[
ParserFactory::PREFER_PHP5, null,
'PhpParser\Parser\Multiple'
Parser\Multiple::class
],
[
ParserFactory::ONLY_PHP7, null,
'PhpParser\Parser\Php7'
Parser\Php7::class
],
[
ParserFactory::ONLY_PHP5, $lexer,
'PhpParser\Parser\Php5'
Parser\Php5::class
]
];
}

View File

@ -4,6 +4,7 @@ namespace PhpParser;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Scalar\String_;
use PHPUnit\Framework\TestCase;
@ -61,9 +62,9 @@ EOC;
$parser = $this->getParser($lexer);
$stmts = $parser->parse($code);
/** @var \PhpParser\Node\Stmt\Function_ $fn */
/** @var Stmt\Function_ $fn */
$fn = $stmts[0];
$this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
$this->assertInstanceOf(Stmt\Function_::class, $fn);
$this->assertEquals([
'comments' => [
new Comment\Doc('/** Doc comment */', 2, 6, 1),
@ -75,7 +76,7 @@ EOC;
], $fn->getAttributes());
$param = $fn->params[0];
$this->assertInstanceOf('PhpParser\Node\Param', $param);
$this->assertInstanceOf(Node\Param::class, $param);
$this->assertEquals([
'startLine' => 3,
'endLine' => 3,
@ -83,9 +84,9 @@ EOC;
'endTokenPos' => 7,
], $param->getAttributes());
/** @var \PhpParser\Node\Stmt\Echo_ $echo */
/** @var Stmt\Echo_ $echo */
$echo = $fn->stmts[0];
$this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
$this->assertInstanceOf(Stmt\Echo_::class, $echo);
$this->assertEquals([
'comments' => [
new Comment("// Line\n", 4, 49, 12),
@ -99,7 +100,7 @@ EOC;
/** @var \PhpParser\Node\Expr\Variable $var */
$var = $echo->exprs[0];
$this->assertInstanceOf('PhpParser\Node\Expr\Variable', $var);
$this->assertInstanceOf(Expr\Variable::class, $var);
$this->assertEquals([
'startLine' => 6,
'endLine' => 6,
@ -124,7 +125,7 @@ EOC;
public function testExtraAttributes($code, $expectedAttributes) {
$parser = $this->getParser(new Lexer);
$stmts = $parser->parse("<?php $code;");
$node = $stmts[0] instanceof Node\Stmt\Expression ? $stmts[0]->expr : $stmts[0];
$node = $stmts[0] instanceof Stmt\Expression ? $stmts[0]->expr : $stmts[0];
$attributes = $node->getAttributes();
foreach ($expectedAttributes as $name => $value) {
$this->assertSame($value, $attributes[$name]);
@ -168,9 +169,9 @@ EOC;
["exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]],
["?>Foo", ['hasLeadingNewline' => false]],
["?>\nFoo", ['hasLeadingNewline' => true]],
["namespace Foo;", ['kind' => Node\Stmt\Namespace_::KIND_SEMICOLON]],
["namespace Foo {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]],
["namespace {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]],
["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]],
["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
];
}
}