From a682a74435c5d8d0623c2029cff9b0b6d4d584a9 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Sat, 5 Nov 2016 00:00:11 -0400 Subject: [PATCH 1/6] Update for PHP 7 Support Changes Cli and Analyser to php-parser 2.0 which supports PHP 7 parsing. --- composer.json | 2 +- src/PhpAssumptions/Analyser.php | 4 ++-- src/PhpAssumptions/Cli.php | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index ddfb2b8..8c2afbf 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "nikic/php-parser": "^1.0", + "nikic/php-parser": "^2.0", "league/climate": "^3.1" }, "require-dev": { diff --git a/src/PhpAssumptions/Analyser.php b/src/PhpAssumptions/Analyser.php index 0565cc8..88c2a20 100644 --- a/src/PhpAssumptions/Analyser.php +++ b/src/PhpAssumptions/Analyser.php @@ -5,7 +5,7 @@ use PhpAssumptions\Output\Result; use PhpParser\Node; use PhpParser\NodeTraverserInterface; -use PhpParser\ParserAbstract; +use PhpParser\Parser\Multiple; class Analyser { @@ -39,7 +39,7 @@ class Analyser * @param NodeTraverserInterface $nodeTraverser */ public function __construct( - ParserAbstract $parser, + Multiple $parser, NodeTraverserInterface $nodeTraverser ) { $this->parser = $parser; diff --git a/src/PhpAssumptions/Cli.php b/src/PhpAssumptions/Cli.php index eda2800..01b1d45 100644 --- a/src/PhpAssumptions/Cli.php +++ b/src/PhpAssumptions/Cli.php @@ -6,9 +6,8 @@ use PhpAssumptions\Output\PrettyOutput; use PhpAssumptions\Output\XmlOutput; use PhpAssumptions\Parser\NodeVisitor; -use PhpParser\Lexer; use PhpParser\NodeTraverser; -use PhpParser\Parser; +use PhpParser\ParserFactory; class Cli { @@ -19,6 +18,11 @@ class Cli */ private $cli; + private function createParser() { + $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + return $parser; + } + public function __construct(CLImate $cli) { $this->cli = $cli; @@ -40,6 +44,7 @@ public function __construct(CLImate $cli) 'defaultValue' => 'phpa.xml', ], ]); + $this->parser = self::createParser(); } /** @@ -68,7 +73,7 @@ public function handle(array $args) $nodeTraverser = new NodeTraverser(); $analyser = new Analyser( - new Parser(new Lexer()), + $this->parser, $nodeTraverser ); From 2ea273b76a31cd931fb7da262d8ba7425c844900 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Sat, 5 Nov 2016 00:03:17 -0400 Subject: [PATCH 2/6] Update all Tests to fix errors I was getting errors when running phpunit and was seeing the following: "Class 'Prophecy\PhpUnit\ProphecyTestCase' not found" --- composer.json | 1 - tests/PhpAssumptions/AnalyserTest.php | 3 +-- tests/PhpAssumptions/CliTest.php | 3 +-- tests/PhpAssumptions/DetectorTest.php | 3 +-- tests/PhpAssumptions/ExampleTest.php | 3 +-- tests/PhpAssumptions/Output/PrettyOutputTest.php | 3 +-- tests/PhpAssumptions/Output/XmlOutputTest.php | 3 +-- tests/PhpAssumptions/Parser/NodeVisitorTest.php | 3 +-- 8 files changed, 7 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 8c2afbf..06006aa 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,6 @@ }, "require-dev": { "phpunit/phpunit": "^4.8", - "phpspec/prophecy-phpunit": "^1.1", "henrikbjorn/phpspec-code-coverage": "^1.0" }, "autoload": { diff --git a/tests/PhpAssumptions/AnalyserTest.php b/tests/PhpAssumptions/AnalyserTest.php index 0122474..9b00bad 100644 --- a/tests/PhpAssumptions/AnalyserTest.php +++ b/tests/PhpAssumptions/AnalyserTest.php @@ -8,9 +8,8 @@ use PhpParser\NodeTraverser; use PhpParser\Parser; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTestCase; -class AnalyserTest extends ProphecyTestCase +class AnalyserTest extends \PHPUnit_Framework_TestCase { /** * @var Parser diff --git a/tests/PhpAssumptions/CliTest.php b/tests/PhpAssumptions/CliTest.php index 2622eab..a16105b 100644 --- a/tests/PhpAssumptions/CliTest.php +++ b/tests/PhpAssumptions/CliTest.php @@ -6,9 +6,8 @@ use League\CLImate\CLImate; use PhpAssumptions\Cli; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTestCase; -class CliTest extends ProphecyTestCase +class CliTest extends \PHPUnit_Framework_TestCase { /** * @var Cli diff --git a/tests/PhpAssumptions/DetectorTest.php b/tests/PhpAssumptions/DetectorTest.php index 55482a1..0fdb62b 100644 --- a/tests/PhpAssumptions/DetectorTest.php +++ b/tests/PhpAssumptions/DetectorTest.php @@ -5,9 +5,8 @@ use PhpAssumptions\Detector; use PhpParser\Lexer; use PhpParser\Parser; -use Prophecy\PhpUnit\ProphecyTestCase; -class NodeVisitorTest extends ProphecyTestCase +class NodeVisitorTest extends \PHPUnit_Framework_TestCase { /** * @var Parser diff --git a/tests/PhpAssumptions/ExampleTest.php b/tests/PhpAssumptions/ExampleTest.php index 38c4804..87b88e9 100644 --- a/tests/PhpAssumptions/ExampleTest.php +++ b/tests/PhpAssumptions/ExampleTest.php @@ -9,9 +9,8 @@ use PhpParser\NodeTraverser; use PhpParser\Parser; use PhpParser\PrettyPrinter\Standard; -use Prophecy\PhpUnit\ProphecyTestCase; -class ExampleTest extends ProphecyTestCase +class ExampleTest extends \PHPUnit_Framework_TestCase { /** * @var Analyser diff --git a/tests/PhpAssumptions/Output/PrettyOutputTest.php b/tests/PhpAssumptions/Output/PrettyOutputTest.php index 2ef7511..3e4189f 100644 --- a/tests/PhpAssumptions/Output/PrettyOutputTest.php +++ b/tests/PhpAssumptions/Output/PrettyOutputTest.php @@ -6,9 +6,8 @@ use PhpAssumptions\Output\PrettyOutput; use PhpAssumptions\Output\Result; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTestCase; -class PrettyOutputTest extends ProphecyTestCase +class PrettyOutputTest extends \PHPUnit_Framework_TestCase { /** * @var PrettyOutput diff --git a/tests/PhpAssumptions/Output/XmlOutputTest.php b/tests/PhpAssumptions/Output/XmlOutputTest.php index 7cec459..7ba6d0c 100644 --- a/tests/PhpAssumptions/Output/XmlOutputTest.php +++ b/tests/PhpAssumptions/Output/XmlOutputTest.php @@ -5,10 +5,9 @@ use League\CLImate\CLImate; use PhpAssumptions\Cli; use PhpAssumptions\Output\Result; -use Prophecy\PhpUnit\ProphecyTestCase; use PhpAssumptions\Output\XmlOutput; -class XmlOutputTest extends ProphecyTestCase +class XmlOutputTest extends \PHPUnit_Framework_TestCase { /** * @var XmlOutput diff --git a/tests/PhpAssumptions/Parser/NodeVisitorTest.php b/tests/PhpAssumptions/Parser/NodeVisitorTest.php index edd4f37..e3a3586 100644 --- a/tests/PhpAssumptions/Parser/NodeVisitorTest.php +++ b/tests/PhpAssumptions/Parser/NodeVisitorTest.php @@ -8,9 +8,8 @@ use PhpParser\Node; use PhpParser\PrettyPrinter\Standard; use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTestCase; -class NodeVisitorTest extends ProphecyTestCase +class NodeVisitorTest extends \PHPUnit_Framework_TestCase { /** * @var NodeVisitor From 2f877888be3007a283543e2ca433a81039c7cb57 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Sat, 5 Nov 2016 00:14:18 -0400 Subject: [PATCH 3/6] Apply PSR2 coding styles --- src/PhpAssumptions/Analyser.php | 2 +- src/PhpAssumptions/Cli.php | 9 ++++++--- src/PhpAssumptions/Detector.php | 2 +- src/PhpAssumptions/Output/PrettyOutput.php | 14 ++++++++------ src/PhpAssumptions/Output/Result.php | 2 +- src/PhpAssumptions/Output/XmlOutput.php | 2 +- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/PhpAssumptions/Analyser.php b/src/PhpAssumptions/Analyser.php index 88c2a20..8f5a85c 100644 --- a/src/PhpAssumptions/Analyser.php +++ b/src/PhpAssumptions/Analyser.php @@ -35,7 +35,7 @@ class Analyser private $result; /** - * @param ParserAbstract $parser + * @param ParserAbstract $parser * @param NodeTraverserInterface $nodeTraverser */ public function __construct( diff --git a/src/PhpAssumptions/Cli.php b/src/PhpAssumptions/Cli.php index 01b1d45..4ba8ec4 100644 --- a/src/PhpAssumptions/Cli.php +++ b/src/PhpAssumptions/Cli.php @@ -18,7 +18,8 @@ class Cli */ private $cli; - private function createParser() { + private function createParser() + { $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); return $parser; } @@ -26,7 +27,8 @@ private function createParser() { public function __construct(CLImate $cli) { $this->cli = $cli; - $this->cli->arguments->add([ + $this->cli->arguments->add( + [ 'path' => [ 'description' => 'The path to analyse', 'required' => true, @@ -43,7 +45,8 @@ public function __construct(CLImate $cli) 'description' => 'Output file', 'defaultValue' => 'phpa.xml', ], - ]); + ] + ); $this->parser = self::createParser(); } diff --git a/src/PhpAssumptions/Detector.php b/src/PhpAssumptions/Detector.php index a3b1677..87e6e50 100644 --- a/src/PhpAssumptions/Detector.php +++ b/src/PhpAssumptions/Detector.php @@ -81,7 +81,7 @@ private function isVariableExpression(Node $node) } /** - * @param Expr $condition + * @param Expr $condition * @param string $left * @param string $right * @return bool diff --git a/src/PhpAssumptions/Output/PrettyOutput.php b/src/PhpAssumptions/Output/PrettyOutput.php index caa86d3..26f2ce4 100644 --- a/src/PhpAssumptions/Output/PrettyOutput.php +++ b/src/PhpAssumptions/Output/PrettyOutput.php @@ -28,11 +28,13 @@ public function output(Result $result) $this->cli->table($result->getAssumptions())->br(); } - $this->cli->out(sprintf( - '%d out of %d boolean expressions are assumptions (%d%%)', - $result->getAssumptionsCount(), - $result->getBoolExpressionsCount(), - $result->getPercentage() - )); + $this->cli->out( + sprintf( + '%d out of %d boolean expressions are assumptions (%d%%)', + $result->getAssumptionsCount(), + $result->getBoolExpressionsCount(), + $result->getPercentage() + ) + ); } } diff --git a/src/PhpAssumptions/Output/Result.php b/src/PhpAssumptions/Output/Result.php index 0e26039..24dad37 100644 --- a/src/PhpAssumptions/Output/Result.php +++ b/src/PhpAssumptions/Output/Result.php @@ -16,7 +16,7 @@ class Result /** * @param string $file - * @param int $line + * @param int $line * @param string $message */ public function addAssumption($file, $line, $message) diff --git a/src/PhpAssumptions/Output/XmlOutput.php b/src/PhpAssumptions/Output/XmlOutput.php index 58e758c..928aee6 100644 --- a/src/PhpAssumptions/Output/XmlOutput.php +++ b/src/PhpAssumptions/Output/XmlOutput.php @@ -29,7 +29,7 @@ class XmlOutput implements OutputInterface /** * @param CLImate $cli - * @param string $file + * @param string $file */ public function __construct(CLImate $cli, $file) { From 25ba4c4f7e9056e7a92760ca20655dee2d0d830b Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Sat, 5 Nov 2016 00:23:44 -0400 Subject: [PATCH 4/6] Update phpunit to fix testing --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 06006aa..421bc1e 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "league/climate": "^3.1" }, "require-dev": { - "phpunit/phpunit": "^4.8", + "phpunit/phpunit": "^5.6", "henrikbjorn/phpspec-code-coverage": "^1.0" }, "autoload": { From 6112c12f09cd51f343f75b3eb59a5fc9a48e7477 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Sat, 5 Nov 2016 01:29:46 -0400 Subject: [PATCH 5/6] Fix errors in tests Well somehow running with the globally installed phpunit I wasn't seeing errors. running with vendor phpunit and I was seeing errors. First I tried an update to local phpunit, fix a few things and now there's only a single error left in the tests. While I was able to fix the issue I'm far too naiev to how this and php-parser package function to write a proper fix for the test. Also, since I have never used phrophecy with my unit tests, I'm a bit unfamiliar with how to use that correctly. --- composer.json | 2 +- tests/PhpAssumptions/AnalyserTest.php | 9 +++++---- tests/PhpAssumptions/DetectorTest.php | 5 ++--- tests/PhpAssumptions/ExampleTest.php | 5 ++--- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 421bc1e..e7d963e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ }, "require-dev": { "phpunit/phpunit": "^5.6", - "henrikbjorn/phpspec-code-coverage": "^1.0" + "phpspec/prophecy": "~1.0" }, "autoload": { "psr-4": { diff --git a/tests/PhpAssumptions/AnalyserTest.php b/tests/PhpAssumptions/AnalyserTest.php index 9b00bad..fca2676 100644 --- a/tests/PhpAssumptions/AnalyserTest.php +++ b/tests/PhpAssumptions/AnalyserTest.php @@ -6,7 +6,7 @@ use PhpAssumptions\Output\OutputInterface; use PhpParser\Node; use PhpParser\NodeTraverser; -use PhpParser\Parser; +use PhpParser\ParserFactory; use Prophecy\Argument; class AnalyserTest extends \PHPUnit_Framework_TestCase @@ -39,11 +39,11 @@ class AnalyserTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->node = $this->prophesize(Node::class); - $this->parser = $this->prophesize(Parser::class); + $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $this->output = $this->prophesize(OutputInterface::class); $this->nodeTraverser = $this->prophesize(NodeTraverser::class); $this->analyser = new Analyser( - $this->parser->reveal(), + $this->parser, $this->nodeTraverser->reveal(), $this->output->reveal() ); @@ -57,7 +57,8 @@ public function itShouldAnalyseAllFiles() $files = [fixture('MyClass.php')]; $nodes = [$this->node]; - $this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes); + $parseRes = $this->parser->parse(Argument::type('string')); + //$this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes); $this->nodeTraverser->traverse($nodes)->shouldBeCalled(); diff --git a/tests/PhpAssumptions/DetectorTest.php b/tests/PhpAssumptions/DetectorTest.php index 0fdb62b..40e1706 100644 --- a/tests/PhpAssumptions/DetectorTest.php +++ b/tests/PhpAssumptions/DetectorTest.php @@ -3,8 +3,7 @@ namespace tests\PhpAssumptions; use PhpAssumptions\Detector; -use PhpParser\Lexer; -use PhpParser\Parser; +use PhpParser\ParserFactory; class NodeVisitorTest extends \PHPUnit_Framework_TestCase { @@ -20,7 +19,7 @@ class NodeVisitorTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->parser = new Parser(new Lexer()); + $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $this->detector = new Detector(); } diff --git a/tests/PhpAssumptions/ExampleTest.php b/tests/PhpAssumptions/ExampleTest.php index 87b88e9..a79c829 100644 --- a/tests/PhpAssumptions/ExampleTest.php +++ b/tests/PhpAssumptions/ExampleTest.php @@ -5,9 +5,8 @@ use PhpAssumptions\Analyser; use PhpAssumptions\Detector; use PhpAssumptions\Parser\NodeVisitor; -use PhpParser\Lexer; use PhpParser\NodeTraverser; -use PhpParser\Parser; +use PhpParser\ParserFactory; use PhpParser\PrettyPrinter\Standard; class ExampleTest extends \PHPUnit_Framework_TestCase @@ -20,7 +19,7 @@ class ExampleTest extends \PHPUnit_Framework_TestCase public function setUp() { $nodeTraverser = new NodeTraverser(); - $this->analyser = new Analyser(new Parser(new Lexer()), $nodeTraverser); + $this->analyser = new Analyser((new ParserFactory)->create(ParserFactory::PREFER_PHP7), $nodeTraverser); $nodeTraverser->addVisitor(new NodeVisitor($this->analyser, new Detector())); } From ea21d0f6f0f7ed0197cf9eac505af01748a11240 Mon Sep 17 00:00:00 2001 From: Daniel Pock Date: Tue, 15 Nov 2016 13:24:04 -0500 Subject: [PATCH 6/6] Fix test bootstrap issues --- tests/PhpAssumptions/AnalyserTest.php | 8 ++++---- tests/bootstrap.php | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/PhpAssumptions/AnalyserTest.php b/tests/PhpAssumptions/AnalyserTest.php index fca2676..36a0e57 100644 --- a/tests/PhpAssumptions/AnalyserTest.php +++ b/tests/PhpAssumptions/AnalyserTest.php @@ -4,9 +4,9 @@ use PhpAssumptions\Analyser; use PhpAssumptions\Output\OutputInterface; +use PhpParser\Parser\Multiple; use PhpParser\Node; use PhpParser\NodeTraverser; -use PhpParser\ParserFactory; use Prophecy\Argument; class AnalyserTest extends \PHPUnit_Framework_TestCase @@ -39,11 +39,11 @@ class AnalyserTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->node = $this->prophesize(Node::class); - $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + $this->parser = $this->prophesize(Multiple::class); $this->output = $this->prophesize(OutputInterface::class); $this->nodeTraverser = $this->prophesize(NodeTraverser::class); $this->analyser = new Analyser( - $this->parser, + $this->parser->reveal(), $this->nodeTraverser->reveal(), $this->output->reveal() ); @@ -58,7 +58,7 @@ public function itShouldAnalyseAllFiles() $nodes = [$this->node]; $parseRes = $this->parser->parse(Argument::type('string')); - //$this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes); + $this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes); $this->nodeTraverser->traverse($nodes)->shouldBeCalled(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 903ceed..18d703d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,6 @@