From 38d205a056f3ebe80499732e1ad347c4a9efd6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 17:55:07 +0100 Subject: [PATCH 01/50] added some base code to test the fixers --- phpunit.xml | 26 +++++----- tests/bootstrap.php | 66 +++++++++++++------------ tests/unit/Fixers/FixerUnitTestCase.php | 41 +++++++++++++++ 3 files changed, 89 insertions(+), 44 deletions(-) create mode 100644 tests/unit/Fixers/FixerUnitTestCase.php diff --git a/phpunit.xml b/phpunit.xml index ffbc9f0..8cf7806 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,19 @@ + backupStaticAttributes="false" + bootstrap="tests/bootstrap.php" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="false" + stopOnError="false" + stopOnFailure="false" + syntaxCheck="true" + verbose="true"> - - tests/ + + ./tests - + \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index bbc90f2..209c198 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,44 +1,46 @@ $value) { - if (is_numeric($option)) { - $arguments .= ' '.escapeshellarg($value); - } else { - $arguments .= " $option=".escapeshellarg($value); - } - } +// $arguments = ''; +// foreach ($args as $option => $value) { +// if (is_numeric($option)) { +// $arguments .= ' '.escapeshellarg($value); +// } else { +// $arguments .= " $option=".escapeshellarg($value); +// } +// } - $cmd = sprintf('php %s %s -o=- -', $path_to_phpfmt, $arguments); +// $cmd = sprintf('php %s %s -o=- -', $path_to_phpfmt, $arguments); - $descriptorspec = array( - 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from - 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to - ); +// $descriptorspec = array( +// 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from +// 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to +// ); - $process = proc_open($cmd, $descriptorspec, $pipes); +// $process = proc_open($cmd, $descriptorspec, $pipes); - if (is_resource($process)) { - // $pipes now looks like this: - // 0 => writeable handle connected to child stdin - // 1 => readable handle connected to child stdout +// if (is_resource($process)) { +// // $pipes now looks like this: +// // 0 => writeable handle connected to child stdin +// // 1 => readable handle connected to child stdout - fwrite($pipes[0], $stdin); // file_get_contents('php://stdin') - fclose($pipes[0]); +// fwrite($pipes[0], $stdin); // file_get_contents('php://stdin') +// fclose($pipes[0]); - $stdout = stream_get_contents($pipes[1]); - fclose($pipes[1]); +// $stdout = stream_get_contents($pipes[1]); +// fclose($pipes[1]); - // It is important that you close any pipes before calling - // proc_close in order to avoid a deadlock - $return_value = proc_close($process); +// // It is important that you close any pipes before calling +// // proc_close in order to avoid a deadlock +// $return_value = proc_close($process); - return $stdout; - } -} +// return $stdout; +// } +// } + +require __DIR__.'/../vendor/autoload.php'; \ No newline at end of file diff --git a/tests/unit/Fixers/FixerUnitTestCase.php b/tests/unit/Fixers/FixerUnitTestCase.php new file mode 100644 index 0000000..41099a1 --- /dev/null +++ b/tests/unit/Fixers/FixerUnitTestCase.php @@ -0,0 +1,41 @@ +getToken($token); + $foundTokens[$id] = $id; + if (T_COMMENT === $id) { + $commentStack[] = [$id, $text]; + } + } + + return [$foundTokens, $commentStack]; + } +} \ No newline at end of file From 6bff7becae76d364d905f678c368107c14c1fe56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 17:55:40 +0100 Subject: [PATCH 02/50] A couple of fixers' tests --- .../unit/Fixers/PSR1/BOMMark/BOMMarkTest.php | 31 ++++++++++++++++ .../Fixers/PSR1/BOMMark/expectedResult.php | 13 +++++++ .../unit/Fixers/PSR1/BOMMark/fileWithBOM.php | 13 +++++++ .../ClassConstants/ClassConstantsTest.php | 35 +++++++++++++++++++ .../PSR1/ClassConstants/expectedResult.php | 13 +++++++ .../PSR1/ClassConstants/fileWithConstants.php | 13 +++++++ .../ClassConstants/fileWithoutConstants.php | 10 ++++++ 7 files changed, 128 insertions(+) create mode 100644 tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php create mode 100644 tests/unit/Fixers/PSR1/BOMMark/expectedResult.php create mode 100644 tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php create mode 100644 tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php create mode 100644 tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php create mode 100644 tests/unit/Fixers/PSR1/ClassConstants/fileWithConstants.php create mode 100644 tests/unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php diff --git a/tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php b/tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php new file mode 100644 index 0000000..6983ce6 --- /dev/null +++ b/tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php @@ -0,0 +1,31 @@ +getSource(__DIR__ . '\fileWithBOM.php'); + $sourceFalse = $this->getSource(__DIR__ . '\expectedResult.php'); + + $class = new BOMMark; + $this->assertTrue($class->candidate($sourceTrue, null)); + $this->assertFalse($class->candidate($sourceFalse, null)); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '\fileWithBOM.php'); + $expected = $this->getSource(__DIR__ . '\expectedResult.php'); + + $class = new BOMMark; + $fixedSource = $class->format($source); + + $this->assertFalse($source === $expected); + $this->assertTrue($fixedSource === $expected); + } +} diff --git a/tests/unit/Fixers/PSR1/BOMMark/expectedResult.php b/tests/unit/Fixers/PSR1/BOMMark/expectedResult.php new file mode 100644 index 0000000..ea88506 --- /dev/null +++ b/tests/unit/Fixers/PSR1/BOMMark/expectedResult.php @@ -0,0 +1,13 @@ +something = $something; + } +} diff --git a/tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php b/tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php new file mode 100644 index 0000000..9fd370a --- /dev/null +++ b/tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php @@ -0,0 +1,13 @@ +something = $something; + } +} diff --git a/tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php b/tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php new file mode 100644 index 0000000..d36bcc0 --- /dev/null +++ b/tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php @@ -0,0 +1,35 @@ +getSource(__DIR__ . '\fileWithConstants.php'); + $sourceFalse = $this->getSource(__DIR__ . '\fileWithoutConstants.php'); + + $tokensSourceTrue = $this->getTokens($sourceTrue); + $tokensSourceFalse = $this->getTokens($sourceFalse); + + $class = new ClassConstants; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + $this->assertFalse($class->candidate($sourceFalse, $tokensSourceFalse[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '\fileWithConstants.php'); + $expected = $this->getSource(__DIR__ . '\expectedResult.php'); + + $class = new ClassConstants; + $fixedSource = $class->format($source); + + $this->assertFalse($source === $expected); + $this->assertTrue($fixedSource === $expected); + } +} diff --git a/tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php b/tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php new file mode 100644 index 0000000..4670ff9 --- /dev/null +++ b/tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php @@ -0,0 +1,13 @@ + Date: Fri, 10 Mar 2017 17:56:20 +0100 Subject: [PATCH 03/50] do we really need to check for strings on the candidate method? --- src/Fixers/PSR1/ClassConstants.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fixers/PSR1/ClassConstants.php b/src/Fixers/PSR1/ClassConstants.php index b38af2b..f4dd067 100644 --- a/src/Fixers/PSR1/ClassConstants.php +++ b/src/Fixers/PSR1/ClassConstants.php @@ -12,7 +12,7 @@ class ClassConstants extends FormatterPass implements FixerInterface { public function candidate($source, $foundTokens) { - if (isset($foundTokens[T_CONST]) || isset($foundTokens[T_STRING])) { + if (isset($foundTokens[T_CONST])) { return true; } return false; From 0a8031f088623685f117c8669a6ce443fc492b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:07:18 +0100 Subject: [PATCH 04/50] added travis-ci? --- .travis.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..809f7ba --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: php + +php: + - 5.6 + - 7.0 + - 7.1 + +sudo: false + +cache: + directories: + - $HOME/.composer/cache + +before_install: + - if [[ $TRAVIS_PHP_VERSION != 7.1 ]] ; then phpenv config-rm xdebug.ini; fi + - travis_retry composer self-update + +install: + - travis_retry composer install --prefer-dist --no-interaction --prefer-dist --no-suggest + +script: vendor/bin/phpunit \ No newline at end of file From a2062ef1b38fe18773bc98a61d417f043d61b232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:09:22 +0100 Subject: [PATCH 05/50] added coveralls config --- .coveralls.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..52bdddb --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,2 @@ +service_name: travis-pro +repo_token: Seu3POyFuJwfEFFwNRtwdYlHIwcedZPed \ No newline at end of file From 2fa327fd3a6894c0965cbcf856bd7094b50f75dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:12:25 +0100 Subject: [PATCH 06/50] removed 5.6 from travis, removed old tests --- tests/TestAlignPHPCode.php | 64 --------------------- tests/TestAlignPHPCode2.php | 73 ------------------------ tests/TestRemoveIncludeParentheses.php | 40 ------------- tests/TestSpaceAroundExclamationMark.php | 40 ------------- tests/TestSpaceAroundParentheses.php | 55 ------------------ 5 files changed, 272 deletions(-) delete mode 100644 tests/TestAlignPHPCode.php delete mode 100644 tests/TestAlignPHPCode2.php delete mode 100644 tests/TestRemoveIncludeParentheses.php delete mode 100644 tests/TestSpaceAroundExclamationMark.php delete mode 100644 tests/TestSpaceAroundParentheses.php diff --git a/tests/TestAlignPHPCode.php b/tests/TestAlignPHPCode.php deleted file mode 100644 index b880b07..0000000 --- a/tests/TestAlignPHPCode.php +++ /dev/null @@ -1,64 +0,0 @@ - -
- -
-CODE; - - public function testEnabled() - { - $output = executeCommand( - array( - '--passes' => 'AlignPHPCode', - ), - $this->code - ); - - $expected_result = <<<'CODE' - -
- -
-CODE; - - $this->assertContains($expected_result, $output); - } - - public function testDisabled() - { - $output = executeCommand( - array( - '--exclude' => 'AlignPHPCode', - ), - $this->code - ); - - $expected_result = <<<'CODE' - -
- -
-CODE; - - $this->assertContains($expected_result, $output); - } -} diff --git a/tests/TestAlignPHPCode2.php b/tests/TestAlignPHPCode2.php deleted file mode 100644 index b4421af..0000000 --- a/tests/TestAlignPHPCode2.php +++ /dev/null @@ -1,73 +0,0 @@ - -
- -
-CODE; - - public function testEnabled() - { - $output = executeCommand( - array( - '--passes' => 'AlignPHPCode2', - ), - $this->code - ); - - $expected_result = <<<'CODE' - -
- -
-CODE; - - $this->assertContains($expected_result, $output); - } - - public function testDisabled() - { - $output = executeCommand( - array( - '--exclude' => 'AlignPHPCode2', - ), - $this->code - ); - - $expected_result = <<<'CODE' - -
- -
-CODE; - - $this->assertContains($expected_result, $output); - } -} diff --git a/tests/TestRemoveIncludeParentheses.php b/tests/TestRemoveIncludeParentheses.php deleted file mode 100644 index aa25d3a..0000000 --- a/tests/TestRemoveIncludeParentheses.php +++ /dev/null @@ -1,40 +0,0 @@ - -CODE; - - public function testEnabled() - { - $output = executeCommand( - array( - '--passes' => 'RemoveIncludeParentheses', - ), - $this->code - ); - - $this->assertContains("include 'a.php';", $output); - $this->assertContains("require 'a.php';", $output); - } - - public function testDisabled() - { - $output = executeCommand( - array( - '--exclude' => 'RemoveIncludeParentheses', - ), - $this->code - ); - - $this->assertContains("include ('a.php');", $output); - $this->assertContains("require ('a.php');", $output); - } -} diff --git a/tests/TestSpaceAroundExclamationMark.php b/tests/TestSpaceAroundExclamationMark.php deleted file mode 100644 index de098b6..0000000 --- a/tests/TestSpaceAroundExclamationMark.php +++ /dev/null @@ -1,40 +0,0 @@ - -CODE; - - public function testEnabled() - { - $output = executeCommand( - array( - '--passes' => 'SpaceAroundExclamationMark', - ), - $this->code - ); - - $this->assertContains('if ( ! true)', $output); - $this->assertContains('if ( ! $foo)', $output); - } - - public function testDisabled() - { - $output = executeCommand( - array( - '--exclude' => 'SpaceAroundExclamationMark', - ), - $this->code - ); - - $this->assertContains('if (!true)', $output); - $this->assertContains('if (!$foo)', $output); - } -} diff --git a/tests/TestSpaceAroundParentheses.php b/tests/TestSpaceAroundParentheses.php deleted file mode 100644 index e01d298..0000000 --- a/tests/TestSpaceAroundParentheses.php +++ /dev/null @@ -1,55 +0,0 @@ - array( - 1, 2, - ), -); -?> -CODE; - - public function testEnabled() - { - $output = executeCommand( - array( - '--passes' => 'SpaceAroundParentheses', - ), - $this->code - ); - - $this->assertContains("foo( 'a.php' );", $output); - $this->assertContains('foo();', $output); - $this->assertContains( - <<<'CODE' -array( - 'a' => array( - 1, 2, - ), -); -CODE - , $output - ); - } - - public function testDisabled() - { - $output = executeCommand( - array( - '--exclude' => 'SpaceAroundParentheses', - ), - $this->code - ); - - $this->assertContains("foo('a.php');", $output); - $this->assertContains('foo();', $output); - } -} From 41692b805a6ecb07eaee515d6db7eee2184b01e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:14:41 +0100 Subject: [PATCH 07/50] modified composer.json to autoload-dev test classes --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index b911617..160e9df 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,11 @@ "Fmt\\": "./src/" } }, + "autoload-dev": { + "psr-4": { + "Fmt\\Tests\\": "tests/" + } + }, "require": { "illuminate/support": "^5.4" }, From dc0f03941d25b61c88ff9fb2d095bc7548030715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:34:09 +0100 Subject: [PATCH 08/50] removed 5.6 from travis --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 809f7ba..2f8a7a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.6 - 7.0 - 7.1 @@ -13,9 +12,8 @@ cache: before_install: - if [[ $TRAVIS_PHP_VERSION != 7.1 ]] ; then phpenv config-rm xdebug.ini; fi - - travis_retry composer self-update install: - - travis_retry composer install --prefer-dist --no-interaction --prefer-dist --no-suggest + - composer install --prefer-dist --no-interaction --no-suggest script: vendor/bin/phpunit \ No newline at end of file From 0046e4e16a3a031619194990636e4a86b6653fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:50:21 +0100 Subject: [PATCH 09/50] unit for Unit --- tests/{unit => Unit}/Fixers/FixerUnitTestCase.php | 0 tests/{unit => Unit}/Fixers/PSR1/BOMMark/BOMMarkTest.php | 0 tests/{unit => Unit}/Fixers/PSR1/BOMMark/expectedResult.php | 0 tests/{unit => Unit}/Fixers/PSR1/BOMMark/fileWithBOM.php | 0 .../Fixers/PSR1/ClassConstants/ClassConstantsTest.php | 0 .../{unit => Unit}/Fixers/PSR1/ClassConstants/expectedResult.php | 0 .../Fixers/PSR1/ClassConstants/fileWithConstants.php | 0 .../Fixers/PSR1/ClassConstants/fileWithoutConstants.php | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename tests/{unit => Unit}/Fixers/FixerUnitTestCase.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/BOMMark/BOMMarkTest.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/BOMMark/expectedResult.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/BOMMark/fileWithBOM.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/ClassConstants/ClassConstantsTest.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/ClassConstants/expectedResult.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/ClassConstants/fileWithConstants.php (100%) rename tests/{unit => Unit}/Fixers/PSR1/ClassConstants/fileWithoutConstants.php (100%) diff --git a/tests/unit/Fixers/FixerUnitTestCase.php b/tests/Unit/Fixers/FixerUnitTestCase.php similarity index 100% rename from tests/unit/Fixers/FixerUnitTestCase.php rename to tests/Unit/Fixers/FixerUnitTestCase.php diff --git a/tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php b/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php similarity index 100% rename from tests/unit/Fixers/PSR1/BOMMark/BOMMarkTest.php rename to tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php diff --git a/tests/unit/Fixers/PSR1/BOMMark/expectedResult.php b/tests/Unit/Fixers/PSR1/BOMMark/expectedResult.php similarity index 100% rename from tests/unit/Fixers/PSR1/BOMMark/expectedResult.php rename to tests/Unit/Fixers/PSR1/BOMMark/expectedResult.php diff --git a/tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php b/tests/Unit/Fixers/PSR1/BOMMark/fileWithBOM.php similarity index 100% rename from tests/unit/Fixers/PSR1/BOMMark/fileWithBOM.php rename to tests/Unit/Fixers/PSR1/BOMMark/fileWithBOM.php diff --git a/tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php b/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php similarity index 100% rename from tests/unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php rename to tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php diff --git a/tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php b/tests/Unit/Fixers/PSR1/ClassConstants/expectedResult.php similarity index 100% rename from tests/unit/Fixers/PSR1/ClassConstants/expectedResult.php rename to tests/Unit/Fixers/PSR1/ClassConstants/expectedResult.php diff --git a/tests/unit/Fixers/PSR1/ClassConstants/fileWithConstants.php b/tests/Unit/Fixers/PSR1/ClassConstants/fileWithConstants.php similarity index 100% rename from tests/unit/Fixers/PSR1/ClassConstants/fileWithConstants.php rename to tests/Unit/Fixers/PSR1/ClassConstants/fileWithConstants.php diff --git a/tests/unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php b/tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php similarity index 100% rename from tests/unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php rename to tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php From ed622290e2afc16cc4482eca9e478154603dc4da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 18:50:56 +0100 Subject: [PATCH 10/50] psr2 --- tests/Unit/Fixers/FixerUnitTestCase.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Unit/Fixers/FixerUnitTestCase.php b/tests/Unit/Fixers/FixerUnitTestCase.php index 41099a1..b4be395 100644 --- a/tests/Unit/Fixers/FixerUnitTestCase.php +++ b/tests/Unit/Fixers/FixerUnitTestCase.php @@ -28,7 +28,6 @@ protected function getTokens($source) $commentStack = []; $tkns = token_get_all($source); foreach ($tkns as $token) { - list($id, $text) = $this->getToken($token); $foundTokens[$id] = $id; if (T_COMMENT === $id) { @@ -38,4 +37,4 @@ protected function getTokens($source) return [$foundTokens, $commentStack]; } -} \ No newline at end of file +} From 4ff5c94d0ad9169bd7477393d71d97a6dc6174cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 19:00:15 +0100 Subject: [PATCH 11/50] composer update + directory separators from windows to linux --- composer.json | 6 +- composer.lock | 199 +++++++++--------- tests/Unit/Fixers/FixerUnitTestCase.php | 1 - .../Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php | 8 +- .../ClassConstants/ClassConstantsTest.php | 8 +- 5 files changed, 109 insertions(+), 113 deletions(-) diff --git a/composer.json b/composer.json index 160e9df..72a18c3 100644 --- a/composer.json +++ b/composer.json @@ -15,13 +15,11 @@ }, "autoload-dev": { "psr-4": { - "Fmt\\Tests\\": "tests/" + "Fmt\\Tests\\": "./tests/" } }, - "require": { - "illuminate/support": "^5.4" - }, "require-dev": { + "illuminate/support": "^5.4", "phpunit/phpunit": "^6.0", "symfony/console": "^3.2", "symfony/var-dumper": "^3.2" diff --git a/composer.lock b/composer.lock index d41df13..42fa910 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,9 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "2137c77d58d031ac12f98e3d2cc353d8", - "packages": [ + "content-hash": "02a789fa76b97e4136b51945904c648d", + "packages": [], + "packages-dev": [ { "name": "doctrine/inflector", "version": "v1.1.0", @@ -73,6 +74,60 @@ ], "time": "2015-11-06T14:35:42+00:00" }, + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14T21:17:01+00:00" + }, { "name": "illuminate/contracts", "version": "v5.4.13", @@ -172,6 +227,48 @@ "homepage": "https://laravel.com", "time": "2017-02-15T19:29:24+00:00" }, + { + "name": "myclabs/deep-copy", + "version": "1.6.0", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "doctrine/collections": "1.*", + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "homepage": "https://github.com/myclabs/DeepCopy", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2017-01-26T22:05:40+00:00" + }, { "name": "paragonie/random_compat", "version": "v2.0.9", @@ -219,104 +316,6 @@ "random" ], "time": "2017-03-03T20:43:42+00:00" - } - ], - "packages-dev": [ - { - "name": "doctrine/instantiator", - "version": "1.0.5", - "source": { - "type": "git", - "url": "https://github.com/doctrine/instantiator.git", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", - "shasum": "" - }, - "require": { - "php": ">=5.3,<8.0-DEV" - }, - "require-dev": { - "athletic/athletic": "~0.1.8", - "ext-pdo": "*", - "ext-phar": "*", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Marco Pivetta", - "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.com/" - } - ], - "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://github.com/doctrine/instantiator", - "keywords": [ - "constructor", - "instantiate" - ], - "time": "2015-06-14T21:17:01+00:00" - }, - { - "name": "myclabs/deep-copy", - "version": "1.6.0", - "source": { - "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/5a5a9fc8025a08d8919be87d6884d5a92520cefe", - "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "doctrine/collections": "1.*", - "phpunit/phpunit": "~4.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Create deep copies (clones) of your objects", - "homepage": "https://github.com/myclabs/DeepCopy", - "keywords": [ - "clone", - "copy", - "duplicate", - "object", - "object graph" - ], - "time": "2017-01-26T22:05:40+00:00" }, { "name": "phpdocumentor/reflection-common", diff --git a/tests/Unit/Fixers/FixerUnitTestCase.php b/tests/Unit/Fixers/FixerUnitTestCase.php index b4be395..8dd8224 100644 --- a/tests/Unit/Fixers/FixerUnitTestCase.php +++ b/tests/Unit/Fixers/FixerUnitTestCase.php @@ -6,7 +6,6 @@ class FixerUnitTestCase extends TestCase { - protected function getSource($file) { return file_get_contents($file); diff --git a/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php b/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php index 6983ce6..0762563 100644 --- a/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php +++ b/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php @@ -9,8 +9,8 @@ class BOMMarkTest extends FixerUnitTestCase { public function testCandidate() { - $sourceTrue = $this->getSource(__DIR__ . '\fileWithBOM.php'); - $sourceFalse = $this->getSource(__DIR__ . '\expectedResult.php'); + $sourceTrue = $this->getSource(__DIR__ . DIRECTORY_SEPARATOR . 'fileWithBOM.php'); + $sourceFalse = $this->getSource(__DIR__ . DIRECTORY_SEPARATOR . 'expectedResult.php'); $class = new BOMMark; $this->assertTrue($class->candidate($sourceTrue, null)); @@ -19,8 +19,8 @@ public function testCandidate() public function testFormat() { - $source = $this->getSource(__DIR__ . '\fileWithBOM.php'); - $expected = $this->getSource(__DIR__ . '\expectedResult.php'); + $source = $this->getSource(__DIR__ . '/fileWithBOM.php'); + $expected = $this->getSource(__DIR__ . '/expectedResult.php'); $class = new BOMMark; $fixedSource = $class->format($source); diff --git a/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php b/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php index d36bcc0..487c48e 100644 --- a/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php +++ b/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php @@ -9,8 +9,8 @@ class ClassConstantsTest extends FixerUnitTestCase { public function testCandidate() { - $sourceTrue = $this->getSource(__DIR__ . '\fileWithConstants.php'); - $sourceFalse = $this->getSource(__DIR__ . '\fileWithoutConstants.php'); + $sourceTrue = $this->getSource(__DIR__ . '/fileWithConstants.php'); + $sourceFalse = $this->getSource(__DIR__ . '/fileWithoutConstants.php'); $tokensSourceTrue = $this->getTokens($sourceTrue); $tokensSourceFalse = $this->getTokens($sourceFalse); @@ -23,8 +23,8 @@ public function testCandidate() public function testFormat() { - $source = $this->getSource(__DIR__ . '\fileWithConstants.php'); - $expected = $this->getSource(__DIR__ . '\expectedResult.php'); + $source = $this->getSource(__DIR__ . '/fileWithConstants.php'); + $expected = $this->getSource(__DIR__ . '/expectedResult.php'); $class = new ClassConstants; $fixedSource = $class->format($source); From 557a38ddd108cfd1421384fb5835521bc2da9b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 19:06:27 +0100 Subject: [PATCH 12/50] removed coveralls and updated readme with travis badge --- .coveralls.yml | 2 -- README.md | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index 52bdddb..0000000 --- a/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -service_name: travis-pro -repo_token: Seu3POyFuJwfEFFwNRtwdYlHIwcedZPed \ No newline at end of file diff --git a/README.md b/README.md index 5e51962..2ef53a9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # phpfmt +[![Build Status](https://travis-ci.org/TJSoler/phpfmt.svg?branch=testing)](https://travis-ci.org/TJSoler/phpfmt) + The **phpfmt** project went _closed source_ on June 13, 2016 [Source](https://news.ycombinator.com/item?id=11896851). As the code had BSD license, someone forked it and kept it working, without updating it. And the source got lost. This is the result of extracting a phar file. And many changes. From 084527190b8d51863a2bcdddf699d6ee02c65e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 19:33:47 +0100 Subject: [PATCH 13/50] added PSR1 ClassNames test --- .../Fixers/PSR1/ClassNames/ClassNamesTest.php | 35 +++++++++++++++++++ .../Fixers/PSR1/ClassNames/expectedResult.php | 11 ++++++ .../PSR1/ClassNames/fileWithoutAClass.php | 8 +++++ .../Fixers/PSR1/ClassNames/wrongClassName.php | 11 ++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php create mode 100644 tests/Unit/Fixers/PSR1/ClassNames/expectedResult.php create mode 100644 tests/Unit/Fixers/PSR1/ClassNames/fileWithoutAClass.php create mode 100644 tests/Unit/Fixers/PSR1/ClassNames/wrongClassName.php diff --git a/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php b/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php new file mode 100644 index 0000000..d89a2bb --- /dev/null +++ b/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php @@ -0,0 +1,35 @@ +getSource(__DIR__ . '/wrongClassName.php'); + $sourceFalse = $this->getSource(__DIR__ . '/fileWithoutAClass.php'); + + $tokensSourceTrue = $this->getTokens($sourceTrue); + $tokensSourceFalse = $this->getTokens($sourceFalse); + + $class = new ClassNames; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + $this->assertFalse($class->candidate($sourceFalse, $tokensSourceFalse[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrongClassName.php'); + $expected = $this->getSource(__DIR__ . '/expectedResult.php'); + + $class = new ClassNames; + $fixedSource = $class->format($source); + + $this->assertFalse($source === $expected); + $this->assertTrue($fixedSource === $expected); + } +} diff --git a/tests/Unit/Fixers/PSR1/ClassNames/expectedResult.php b/tests/Unit/Fixers/PSR1/ClassNames/expectedResult.php new file mode 100644 index 0000000..ffa332a --- /dev/null +++ b/tests/Unit/Fixers/PSR1/ClassNames/expectedResult.php @@ -0,0 +1,11 @@ + Date: Fri, 10 Mar 2017 19:34:02 +0100 Subject: [PATCH 14/50] modified ClassNames fixer according to test --- src/Fixers/PSR1/ClassNames.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fixers/PSR1/ClassNames.php b/src/Fixers/PSR1/ClassNames.php index 191c3b9..58a48ed 100644 --- a/src/Fixers/PSR1/ClassNames.php +++ b/src/Fixers/PSR1/ClassNames.php @@ -12,7 +12,7 @@ class ClassNames extends FormatterPass implements FixerInterface { public function candidate($source, $foundTokens) { - if (isset($foundTokens[T_CLASS]) || isset($foundTokens[T_STRING])) { + if (isset($foundTokens[T_CLASS])) { return true; } return false; From 928334d7f6b0e3134420b23a7b7a50153eb8fc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 20:05:51 +0100 Subject: [PATCH 15/50] started using strcmp to test the expected is exactly the same as the fixed --- src/Fixers/PSR1/MethodNames.php | 6 +++--- tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php | 5 +++-- .../Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php | 4 ++-- tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Fixers/PSR1/MethodNames.php b/src/Fixers/PSR1/MethodNames.php index c640588..81b2d7d 100644 --- a/src/Fixers/PSR1/MethodNames.php +++ b/src/Fixers/PSR1/MethodNames.php @@ -12,7 +12,7 @@ class MethodNames extends FormatterPass implements FixerInterface { public function candidate($source, $foundTokens) { - if (isset($foundTokens[T_FUNCTION]) || isset($foundTokens[T_STRING]) || isset($foundTokens[ST_PARENTHESES_OPEN])) { + if (isset($foundTokens[T_FUNCTION])) { return true; } return false; @@ -47,7 +47,7 @@ public function format($source) $foundMethod = false; break; } - case ST_PARENTHESES_OPEN: + case '(': $foundMethod = false; default: $this->appendCode($text); @@ -62,7 +62,7 @@ public function format($source) $this->ptr = $index; switch ($id) { case T_STRING: - if (isset($methodReplaceList[$text]) && $this->rightUsefulTokenIs(ST_PARENTHESES_OPEN)) { + if (isset($methodReplaceList[$text]) && $this->rightUsefulTokenIs('(')) { $this->appendCode($methodReplaceList[$text]); break; } diff --git a/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php b/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php index 0762563..8b39e5f 100644 --- a/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php +++ b/tests/Unit/Fixers/PSR1/BOMMark/BOMMarkTest.php @@ -13,6 +13,7 @@ public function testCandidate() $sourceFalse = $this->getSource(__DIR__ . DIRECTORY_SEPARATOR . 'expectedResult.php'); $class = new BOMMark; + $this->assertTrue($class->candidate($sourceTrue, null)); $this->assertFalse($class->candidate($sourceFalse, null)); } @@ -25,7 +26,7 @@ public function testFormat() $class = new BOMMark; $fixedSource = $class->format($source); - $this->assertFalse($source === $expected); - $this->assertTrue($fixedSource === $expected); + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); } } diff --git a/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php b/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php index 487c48e..14149f9 100644 --- a/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php +++ b/tests/Unit/Fixers/PSR1/ClassConstants/ClassConstantsTest.php @@ -29,7 +29,7 @@ public function testFormat() $class = new ClassConstants; $fixedSource = $class->format($source); - $this->assertFalse($source === $expected); - $this->assertTrue($fixedSource === $expected); + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); } } diff --git a/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php b/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php index d89a2bb..6fe98a2 100644 --- a/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php +++ b/tests/Unit/Fixers/PSR1/ClassNames/ClassNamesTest.php @@ -29,7 +29,7 @@ public function testFormat() $class = new ClassNames; $fixedSource = $class->format($source); - $this->assertFalse($source === $expected); - $this->assertTrue($fixedSource === $expected); + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); } } From 57b7a6f5c98704d5b067c68d64325fb647837e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 20:06:03 +0100 Subject: [PATCH 16/50] added tests for the Method Name fixer --- .../PSR1/MethodNames/MethodNamesTest.php | 35 +++++++++++++++++++ .../Unit/Fixers/PSR1/MethodNames/expected.php | 11 ++++++ .../Unit/Fixers/PSR1/MethodNames/without.php | 8 +++++ tests/Unit/Fixers/PSR1/MethodNames/wrong.php | 11 ++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/Unit/Fixers/PSR1/MethodNames/MethodNamesTest.php create mode 100644 tests/Unit/Fixers/PSR1/MethodNames/expected.php create mode 100644 tests/Unit/Fixers/PSR1/MethodNames/without.php create mode 100644 tests/Unit/Fixers/PSR1/MethodNames/wrong.php diff --git a/tests/Unit/Fixers/PSR1/MethodNames/MethodNamesTest.php b/tests/Unit/Fixers/PSR1/MethodNames/MethodNamesTest.php new file mode 100644 index 0000000..541db32 --- /dev/null +++ b/tests/Unit/Fixers/PSR1/MethodNames/MethodNamesTest.php @@ -0,0 +1,35 @@ +getSource(__DIR__ . '/wrong.php'); + $sourceFalse = $this->getSource(__DIR__ . '/without.php'); + + $tokensSourceTrue = $this->getTokens($sourceTrue); + $tokensSourceFalse = $this->getTokens($sourceFalse); + + $class = new MethodNames; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + $this->assertFalse($class->candidate($sourceFalse, $tokensSourceFalse[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new MethodNames; + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR1/MethodNames/expected.php b/tests/Unit/Fixers/PSR1/MethodNames/expected.php new file mode 100644 index 0000000..52e3468 --- /dev/null +++ b/tests/Unit/Fixers/PSR1/MethodNames/expected.php @@ -0,0 +1,11 @@ + Date: Fri, 10 Mar 2017 21:40:43 +0100 Subject: [PATCH 17/50] added to gigignore a build folder used to experiment with phar --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 57872d0..a4a031f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +/build/ From 00e0fda12ccbb5b5bceea56bbe890b2764e90a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 21:43:14 +0100 Subject: [PATCH 18/50] added ifs to each constant definition --- src/BaseCodeFormatter.php | 105 ++++++++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 26 deletions(-) diff --git a/src/BaseCodeFormatter.php b/src/BaseCodeFormatter.php index fa5f04b..8f16003 100644 --- a/src/BaseCodeFormatter.php +++ b/src/BaseCodeFormatter.php @@ -173,32 +173,84 @@ abstract class BaseCodeFormatter public function __construct() { // what do we do with this shit? - define('ST_AT', '@'); - define('ST_BRACKET_CLOSE', ']'); - define('ST_BRACKET_OPEN', '['); - define('ST_COLON', ':'); - define('ST_COMMA', ','); - define('ST_CONCAT', '.'); - define('ST_CURLY_CLOSE', '}'); - define('ST_CURLY_OPEN', '{'); - define('ST_DIVIDE', '/'); - define('ST_DOLLAR', '$'); - define('ST_EQUAL', '='); - define('ST_EXCLAMATION', '!'); - define('ST_IS_GREATER', '>'); - define('ST_IS_SMALLER', '<'); - define('ST_MINUS', '-'); - define('ST_MODULUS', '%'); - define('ST_PARENTHESES_CLOSE', ')'); - define('ST_PARENTHESES_OPEN', '('); - define('ST_PLUS', '+'); - define('ST_QUESTION', '?'); - define('ST_QUOTE', '"'); - define('ST_REFERENCE', '&'); - define('ST_SEMI_COLON', ';'); - define('ST_TIMES', '*'); - define('ST_BITWISE_OR', '|'); - define('ST_BITWISE_XOR', '^'); + if (! defined('ST_AT')) { + define('ST_AT', '@'); + } + if (! defined('ST_BRACKET_CLOSE')) { + define('ST_BRACKET_CLOSE', ']'); + } + if (! defined('ST_BRACKET_OPEN')) { + define('ST_BRACKET_OPEN', '['); + } + if (! defined('ST_COLON')) { + define('ST_COLON', ':'); + } + if (! defined('ST_COMMA')) { + define('ST_COMMA', ','); + } + if (! defined('ST_CONCAT')) { + define('ST_CONCAT', '.'); + } + if (! defined('ST_CURLY_CLOSE')) { + define('ST_CURLY_CLOSE', '}'); + } + if (! defined('ST_CURLY_OPEN')) { + define('ST_CURLY_OPEN', '{'); + } + if (! defined('ST_DIVIDE')) { + define('ST_DIVIDE', '/'); + } + if (! defined('ST_DOLLAR')) { + define('ST_DOLLAR', '$'); + } + if (! defined('ST_EQUAL')) { + define('ST_EQUAL', '='); + } + if (! defined('ST_EXCLAMATION')) { + define('ST_EXCLAMATION', '!'); + } + if (! defined('ST_IS_GREATER')) { + define('ST_IS_GREATER', '>'); + } + if (! defined('ST_IS_SMALLER')) { + define('ST_IS_SMALLER', '<'); + } + if (! defined('ST_MINUS')) { + define('ST_MINUS', '-'); + } + if (! defined('ST_MODULUS')) { + define('ST_MODULUS', '%'); + } + if (! defined('ST_PARENTHESES_CLOSE')) { + define('ST_PARENTHESES_CLOSE', ')'); + } + if (! defined('ST_PARENTHESES_OPEN')) { + define('ST_PARENTHESES_OPEN', '('); + } + if (! defined('ST_PLUS')) { + define('ST_PLUS', '+'); + } + if (! defined('ST_QUESTION')) { + define('ST_QUESTION', '?'); + } + if (! defined('ST_QUOTE')) { + define('ST_QUOTE', '"'); + } + if (! defined('ST_REFERENCE')) { + define('ST_REFERENCE', '&'); + } + if (! defined('ST_SEMI_COLON')) { + define('ST_SEMI_COLON', ';'); + } + if (! defined('ST_TIMES')) { + define('ST_TIMES', '*'); + } + if (! defined('ST_BITWISE_OR')) { + define('ST_BITWISE_OR', '|'); + } + if (! defined('ST_BITWISE_XOR')) { + define('ST_BITWISE_XOR', '^'); + } if (!defined('T_POW')) { define('T_POW', '**'); } @@ -222,6 +274,7 @@ public function __construct() define('ST_BRACKET_BLOCK', 'ST_BRACKET_BLOCK'); define('ST_CURLY_BLOCK', 'ST_CURLY_BLOCK'); + $this->passes['AddMissingCurlyBraces'] = new AddMissingCurlyBraces(); $this->passes['EliminateDuplicatedEmptyLines'] = new EliminateDuplicatedEmptyLines(); $this->passes['ExtraCommaInArray'] = new ExtraCommaInArray(); From bd38943702906f1aeedd53db01f5ecce5b449952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 21:44:01 +0100 Subject: [PATCH 19/50] added coverals and phpunit code-coverage --- composer.json | 4 +- composer.lock | 425 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 427 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 72a18c3..cb08c96 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,8 @@ "illuminate/support": "^5.4", "phpunit/phpunit": "^6.0", "symfony/console": "^3.2", - "symfony/var-dumper": "^3.2" + "symfony/var-dumper": "^3.2", + "satooshi/php-coveralls": "^1.0", + "phpunit/php-code-coverage": "^5.0" } } diff --git a/composer.lock b/composer.lock index 42fa910..39346ba 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "02a789fa76b97e4136b51945904c648d", + "content-hash": "7c18d7d10bd59bc9157f4c0e51aacd30", "packages": [], "packages-dev": [ { @@ -128,6 +128,102 @@ ], "time": "2015-06-14T21:17:01+00:00" }, + { + "name": "guzzle/guzzle", + "version": "v3.9.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle3.git", + "reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/0645b70d953bc1c067bbc8d5bc53194706b628d9", + "reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.3", + "symfony/event-dispatcher": "~2.1" + }, + "replace": { + "guzzle/batch": "self.version", + "guzzle/cache": "self.version", + "guzzle/common": "self.version", + "guzzle/http": "self.version", + "guzzle/inflection": "self.version", + "guzzle/iterator": "self.version", + "guzzle/log": "self.version", + "guzzle/parser": "self.version", + "guzzle/plugin": "self.version", + "guzzle/plugin-async": "self.version", + "guzzle/plugin-backoff": "self.version", + "guzzle/plugin-cache": "self.version", + "guzzle/plugin-cookie": "self.version", + "guzzle/plugin-curlauth": "self.version", + "guzzle/plugin-error-response": "self.version", + "guzzle/plugin-history": "self.version", + "guzzle/plugin-log": "self.version", + "guzzle/plugin-md5": "self.version", + "guzzle/plugin-mock": "self.version", + "guzzle/plugin-oauth": "self.version", + "guzzle/service": "self.version", + "guzzle/stream": "self.version" + }, + "require-dev": { + "doctrine/cache": "~1.3", + "monolog/monolog": "~1.0", + "phpunit/phpunit": "3.7.*", + "psr/log": "~1.0", + "symfony/class-loader": "~2.1", + "zendframework/zend-cache": "2.*,<2.3", + "zendframework/zend-log": "2.*,<2.3" + }, + "suggest": { + "guzzlehttp/guzzle": "Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.9-dev" + } + }, + "autoload": { + "psr-0": { + "Guzzle": "src/", + "Guzzle\\Tests": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Guzzle Community", + "homepage": "https://github.com/guzzle/guzzle/contributors" + } + ], + "description": "PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "abandoned": "guzzlehttp/guzzle", + "time": "2015-03-18T18:23:50+00:00" + }, { "name": "illuminate/contracts", "version": "v5.4.13", @@ -963,6 +1059,64 @@ ], "time": "2016-10-10T12:19:37+00:00" }, + { + "name": "satooshi/php-coveralls", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/satooshi/php-coveralls.git", + "reference": "da51d304fe8622bf9a6da39a8446e7afd432115c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/satooshi/php-coveralls/zipball/da51d304fe8622bf9a6da39a8446e7afd432115c", + "reference": "da51d304fe8622bf9a6da39a8446e7afd432115c", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-simplexml": "*", + "guzzle/guzzle": "^2.8|^3.0", + "php": ">=5.3.3", + "psr/log": "^1.0", + "symfony/config": "^2.1|^3.0", + "symfony/console": "^2.1|^3.0", + "symfony/stopwatch": "^2.0|^3.0", + "symfony/yaml": "^2.0|^3.0" + }, + "suggest": { + "symfony/http-kernel": "Allows Symfony integration" + }, + "bin": [ + "bin/coveralls" + ], + "type": "library", + "autoload": { + "psr-4": { + "Satooshi\\": "src/Satooshi/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kitamura Satoshi", + "email": "with.no.parachute@gmail.com", + "homepage": "https://www.facebook.com/satooshi.jp" + } + ], + "description": "PHP client library for Coveralls API", + "homepage": "https://github.com/satooshi/php-coveralls", + "keywords": [ + "ci", + "coverage", + "github", + "test" + ], + "time": "2016-01-20T17:35:46+00:00" + }, { "name": "sebastian/code-unit-reverse-lookup", "version": "1.0.1", @@ -1476,6 +1630,62 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2016-10-03T07:35:21+00:00" }, + { + "name": "symfony/config", + "version": "v3.2.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/config.git", + "reference": "741d6d4cd1414d67d48eb71aba6072b46ba740c2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/config/zipball/741d6d4cd1414d67d48eb71aba6072b46ba740c2", + "reference": "741d6d4cd1414d67d48eb71aba6072b46ba740c2", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/filesystem": "~2.8|~3.0" + }, + "require-dev": { + "symfony/yaml": "~3.0" + }, + "suggest": { + "symfony/yaml": "To use the yaml reference dumper" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Config\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Config Component", + "homepage": "https://symfony.com", + "time": "2017-03-01T18:18:25+00:00" + }, { "name": "symfony/console", "version": "v3.2.5", @@ -1596,6 +1806,115 @@ "homepage": "https://symfony.com", "time": "2017-02-18T17:28:00+00:00" }, + { + "name": "symfony/event-dispatcher", + "version": "v2.8.18", + "source": { + "type": "git", + "url": "https://github.com/symfony/event-dispatcher.git", + "reference": "bb4ec47e8e109c1c1172145732d0aa468d967cd0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/bb4ec47e8e109c1c1172145732d0aa468d967cd0", + "reference": "bb4ec47e8e109c1c1172145732d0aa468d967cd0", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "^2.0.5|~3.0.0", + "symfony/dependency-injection": "~2.6|~3.0.0", + "symfony/expression-language": "~2.6|~3.0.0", + "symfony/stopwatch": "~2.3|~3.0.0" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\EventDispatcher\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony EventDispatcher Component", + "homepage": "https://symfony.com", + "time": "2017-02-21T08:33:48+00:00" + }, + { + "name": "symfony/filesystem", + "version": "v3.2.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/filesystem.git", + "reference": "bc0f17bed914df2cceb989972c3b996043c4da4a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/bc0f17bed914df2cceb989972c3b996043c4da4a", + "reference": "bc0f17bed914df2cceb989972c3b996043c4da4a", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Filesystem\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Filesystem Component", + "homepage": "https://symfony.com", + "time": "2017-03-06T19:30:27+00:00" + }, { "name": "symfony/polyfill-mbstring", "version": "v1.3.0", @@ -1655,6 +1974,55 @@ ], "time": "2016-11-14T01:06:16+00:00" }, + { + "name": "symfony/stopwatch", + "version": "v3.2.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/stopwatch.git", + "reference": "c5ee0f8650c84b4d36a5f76b3b504233feaabf75" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/c5ee0f8650c84b4d36a5f76b3b504233feaabf75", + "reference": "c5ee0f8650c84b4d36a5f76b3b504233feaabf75", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Stopwatch\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Stopwatch Component", + "homepage": "https://symfony.com", + "time": "2017-02-18T17:28:00+00:00" + }, { "name": "symfony/var-dumper", "version": "v3.2.5", @@ -1721,6 +2089,61 @@ ], "time": "2017-02-20T13:45:48+00:00" }, + { + "name": "symfony/yaml", + "version": "v3.2.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "093e416ad096355149e265ea2e4cc1f9ee40ab1a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/093e416ad096355149e265ea2e4cc1f9ee40ab1a", + "reference": "093e416ad096355149e265ea2e4cc1f9ee40ab1a", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2017-03-07T16:47:02+00:00" + }, { "name": "webmozart/assert", "version": "1.2.0", From d8622f51ed5a79045d6b4d40bb8180484edbb3c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 21:46:20 +0100 Subject: [PATCH 20/50] modified phppunit.xml to try and have code coverage with coveralls --- phpunit.xml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 8cf7806..c1b9787 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,9 +11,18 @@ stopOnFailure="false" syntaxCheck="true" verbose="true"> - - - ./tests - - + + + ./tests + + + + + ./src + + ./vendor + ./tests + + + \ No newline at end of file From 1c4467480d5788b82714feecc2112622e6520b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 21:47:24 +0100 Subject: [PATCH 21/50] removed commented code --- tests/bootstrap.php | 49 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 209c198..ef755ab 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,46 +1,11 @@ $value) { -// if (is_numeric($option)) { -// $arguments .= ' '.escapeshellarg($value); -// } else { -// $arguments .= " $option=".escapeshellarg($value); -// } -// } - -// $cmd = sprintf('php %s %s -o=- -', $path_to_phpfmt, $arguments); - -// $descriptorspec = array( -// 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from -// 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to -// ); - -// $process = proc_open($cmd, $descriptorspec, $pipes); - -// if (is_resource($process)) { -// // $pipes now looks like this: -// // 0 => writeable handle connected to child stdin -// // 1 => readable handle connected to child stdout - -// fwrite($pipes[0], $stdin); // file_get_contents('php://stdin') -// fclose($pipes[0]); - -// $stdout = stream_get_contents($pipes[1]); -// fclose($pipes[1]); - -// // It is important that you close any pipes before calling -// // proc_close in order to avoid a deadlock -// $return_value = proc_close($process); - -// return $stdout; -// } -// } - -require __DIR__.'/../vendor/autoload.php'; \ No newline at end of file +// we probably need to find a better way to +// accomplish this. \ No newline at end of file From 8ffdb586b7cde2e222ec2fe6077be46c9b8d7bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 21:49:40 +0100 Subject: [PATCH 22/50] fail --- tests/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ef755ab..55f9c78 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,7 +5,7 @@ // we create a new instance of BaseCodeFormatter // only because I don't want to instantiate or // copy / paste all the constants we need. -$removeFromHerePlease =new Fmt\BaseCodeFormatter; +$removeFromHerePlease =new Fmt\CodeFormatter; // we probably need to find a better way to // accomplish this. \ No newline at end of file From eb38feb3fb24aa49239a0bfb8d35f5ab3def0eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 22:03:08 +0100 Subject: [PATCH 23/50] only needed as a reference, for the time being can be checked on master --- src/phpfmt.php | 782 ------------------------------------------------- 1 file changed, 782 deletions(-) delete mode 100755 src/phpfmt.php diff --git a/src/phpfmt.php b/src/phpfmt.php deleted file mode 100755 index a5bdb64..0000000 --- a/src/phpfmt.php +++ /dev/null @@ -1,782 +0,0 @@ - 1) { - $params = array_slice(func_get_args(), 1); - } - call_user_func_array($fn, $params); - die(); - } - - class Message - { - private $key; - private $shm; - public function __construct($key = null) - { - if (null === $key) { - $key = ftok(tempnam(sys_get_temp_dir(), 'csp.' . uniqid('shm', true)), 'C'); - } - $this->shm = shm_attach($key); - if (false === $this->shm) { - trigger_error('Unable to attach shared memory segment for channel', E_ERROR); - } - $this->key = $key; - } - - public function store($msg) - { - shm_put_var($this->shm, 1, $msg); - shm_detach($this->shm); - } - - public function key() - { - return sprintf('%0' . PHP_INT_LENGTH . 'd', (int) $this->key); - } - - public function fetch() - { - $ret = shm_get_var($this->shm, 1); - $this->destroy(); - - return $ret; - } - - public function destroy() - { - if (shm_has_var($this->shm, 1)) { - shm_remove_var($this->shm, 1); - } - shm_remove($this->shm); - } - } - - function make_channel() - { - return new CSP_Channel(); - } - - function select_channel(array $actions) - { - while (true) { - foreach ($actions as $action) { - if ('default' == $action[0]) { - call_user_func_array($action[1]); - break 2; - } elseif (is_callable($action[1])) { - $chn = &$action[0]; - $callback = &$action[1]; - - list($ok, $result) = $chn->non_blocking_out(); - if (true === $ok) { - call_user_func_array($callback, [$result]); - break 2; - } - } elseif ($action[0] instanceof CSP_Channel) { - $chn = &$action[0]; - $msg = &$action[1]; - $callback = &$action[2]; - $params = array_slice($action, 3); - - $ok = $chn->non_blocking_in($msg); - if (CSP_Channel::CLOSED === $ok) { - throw new Exception('Cannot send to closed channel'); - } elseif (true === $ok) { - call_user_func($callback); - break 2; - } - } else { - throw new Exception('Invalid action for CSP select_channel'); - } - } - } - } -} - -$enableCache = false; - -function extractFromArgv($argv, $item) -{ - return array_values( - array_filter($argv, - function ($v) use ($item) { - return substr($v, 0, strlen('--' . $item)) !== '--' . $item; - } - ) - ); -} - -function extractFromArgvShort($argv, $item) -{ - return array_values( - array_filter($argv, - function ($v) use ($item) { - return substr($v, 0, strlen('-' . $item)) !== '-' . $item; - } - ) - ); -} - -function lint($file) -{ - $output = null; - $ret = null; - exec('php -l ' . escapeshellarg($file), $output, $ret); - - return 0 === $ret; -} - -function tabwriter(array $lines) -{ - $colsize = []; - foreach ($lines as $line) { - foreach ($line as $idx => $text) { - $cs = &$colsize[$idx]; - $len = strlen($text); - $cs = max($cs, $len); - } - } - - $final = ''; - foreach ($lines as $line) { - $out = ''; - foreach ($line as $idx => $text) { - $cs = &$colsize[$idx]; - $out .= str_pad($text, $cs) . ' '; - } - $final .= rtrim($out) . PHP_EOL; - } - - return $final; -} - -function selfupdate($argv, $inPhar) -{ - // reimplement. -} - -define('ST_AT', '@'); -define('ST_BRACKET_CLOSE', ']'); -define('ST_BRACKET_OPEN', '['); -define('ST_COLON', ':'); -define('ST_COMMA', ','); -define('ST_CONCAT', '.'); -define('ST_CURLY_CLOSE', '}'); -define('ST_CURLY_OPEN', '{'); -define('ST_DIVIDE', '/'); -define('ST_DOLLAR', '$'); -define('ST_EQUAL', '='); -define('ST_EXCLAMATION', '!'); -define('ST_IS_GREATER', '>'); -define('ST_IS_SMALLER', '<'); -define('ST_MINUS', '-'); -define('ST_MODULUS', '%'); -define('ST_PARENTHESES_CLOSE', ')'); -define('ST_PARENTHESES_OPEN', '('); -define('ST_PLUS', '+'); -define('ST_QUESTION', '?'); -define('ST_QUOTE', '"'); -define('ST_REFERENCE', '&'); -define('ST_SEMI_COLON', ';'); -define('ST_TIMES', '*'); -define('ST_BITWISE_OR', '|'); -define('ST_BITWISE_XOR', '^'); -if (!defined('T_POW')) { - define('T_POW', '**'); -} -if (!defined('T_POW_EQUAL')) { - define('T_POW_EQUAL', '**='); -} -if (!defined('T_YIELD')) { - define('T_YIELD', 'yield'); -} -if (!defined('T_FINALLY')) { - define('T_FINALLY', 'finally'); -} -if (!defined('T_SPACESHIP')) { - define('T_SPACESHIP', '<=>'); -} -if (!defined('T_COALESCE')) { - define('T_COALESCE', '??'); -} - -define('ST_PARENTHESES_BLOCK', 'ST_PARENTHESES_BLOCK'); -define('ST_BRACKET_BLOCK', 'ST_BRACKET_BLOCK'); -define('ST_CURLY_BLOCK', 'ST_CURLY_BLOCK'); - -if (!isset($inPhar)) { - $inPhar = false; -} - -function getOptions($inPhar) -{ - $options = [ - '--cakephp' => 'Apply CakePHP coding style', - '--config=FILENAME' => 'configuration file. Default: .phpfmt.ini', - '--constructor=type' => 'analyse classes for attributes and generate constructor - camel, snake, golang', - '--dry-run' => 'Runs the formatter without atually changing files; returns exit code 1 if changes would have been applied', - '--enable_auto_align' => 'disable auto align of ST_EQUAL and T_DOUBLE_ARROW', - '--exclude=pass1,passN,...' => 'disable specific passes', - '--help-pass' => 'show specific information for one pass', - '--ignore=PATTERN-1,PATTERN-N,...' => 'ignore file names whose names contain any PATTERN-N', - '--indent_with_space=SIZE' => 'use spaces instead of tabs for indentation. Default 4', - '--lint-before' => 'lint files before pretty printing (PHP must be declared in %PATH%/$PATH)', - '--list' => 'list possible transformations', - '--list-simple' => 'list possible transformations - greppable', - '--no-backup' => 'no backup file (original.php~)', - '--passes=pass1,passN,...' => 'call specific compiler pass', - '--profile=NAME' => 'use one of profiles present in configuration file', - '--psr' => 'activate PSR1 and PSR2 styles', - '--psr1' => 'activate PSR1 style', - '--psr1-naming' => 'activate PSR1 style - Section 3 and 4.3 - Class and method names case.', - '--psr2' => 'activate PSR2 style', - '--setters_and_getters=type' => 'analyse classes for attributes and generate setters and getters - camel, snake, golang', - '--smart_linebreak_after_curly' => 'convert multistatement blocks into multiline blocks', - '--visibility_order' => 'fixes visibiliy order for method in classes - PSR-2 4.2', - '--yoda' => 'yoda-style comparisons', - '-h, --help' => 'this help message', - '-o=file' => 'output the formatted code to "file"', - '-o=-' => 'output the formatted code to standard output', - '-v' => 'verbose', - ]; - - if ($inPhar) { - $options['--selfupdate'] = 'self-update fmt.phar from Github'; - $options['--version'] = 'version'; - } - - ksort($options); - - return $options; -} - - function showHelp($argv, $enableCache, $inPhar) - { - $options = getOptions($inPhar); - - echo 'Usage: ' . $argv[0] . ' [-hv] [-o=FILENAME] [--config=FILENAME] [options] ', PHP_EOL; - - $maxLen = max(array_map(function ($v) { - return strlen($v); - }, array_keys($options))); - foreach ($options as $k => $v) { - echo ' ', str_pad($k, $maxLen), ' ', $v, PHP_EOL; - } - - echo PHP_EOL, 'If is "-", it reads from stdin', PHP_EOL; - } - - $getoptLongOptions = [ - 'cakephp', - 'config:', - 'constructor:', - 'dry-run', - 'enable_auto_align', - 'exclude:', - 'help', - 'help-pass:', - 'ignore:', - 'indent_with_space::', - 'lint-before', - 'list', - 'list-simple', - 'no-backup', - 'oracleDB::', - 'passes:', - 'php2go', - 'profile:', - 'psr', - 'psr1', - 'psr1-naming', - 'psr2', - 'setters_and_getters:', - 'smart_linebreak_after_curly', - 'visibility_order', - 'yoda', - ]; - - $opts = getopt( - 'ihvo:', - $getoptLongOptions - ); - - if (isset($opts['list'])) { - echo 'Usage: ', $argv[0], ' --help-pass=PASSNAME', PHP_EOL; - $classes = get_declared_classes(); - $helpLines = []; - foreach ($classes as $className) { - if (is_subclass_of($className, 'AdditionalPass')) { - $pass = new $className(); - $helpLines[] = ["\t- " . $className, $pass->getDescription()]; - } - } - echo tabwriter($helpLines); - die(); - } - - if (isset($opts['list-simple'])) { - $classes = get_declared_classes(); - $helpLines = []; - foreach ($classes as $className) { - if (is_subclass_of($className, 'AdditionalPass')) { - $pass = new $className(); - $helpLines[] = [$className, $pass->getDescription()]; - } - } - echo tabwriter($helpLines); - die(); - } - if (isset($opts['selfupdate'])) { - selfupdate($argv, $inPhar); - } - if (isset($opts['version'])) { - if ($inPhar) { - echo $argv[0], ' ', VERSION, PHP_EOL; - } - exit(0); - } - if (isset($opts['config'])) { - $argv = extractFromArgv($argv, 'config'); - - if ('scan' == $opts['config']) { - $cfgfn = getcwd() . DIRECTORY_SEPARATOR . '.phpfmt.ini'; - $lastcfgfn = ''; - fwrite(STDERR, 'Scanning for configuration file...'); - while (!is_file($cfgfn) && $lastcfgfn != $cfgfn) { - $lastcfgfn = $cfgfn; - $cfgfn = dirname(dirname($cfgfn)) . DIRECTORY_SEPARATOR . '.phpfmt.ini'; - } - $opts['config'] = $cfgfn; - if (file_exists($opts['config']) && is_file($opts['config'])) { - fwrite(STDERR, $opts['config']); - $iniOpts = parse_ini_file($opts['config'], true); - if (!empty($iniOpts)) { - $opts += $iniOpts; - } - } - fwrite(STDERR, PHP_EOL); - } else { - if (!file_exists($opts['config']) || !is_file($opts['config'])) { - fwrite(STDERR, 'Custom configuration not file found' . PHP_EOL); - exit(255); - } - $iniOpts = parse_ini_file($opts['config'], true); - if (!empty($iniOpts)) { - $opts += $iniOpts; - } - } - } elseif (file_exists(getcwd() . DIRECTORY_SEPARATOR . '.phpfmt.ini') && is_file(getcwd() . DIRECTORY_SEPARATOR . '.phpfmt.ini')) { - fwrite(STDERR, 'Configuration file found' . PHP_EOL); - $iniOpts = parse_ini_file(getcwd() . DIRECTORY_SEPARATOR . '.phpfmt.ini', true); - if (isset($opts['profile'])) { - $argv = extractFromArgv($argv, 'profile'); - $profile = &$iniOpts[$opts['profile']]; - if (isset($profile)) { - $iniOpts = $profile; - } - } - $opts = array_merge($iniOpts, $opts); - } - if (isset($opts['h']) || isset($opts['help'])) { - showHelp($argv, $enableCache, $inPhar); - exit(0); - } - - if (isset($opts['help-pass'])) { - $optPass = $opts['help-pass']; - if (class_exists($optPass) && method_exists($optPass, 'getDescription')) { - $pass = new $optPass(); - echo $argv[0], ': "', $optPass, '" - ', $pass->getDescription(), PHP_EOL, PHP_EOL; - echo 'Example:', PHP_EOL, $pass->getExample(), PHP_EOL; - } else { - echo $argv[0], ': Core pass.'; - } - die(); - } - - $backup = true; - if (isset($opts['no-backup'])) { - $argv = extractFromArgv($argv, 'no-backup'); - $backup = false; - } - - $dryRun = false; - if (isset($opts['dry-run'])) { - $argv = extractFromArgv($argv, 'dry-run'); - $dryRun = true; - } - - $ignore_list = null; - if (isset($opts['ignore'])) { - $argv = extractFromArgv($argv, 'ignore'); - $ignore_list = array_map(function ($v) { - return trim($v); - }, explode(',', $opts['ignore'])); - } - - $lintBefore = false; - if (isset($opts['lint-before'])) { - $argv = extractFromArgv($argv, 'lint-before'); - $lintBefore = true; - } - - $fmt = new CodeFormatter(); - if (isset($opts['setters_and_getters'])) { - $argv = extractFromArgv($argv, 'setters_and_getters'); - $fmt->enablePass('SettersAndGettersPass', $opts['setters_and_getters']); - } - - if (isset($opts['constructor'])) { - $argv = extractFromArgv($argv, 'constructor'); - $fmt->enablePass('ConstructorPass', $opts['constructor']); - } - - if (isset($opts['oracleDB'])) { - $argv = extractFromArgv($argv, 'oracleDB'); - - if ('scan' == $opts['oracleDB']) { - $oracle = getcwd() . DIRECTORY_SEPARATOR . 'oracle.sqlite'; - $lastoracle = ''; - while (!is_file($oracle) && $lastoracle != $oracle) { - $lastoracle = $oracle; - $oracle = dirname(dirname($oracle)) . DIRECTORY_SEPARATOR . 'oracle.sqlite'; - } - $opts['oracleDB'] = $oracle; - fwrite(STDERR, PHP_EOL); - } - - if (file_exists($opts['oracleDB']) && is_file($opts['oracleDB'])) { - $fmt->enablePass('AutoImportPass', $opts['oracleDB']); - } - } - - if (isset($opts['smart_linebreak_after_curly'])) { - $fmt->enablePass('SmartLnAfterCurlyOpen'); - $argv = extractFromArgv($argv, 'smart_linebreak_after_curly'); - } - - if (isset($opts['yoda'])) { - $fmt->enablePass('YodaComparisons'); - $argv = extractFromArgv($argv, 'yoda'); - } - - if (isset($opts['enable_auto_align'])) { - $fmt->enablePass('AlignEquals'); - $fmt->enablePass('AlignDoubleArrow'); - $argv = extractFromArgv($argv, 'enable_auto_align'); - } - - if (isset($opts['psr'])) { - PsrDecorator::decorate($fmt); - $argv = extractFromArgv($argv, 'psr'); - } - - if (isset($opts['psr1'])) { - PsrDecorator::PSR1($fmt); - $argv = extractFromArgv($argv, 'psr1'); - } - - if (isset($opts['psr1-naming'])) { - PsrDecorator::PSR1Naming($fmt); - $argv = extractFromArgv($argv, 'psr1-naming'); - } - - if (isset($opts['psr2'])) { - PsrDecorator::PSR2($fmt); - $argv = extractFromArgv($argv, 'psr2'); - } - - if (isset($opts['indent_with_space'])) { - $fmt->enablePass('PSR2IndentWithSpace', $opts['indent_with_space']); - $argv = extractFromArgv($argv, 'indent_with_space'); - } - - if ((isset($opts['psr1']) || isset($opts['psr2']) || isset($opts['psr'])) && isset($opts['enable_auto_align'])) { - $fmt->enablePass('PSR2AlignObjOp'); - } - - if (isset($opts['visibility_order'])) { - $fmt->enablePass('PSR2ModifierVisibilityStaticOrder'); - $argv = extractFromArgv($argv, 'visibility_order'); - } - - if (isset($opts['passes'])) { - $optPasses = array_map(function ($v) { - return trim($v); - }, explode(',', $opts['passes'])); - foreach ($optPasses as $optPass) { - $fmt->enablePass($optPass); - } - $argv = extractFromArgv($argv, 'passes'); - } - - if (isset($opts['cakephp'])) { - $fmt->enablePass('CakePHPStyle'); - $argv = extractFromArgv($argv, 'cakephp'); - } - - if (isset($opts['php2go'])) { - Php2GoDecorator::decorate($fmt); - $argv = extractFromArgv($argv, 'php2go'); - } - - if (isset($opts['exclude'])) { - $passesNames = explode(',', $opts['exclude']); - foreach ($passesNames as $passName) { - $fmt->disablePass(trim($passName)); - } - $argv = extractFromArgv($argv, 'exclude'); - } - - if (isset($opts['v'])) { - $argv = extractFromArgvShort($argv, 'v'); - fwrite(STDERR, 'Used passes: ' . implode(', ', $fmt->getPassesNames()) . PHP_EOL); - } - - if (isset($opts['i'])) { - echo 'php.tools fmt.php interactive mode.', PHP_EOL; - echo 'no '); - $str .= $line; - } while (!('.' == $line || 'quit' == $line)); - if ('quit' == $line) { - exit(0); - } - readline_add_history(substr($str, 0, -1)); - echo $fmt->formatCode('formatCode(file_get_contents('php://stdin')); - exit(0); - } - if ($inPhar) { - if (!file_exists($argv[1])) { - $argv[1] = getcwd() . DIRECTORY_SEPARATOR . $argv[1]; - } - } - if (!is_file($argv[1])) { - fwrite(STDERR, 'File not found: ' . $argv[1] . PHP_EOL); - exit(255); - } - if ('-' == $opts['o']) { - echo $fmt->formatCode(file_get_contents($argv[1])); - exit(0); - } - $argv = array_values($argv); - file_put_contents($opts['o'], $fmt->formatCode(file_get_contents($argv[1]))); - } elseif (isset($argv[1])) { - if ('-' == $argv[1]) { - echo $fmt->formatCode(file_get_contents('php://stdin')); - exit(0); - } - $fileNotFound = false; - $start = microtime(true); - fwrite(STDERR, 'Formatting ...' . PHP_EOL); - $missingFiles = []; - $fileCount = 0; - - $workers = 4; - - $hasFnSeparator = false; - - $filesChanged = false; - - for ($j = 1; $j < $argc; ++$j) { - $arg = &$argv[$j]; - if (!isset($arg)) { - continue; - } - if ('--' == $arg) { - $hasFnSeparator = true; - continue; - } - if ($inPhar && !file_exists($arg)) { - $arg = getcwd() . DIRECTORY_SEPARATOR . $arg; - } - if (is_file($arg)) { - $file = $arg; - if ($lintBefore && !lint($file)) { - fwrite(STDERR, 'Error lint:' . $file . PHP_EOL); - continue; - } - ++$fileCount; - fwrite(STDERR, '.'); - $fileContents = file_get_contents($file); - $formattedCode = $fmt->formatCode($fileContents); - if ($dryRun) { - if ($fileContents !== $formattedCode) { - $filesChanged = true; - } - } else { - file_put_contents($file . '-tmp', $formattedCode); - $oldchmod = fileperms($file); - rename($file . '-tmp', $file); - chmod($file, $oldchmod); - } - } elseif (is_dir($arg)) { - fwrite(STDERR, $arg . PHP_EOL); - - $target_dir = $arg; - $dir = new RecursiveDirectoryIterator($target_dir); - $it = new RecursiveIteratorIterator($dir); - $files = new RegexIterator($it, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH); - - if ($function_exists('pcntl_fork')) { - $chn = make_channel(); - $chn_done = make_channel(); - if ($function_exists('pcntl_fork')) { - fwrite(STDERR, 'Starting ' . $workers . ' workers ...' . PHP_EOL); - } - for ($i = 0; $i < $workers; ++$i) { - cofunc(function ($fmt, $backup, $chn, $lintBefore, $dryRun) { - $filesChanged = false; - while (true) { - $msg = $chn->out(); - if (null === $msg) { - break; - } - $target_dir = $msg['target_dir']; - $file = $msg['file']; - if (empty($file)) { - continue; - } - if ($lintBefore && !lint($file)) { - fwrite(STDERR, 'Error lint:' . $file . PHP_EOL); - continue; - } - - $fmtCode = $fmt->formatCode($content); - if ($dryRun) { - if ($fmtCode !== $content) { - $filesChanged = true; - } - } else { - file_put_contents($file . '-tmp', $fmtCode); - $oldchmod = fileperms($file); - $backup && rename($file, $file . '~'); - rename($file . '-tmp', $file); - chmod($file, $oldchmod); - } - } - }, $fmt, $backup, $chn, $lintBefore, $dryRun); - } - } - - $progress = new ProgressBar( - new StreamOutput(fopen('php://stderr', 'w')), - sizeof(iterator_to_array($files)) - ); - $progress->start(); - - foreach ($files as $file) { - $progress->advance(); - $file = $file[0]; - if (null !== $ignore_list) { - foreach ($ignore_list as $pattern) { - if (false !== strpos($file, $pattern)) { - continue 2; - } - } - } - - ++$fileCount; - if ($function_exists('pcntl_fork')) { - $chn->in([ - 'target_dir' => $target_dir, - 'file' => $file, - ]); - } else { - if (0 == ($fileCount % 20)) { - fwrite(STDERR, ' ' . $fileCount . PHP_EOL); - } - - if ($lintBefore && !lint($file)) { - fwrite(STDERR, 'Error lint:' . $file . PHP_EOL); - continue; - } - $fmtCode = $fmt->formatCode($content); - fwrite(STDERR, '.'); - if ($dryRun) { - if ($fmtCode !== $content) { - $filesChanged = true; - } - } else { - file_put_contents($file . '-tmp', $fmtCode); - $oldchmod = fileperms($file); - $backup && rename($file, $file . '~'); - rename($file . '-tmp', $file); - chmod($file, $oldchmod); - } - } - } - if ($function_exists('pcntl_fork')) { - for ($i = 0; $i < $workers; ++$i) { - $chn->in(null); - } - $chn_done->close(); - $chn->close(); - } - $progress->finish(); - fwrite(STDERR, PHP_EOL); - - continue; - } elseif ( - !is_file($arg) && - ('--' != substr($arg, 0, 2) || $hasFnSeparator) - ) { - $fileNotFound = true; - $missingFiles[] = $arg; - fwrite(STDERR, '!'); - } - if (0 == ($fileCount % 20)) { - fwrite(STDERR, ' ' . $fileCount . PHP_EOL); - } - } - fwrite(STDERR, PHP_EOL); - fwrite(STDERR, ' ' . $fileCount . ' files total' . PHP_EOL); - fwrite(STDERR, 'Took ' . round(microtime(true) - $start, 2) . 's' . PHP_EOL); - if (sizeof($missingFiles)) { - fwrite(STDERR, 'Files not found: ' . PHP_EOL); - foreach ($missingFiles as $file) { - fwrite(STDERR, "\t - " . $file . PHP_EOL); - } - } - if ($dryRun && $filesChanged) { - exit(1); - } - if ($fileNotFound) { - exit(255); - } - } else { - showHelp($argv, $enableCache, $inPhar); - } - exit(0); \ No newline at end of file From 1f2ca74632f9867ad9bf4c231ccbbf35e5683f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 22:03:40 +0100 Subject: [PATCH 24/50] this test, as it is, will fail on php 7.1+, need to rethink --- src/Fixers/PSR1/OpenTags.php | 6 +-- .../Fixers/PSR1/OpenTags/OpenTagsTest.php | 38 +++++++++++++++++++ tests/Unit/Fixers/PSR1/OpenTags/expected.php | 3 ++ tests/Unit/Fixers/PSR1/OpenTags/wrong.php | 3 ++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Fixers/PSR1/OpenTags/OpenTagsTest.php create mode 100644 tests/Unit/Fixers/PSR1/OpenTags/expected.php create mode 100644 tests/Unit/Fixers/PSR1/OpenTags/wrong.php diff --git a/src/Fixers/PSR1/OpenTags.php b/src/Fixers/PSR1/OpenTags.php index 73b5d9e..b98146e 100644 --- a/src/Fixers/PSR1/OpenTags.php +++ b/src/Fixers/PSR1/OpenTags.php @@ -10,11 +10,11 @@ */ class OpenTags extends FormatterPass implements FixerInterface { - public function fix($source): string + public function candidate($source, $tokens) { - return $this->format($source); + return true; } - + public function format($source) { $this->tkns = token_get_all($source); diff --git a/tests/Unit/Fixers/PSR1/OpenTags/OpenTagsTest.php b/tests/Unit/Fixers/PSR1/OpenTags/OpenTagsTest.php new file mode 100644 index 0000000..cc7448a --- /dev/null +++ b/tests/Unit/Fixers/PSR1/OpenTags/OpenTagsTest.php @@ -0,0 +1,38 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); +// $sourceFalse = $this->getSource(__DIR__ . DIRECTORY_SEPARATOR . 'expected.php'); + +// $tokensSourceTrue = $this->getTokens($sourceTrue); +// $tokensSourceFalse = $this->getTokens($sourceFalse); + +// $class = new OpenTags; + +// $this->assertTrue($class->candidate($sourceTrue, null)); +// $this->assertFalse($class->candidate($sourceFalse, null)); +// } + +// public function testFormat() +// { +// $source = $this->getSource(__DIR__ . '/wrong.php'); +// $expected = $this->getSource(__DIR__ . '/expected.php'); + +// $class = new OpenTags; +// $fixedSource = $class->format($source); +// dd($fixedSource); +// $this->assertFalse(strcmp($source, $expected) === 0); +// $this->assertTrue(strcmp($fixedSource, $expected) === 0); +// } +// } diff --git a/tests/Unit/Fixers/PSR1/OpenTags/expected.php b/tests/Unit/Fixers/PSR1/OpenTags/expected.php new file mode 100644 index 0000000..b76ff19 --- /dev/null +++ b/tests/Unit/Fixers/PSR1/OpenTags/expected.php @@ -0,0 +1,3 @@ + Date: Fri, 10 Mar 2017 22:06:29 +0100 Subject: [PATCH 25/50] trying coveralls service? --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2f8a7a5..b7a550e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,4 +16,8 @@ before_install: install: - composer install --prefer-dist --no-interaction --no-suggest -script: vendor/bin/phpunit \ No newline at end of file +script: + - phpunit --coverage-clover build/logs/clover.xml + +after_success: + - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;' \ No newline at end of file From 53d92672a3e2744ee73b36643e809d4a3b8f7d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 22:09:13 +0100 Subject: [PATCH 26/50] added coveralls to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2ef53a9..4df62fa 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![Build Status](https://travis-ci.org/TJSoler/phpfmt.svg?branch=testing)](https://travis-ci.org/TJSoler/phpfmt) +[![Coverage Status](https://coveralls.io/repos/github/TJSoler/phpfmt/badge.svg?branch=testing)](https://coveralls.io/github/TJSoler/phpfmt?branch=testing) + The **phpfmt** project went _closed source_ on June 13, 2016 [Source](https://news.ycombinator.com/item?id=11896851). As the code had BSD license, someone forked it and kept it working, without updating it. And the source got lost. This is the result of extracting a phar file. And many changes. From 02389e03f697b35b336cd88da7bd840ab7d6bdbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 10 Mar 2017 22:37:57 +0100 Subject: [PATCH 27/50] psr2 keywords to lowercase test --- src/Fixers/PSR2/KeywordsLowerCase.php | 7 ++-- .../KeywordsLowerCaseTest.php | 31 ++++++++++++++++ .../PSR2/KeywordsLowerCase/expected.php | 35 +++++++++++++++++++ .../Fixers/PSR2/KeywordsLowerCase/wrong.php | 35 +++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Fixers/PSR2/KeywordsLowerCase/KeywordsLowerCaseTest.php create mode 100644 tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php create mode 100644 tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php diff --git a/src/Fixers/PSR2/KeywordsLowerCase.php b/src/Fixers/PSR2/KeywordsLowerCase.php index 51200b7..71ddfaa 100644 --- a/src/Fixers/PSR2/KeywordsLowerCase.php +++ b/src/Fixers/PSR2/KeywordsLowerCase.php @@ -77,11 +77,14 @@ class KeywordsLowerCase extends FormatterPass implements FixerInterface 'var', 'while', 'xor', + 'true', + 'false', + 'null' ]; public function candidate($source, $foundTokens) { - return true; + return $value = str_contains($source, static::$reservedWords); } public function format($source) @@ -132,7 +135,7 @@ public function format($source) T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST, ]) ) || - isset(static::$reservedWords[$lcText]) + in_array($lcText, static::$reservedWords) ) { $text = $lcText; } diff --git a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/KeywordsLowerCaseTest.php b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/KeywordsLowerCaseTest.php new file mode 100644 index 0000000..1173d8e --- /dev/null +++ b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/KeywordsLowerCaseTest.php @@ -0,0 +1,31 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + $class = new KeywordsLowerCase; + + $this->assertTrue($class->candidate($sourceTrue, null)); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new KeywordsLowerCase; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php new file mode 100644 index 0000000..9365f73 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php @@ -0,0 +1,35 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if($this->testArray[1] == 'array') { + die(); + } + elseif($this->testArray[1] != 'array') { + echo 'something'; + } + else { + $this->something = clone $this->testArray; + } + } + } +} \ No newline at end of file diff --git a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php new file mode 100644 index 0000000..9d44d8f --- /dev/null +++ b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php @@ -0,0 +1,35 @@ +testArray) { + CASE 'an': + // do nothing, because this is only a test class... + BREAK; + + DEFAULT: + // code... + break; + + IF($this->testArray[1] == 'array') { + DIE(); + } + ELSEIF($this->testArray[1] != 'array') { + ECHO 'something'; + } + ELSE { + $this->something = CLONE $this->testArray; + } + } + } +} \ No newline at end of file From c354d44b124054b1d095395942baf81c5a275482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 28 Apr 2017 11:51:14 +0200 Subject: [PATCH 28/50] added tests for psr2 - indent with spaces --- README.md | 6 + composer.json | 5 +- composer.lock | 401 +++++++++++++----- src/Console/Application.php | 12 +- src/Console/Commands/FixCommand.php | 346 +++++++-------- .../PSR2/IndentWithSpace.php} | 7 +- src/Fixers/PSR2/KeywordsLowerCase.php | 30 +- .../ClassConstants/fileWithoutConstants.php | 2 +- .../IndentWithSpace/IndentWithSpaceTest.php | 35 ++ .../Fixers/PSR2/IndentWithSpace/expected.php | 33 ++ .../IndentWithSpace/expectedWithTwoSpaces.php | 33 ++ .../Fixers/PSR2/IndentWithSpace/wrong.php | 33 ++ .../PSR2/KeywordsLowerCase/expected.php | 8 +- .../Fixers/PSR2/KeywordsLowerCase/wrong.php | 8 +- tests/bootstrap.php | 2 +- 15 files changed, 660 insertions(+), 301 deletions(-) rename src/{PSR2IndentWithSpace.php => Fixers/PSR2/IndentWithSpace.php} (86%) create mode 100644 tests/Unit/Fixers/PSR2/IndentWithSpace/IndentWithSpaceTest.php create mode 100644 tests/Unit/Fixers/PSR2/IndentWithSpace/expected.php create mode 100644 tests/Unit/Fixers/PSR2/IndentWithSpace/expectedWithTwoSpaces.php create mode 100644 tests/Unit/Fixers/PSR2/IndentWithSpace/wrong.php diff --git a/README.md b/README.md index 4df62fa..9d5f7e8 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,12 @@ The **phpfmt** project went _closed source_ on June 13, 2016 [Source](https://ne As the code had BSD license, someone forked it and kept it working, without updating it. And the source got lost. This is the result of extracting a phar file. And many changes. +## This fork objectives: +- + +## This branch: +This branch should be UP until every test is been written. + ## What Is It ? **phpfmt** formats PHP code by making it readable and following a specific coding guideline. diff --git a/composer.json b/composer.json index cb08c96..faad222 100644 --- a/composer.json +++ b/composer.json @@ -20,10 +20,11 @@ }, "require-dev": { "illuminate/support": "^5.4", - "phpunit/phpunit": "^6.0", + "phpunit/phpunit": "^6.1", "symfony/console": "^3.2", "symfony/var-dumper": "^3.2", "satooshi/php-coveralls": "^1.0", - "phpunit/php-code-coverage": "^5.0" + "phpunit/php-code-coverage": "^5.0", + "sebastian/diff": "^1.4" } } diff --git a/composer.lock b/composer.lock index 39346ba..aa5b156 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "7c18d7d10bd59bc9157f4c0e51aacd30", + "content-hash": "e5eca509669e600015cb489f3e74440b", "packages": [], "packages-dev": [ { @@ -226,16 +226,16 @@ }, { "name": "illuminate/contracts", - "version": "v5.4.13", + "version": "v5.4.19", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", - "reference": "dd256891c80fd94a58ab83d7989d6da2f50e30ea" + "reference": "ab2825726bee46a67c8cc66789852189dbef74a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/contracts/zipball/dd256891c80fd94a58ab83d7989d6da2f50e30ea", - "reference": "dd256891c80fd94a58ab83d7989d6da2f50e30ea", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/ab2825726bee46a67c8cc66789852189dbef74a9", + "reference": "ab2825726bee46a67c8cc66789852189dbef74a9", "shasum": "" }, "require": { @@ -264,20 +264,20 @@ ], "description": "The Illuminate Contracts package.", "homepage": "https://laravel.com", - "time": "2017-02-21T14:21:59+00:00" + "time": "2017-03-29T13:17:47+00:00" }, { "name": "illuminate/support", - "version": "v5.4.13", + "version": "v5.4.19", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", - "reference": "904f63003fd67ede2ec3be018b322d1c29415465" + "reference": "b8cb37e15331c59da51c8ee5838038baa22d7955" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/support/zipball/904f63003fd67ede2ec3be018b322d1c29415465", - "reference": "904f63003fd67ede2ec3be018b322d1c29415465", + "url": "https://api.github.com/repos/illuminate/support/zipball/b8cb37e15331c59da51c8ee5838038baa22d7955", + "reference": "b8cb37e15331c59da51c8ee5838038baa22d7955", "shasum": "" }, "require": { @@ -321,20 +321,20 @@ ], "description": "The Illuminate Support package.", "homepage": "https://laravel.com", - "time": "2017-02-15T19:29:24+00:00" + "time": "2017-04-09T14:34:57+00:00" }, { "name": "myclabs/deep-copy", - "version": "1.6.0", + "version": "1.6.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe" + "reference": "8e6e04167378abf1ddb4d3522d8755c5fd90d102" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/5a5a9fc8025a08d8919be87d6884d5a92520cefe", - "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/8e6e04167378abf1ddb4d3522d8755c5fd90d102", + "reference": "8e6e04167378abf1ddb4d3522d8755c5fd90d102", "shasum": "" }, "require": { @@ -363,20 +363,20 @@ "object", "object graph" ], - "time": "2017-01-26T22:05:40+00:00" + "time": "2017-04-12T18:52:22+00:00" }, { "name": "paragonie/random_compat", - "version": "v2.0.9", + "version": "v2.0.10", "source": { "type": "git", "url": "https://github.com/paragonie/random_compat.git", - "reference": "6968531206671f94377b01dc7888d5d1b858a01b" + "reference": "634bae8e911eefa89c1abfbf1b66da679ac8f54d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/paragonie/random_compat/zipball/6968531206671f94377b01dc7888d5d1b858a01b", - "reference": "6968531206671f94377b01dc7888d5d1b858a01b", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/634bae8e911eefa89c1abfbf1b66da679ac8f54d", + "reference": "634bae8e911eefa89c1abfbf1b66da679ac8f54d", "shasum": "" }, "require": { @@ -411,7 +411,109 @@ "pseudorandom", "random" ], - "time": "2017-03-03T20:43:42+00:00" + "time": "2017-03-13T16:27:32+00:00" + }, + { + "name": "phar-io/manifest", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "2df402786ab5368a0169091f61a7c1e0eb6852d0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/2df402786ab5368a0169091f61a7c1e0eb6852d0", + "reference": "2df402786ab5368a0169091f61a7c1e0eb6852d0", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-phar": "*", + "phar-io/version": "^1.0.1", + "php": "^5.6 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "time": "2017-03-05T18:14:27+00:00" + }, + { + "name": "phar-io/version", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/a70c0ced4be299a63d32fa96d9281d03e94041df", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "time": "2017-03-05T17:38:23+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -624,16 +726,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "5.0.3", + "version": "5.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "4e99e1c4f9b05cbf4d6e84b100b3ff4107cf8cd1" + "reference": "dc421f9ca5082a0c0cb04afb171c765f79add85b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/4e99e1c4f9b05cbf4d6e84b100b3ff4107cf8cd1", - "reference": "4e99e1c4f9b05cbf4d6e84b100b3ff4107cf8cd1", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/dc421f9ca5082a0c0cb04afb171c765f79add85b", + "reference": "dc421f9ca5082a0c0cb04afb171c765f79add85b", "shasum": "" }, "require": { @@ -644,20 +746,21 @@ "phpunit/php-text-template": "^1.2", "phpunit/php-token-stream": "^1.4.11 || ^2.0", "sebastian/code-unit-reverse-lookup": "^1.0", - "sebastian/environment": "^2.0", - "sebastian/version": "^2.0" + "sebastian/environment": "^3.0", + "sebastian/version": "^2.0", + "theseer/tokenizer": "^1.1" }, "require-dev": { "ext-xdebug": "^2.5", "phpunit/phpunit": "^6.0" }, "suggest": { - "ext-xdebug": "^2.5.1" + "ext-xdebug": "^2.5.3" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "5.0.x-dev" + "dev-master": "5.2.x-dev" } }, "autoload": { @@ -683,7 +786,7 @@ "testing", "xunit" ], - "time": "2017-03-06T14:22:16+00:00" + "time": "2017-04-21T08:03:57+00:00" }, { "name": "phpunit/php-file-iterator", @@ -873,16 +976,16 @@ }, { "name": "phpunit/phpunit", - "version": "6.0.8", + "version": "6.1.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6" + "reference": "fbf2e46d5c563ee78c9b559bcbeb1c97cad6af0f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/47ee3fa1bca5c50f1d25105201eb20df777bd7b6", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/fbf2e46d5c563ee78c9b559bcbeb1c97cad6af0f", + "reference": "fbf2e46d5c563ee78c9b559bcbeb1c97cad6af0f", "shasum": "" }, "require": { @@ -892,19 +995,21 @@ "ext-mbstring": "*", "ext-xml": "*", "myclabs/deep-copy": "^1.3", + "phar-io/manifest": "^1.0.1", + "phar-io/version": "^1.0", "php": "^7.0", - "phpspec/prophecy": "^1.6.2", - "phpunit/php-code-coverage": "^5.0", + "phpspec/prophecy": "^1.7", + "phpunit/php-code-coverage": "^5.2", "phpunit/php-file-iterator": "^1.4", "phpunit/php-text-template": "^1.2", "phpunit/php-timer": "^1.0.6", "phpunit/phpunit-mock-objects": "^4.0", - "sebastian/comparator": "^1.2.4 || ^2.0", + "sebastian/comparator": "^2.0", "sebastian/diff": "^1.2", - "sebastian/environment": "^2.0", - "sebastian/exporter": "^2.0 || ^3.0", + "sebastian/environment": "^3.0.1", + "sebastian/exporter": "^3.1", "sebastian/global-state": "^1.1 || ^2.0", - "sebastian/object-enumerator": "^2.0 || ^3.0", + "sebastian/object-enumerator": "^3.0.2", "sebastian/resource-operations": "^1.0", "sebastian/version": "^2.0" }, @@ -925,7 +1030,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "6.0.x-dev" + "dev-master": "6.1.x-dev" } }, "autoload": { @@ -951,7 +1056,7 @@ "testing", "xunit" ], - "time": "2017-03-02T15:24:03+00:00" + "time": "2017-04-25T21:30:13+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -1280,28 +1385,28 @@ }, { "name": "sebastian/environment", - "version": "2.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac" + "reference": "11e7710b7724d42c62249b0e9d3030240398949d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5795ffe5dc5b02460c3e34222fee8cbe245d8fac", - "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/11e7710b7724d42c62249b0e9d3030240398949d", + "reference": "11e7710b7724d42c62249b0e9d3030240398949d", "shasum": "" }, "require": { - "php": "^5.6 || ^7.0" + "php": "^7.0" }, "require-dev": { - "phpunit/phpunit": "^5.0" + "phpunit/phpunit": "^6.1" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "3.0.x-dev" } }, "autoload": { @@ -1326,20 +1431,20 @@ "environment", "hhvm" ], - "time": "2016-11-26T07:53:53+00:00" + "time": "2017-04-21T14:40:32+00:00" }, { "name": "sebastian/exporter", - "version": "3.0.0", + "version": "3.1.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "b82d077cb3459e393abcf4867ae8f7230dcb51f6" + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/b82d077cb3459e393abcf4867ae8f7230dcb51f6", - "reference": "b82d077cb3459e393abcf4867ae8f7230dcb51f6", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/234199f4528de6d12aaa58b612e98f7d36adb937", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937", "shasum": "" }, "require": { @@ -1353,7 +1458,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0.x-dev" + "dev-master": "3.1.x-dev" } }, "autoload": { @@ -1393,27 +1498,27 @@ "export", "exporter" ], - "time": "2017-03-03T06:25:06+00:00" + "time": "2017-04-03T13:19:02+00:00" }, { "name": "sebastian/global-state", - "version": "1.1.1", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4" + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bc37d50fea7d017d3d340f230811c9f1d7280af4", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": "^7.0" }, "require-dev": { - "phpunit/phpunit": "~4.2" + "phpunit/phpunit": "^6.0" }, "suggest": { "ext-uopz": "*" @@ -1421,7 +1526,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "2.0-dev" } }, "autoload": { @@ -1444,24 +1549,25 @@ "keywords": [ "global state" ], - "time": "2015-10-12T03:26:01+00:00" + "time": "2017-04-27T15:39:26+00:00" }, { "name": "sebastian/object-enumerator", - "version": "3.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/object-enumerator.git", - "reference": "de6e32f7192dfea2e4bedc892434f4830b5c5794" + "reference": "31dd3379d16446c5d86dec32ab1ad1f378581ad8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/de6e32f7192dfea2e4bedc892434f4830b5c5794", - "reference": "de6e32f7192dfea2e4bedc892434f4830b5c5794", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/31dd3379d16446c5d86dec32ab1ad1f378581ad8", + "reference": "31dd3379d16446c5d86dec32ab1ad1f378581ad8", "shasum": "" }, "require": { "php": "^7.0", + "sebastian/object-reflector": "^1.0", "sebastian/recursion-context": "^3.0" }, "require-dev": { @@ -1490,7 +1596,52 @@ ], "description": "Traverses array structures and object graphs to enumerate all referenced objects", "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2017-03-03T06:21:01+00:00" + "time": "2017-03-12T15:17:29+00:00" + }, + { + "name": "sebastian/object-reflector", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "773f97c67f28de00d397be301821b06708fca0be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", + "reference": "773f97c67f28de00d397be301821b06708fca0be", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "time": "2017-03-29T09:07:27+00:00" }, { "name": "sebastian/recursion-context", @@ -1632,16 +1783,16 @@ }, { "name": "symfony/config", - "version": "v3.2.6", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "741d6d4cd1414d67d48eb71aba6072b46ba740c2" + "reference": "8444bde28e3c2a33e571e6f180c2d78bfdc4480d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/741d6d4cd1414d67d48eb71aba6072b46ba740c2", - "reference": "741d6d4cd1414d67d48eb71aba6072b46ba740c2", + "url": "https://api.github.com/repos/symfony/config/zipball/8444bde28e3c2a33e571e6f180c2d78bfdc4480d", + "reference": "8444bde28e3c2a33e571e6f180c2d78bfdc4480d", "shasum": "" }, "require": { @@ -1684,20 +1835,20 @@ ], "description": "Symfony Config Component", "homepage": "https://symfony.com", - "time": "2017-03-01T18:18:25+00:00" + "time": "2017-04-04T15:30:56+00:00" }, { "name": "symfony/console", - "version": "v3.2.5", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "28fb243a2b5727774ca309ec2d92da240f1af0dd" + "reference": "c30243cc51f726812be3551316b109a2f5deaf8d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/28fb243a2b5727774ca309ec2d92da240f1af0dd", - "reference": "28fb243a2b5727774ca309ec2d92da240f1af0dd", + "url": "https://api.github.com/repos/symfony/console/zipball/c30243cc51f726812be3551316b109a2f5deaf8d", + "reference": "c30243cc51f726812be3551316b109a2f5deaf8d", "shasum": "" }, "require": { @@ -1747,20 +1898,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2017-03-06T19:30:27+00:00" + "time": "2017-04-04T14:33:42+00:00" }, { "name": "symfony/debug", - "version": "v3.2.5", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/debug.git", - "reference": "b90c9f91ad8ac37d9f114e369042d3226b34dc1a" + "reference": "56f613406446a4a0a031475cfd0a01751de22659" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/debug/zipball/b90c9f91ad8ac37d9f114e369042d3226b34dc1a", - "reference": "b90c9f91ad8ac37d9f114e369042d3226b34dc1a", + "url": "https://api.github.com/repos/symfony/debug/zipball/56f613406446a4a0a031475cfd0a01751de22659", + "reference": "56f613406446a4a0a031475cfd0a01751de22659", "shasum": "" }, "require": { @@ -1804,20 +1955,20 @@ ], "description": "Symfony Debug Component", "homepage": "https://symfony.com", - "time": "2017-02-18T17:28:00+00:00" + "time": "2017-03-28T21:38:24+00:00" }, { "name": "symfony/event-dispatcher", - "version": "v2.8.18", + "version": "v2.8.19", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "bb4ec47e8e109c1c1172145732d0aa468d967cd0" + "reference": "88b65f0ac25355090e524aba4ceb066025df8bd2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/bb4ec47e8e109c1c1172145732d0aa468d967cd0", - "reference": "bb4ec47e8e109c1c1172145732d0aa468d967cd0", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/88b65f0ac25355090e524aba4ceb066025df8bd2", + "reference": "88b65f0ac25355090e524aba4ceb066025df8bd2", "shasum": "" }, "require": { @@ -1864,20 +2015,20 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2017-02-21T08:33:48+00:00" + "time": "2017-04-03T20:37:06+00:00" }, { "name": "symfony/filesystem", - "version": "v3.2.6", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "bc0f17bed914df2cceb989972c3b996043c4da4a" + "reference": "64421e6479c4a8e60d790fb666bd520992861b66" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/bc0f17bed914df2cceb989972c3b996043c4da4a", - "reference": "bc0f17bed914df2cceb989972c3b996043c4da4a", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/64421e6479c4a8e60d790fb666bd520992861b66", + "reference": "64421e6479c4a8e60d790fb666bd520992861b66", "shasum": "" }, "require": { @@ -1913,7 +2064,7 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2017-03-06T19:30:27+00:00" + "time": "2017-03-26T15:47:15+00:00" }, { "name": "symfony/polyfill-mbstring", @@ -1976,7 +2127,7 @@ }, { "name": "symfony/stopwatch", - "version": "v3.2.6", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", @@ -2025,16 +2176,16 @@ }, { "name": "symfony/var-dumper", - "version": "v3.2.5", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/var-dumper.git", - "reference": "4100f347aff890bc16b0b4b42843b599db257b2d" + "reference": "81dce20f69a8b40427e1f4e6462178df87cafc03" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/var-dumper/zipball/4100f347aff890bc16b0b4b42843b599db257b2d", - "reference": "4100f347aff890bc16b0b4b42843b599db257b2d", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/81dce20f69a8b40427e1f4e6462178df87cafc03", + "reference": "81dce20f69a8b40427e1f4e6462178df87cafc03", "shasum": "" }, "require": { @@ -2087,20 +2238,20 @@ "debug", "dump" ], - "time": "2017-02-20T13:45:48+00:00" + "time": "2017-03-12T16:07:05+00:00" }, { "name": "symfony/yaml", - "version": "v3.2.6", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "093e416ad096355149e265ea2e4cc1f9ee40ab1a" + "reference": "62b4cdb99d52cb1ff253c465eb1532a80cebb621" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/093e416ad096355149e265ea2e4cc1f9ee40ab1a", - "reference": "093e416ad096355149e265ea2e4cc1f9ee40ab1a", + "url": "https://api.github.com/repos/symfony/yaml/zipball/62b4cdb99d52cb1ff253c465eb1532a80cebb621", + "reference": "62b4cdb99d52cb1ff253c465eb1532a80cebb621", "shasum": "" }, "require": { @@ -2142,7 +2293,47 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2017-03-07T16:47:02+00:00" + "time": "2017-03-20T09:45:15+00:00" + }, + { + "name": "theseer/tokenizer", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/theseer/tokenizer.git", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": "^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + } + ], + "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "time": "2017-04-07T12:08:54+00:00" }, { "name": "webmozart/assert", diff --git a/src/Console/Application.php b/src/Console/Application.php index 6e890a9..34dc333 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -7,19 +7,19 @@ class Application extends BaseApplication { - const VERSION = '20.0.0-DEV'; + const VERSION = '20.0.0-DEV'; - public function __construct() - { - parent::__construct('PHPFMT', self::VERSION); + public function __construct() + { + parent::__construct('PHPFMT', self::VERSION); $this->add(new FixCommand()); $this->setDefaultCommand(' ', true); // is there a way to show the help if no arguments are found? - } + } - public function getLongVersion() + public function getLongVersion() { $version = parent::getLongVersion().' created by U Cirello, rescued by David Nanch and maintained by Tomás Soler'; $commit = '@git-commit@'; diff --git a/src/Console/Commands/FixCommand.php b/src/Console/Commands/FixCommand.php index a1c1beb..8ffbe92 100644 --- a/src/Console/Commands/FixCommand.php +++ b/src/Console/Commands/FixCommand.php @@ -27,203 +27,205 @@ protected function configure() ->setDefinition([ // arguments new InputArgument( - 'path', - InputArgument::REQUIRED | InputArgument::IS_ARRAY, - 'The path or file.' + 'path', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'The path or file.' ), // options new InputOption( - 'cakephp', - '', - InputOption::VALUE_NONE, - 'Apply CakePHP coding style' + 'cakephp', + '', + InputOption::VALUE_NONE, + 'Apply CakePHP coding style' ), new InputOption( - 'config', - '', - InputOption::VALUE_REQUIRED, - 'The path to a configuration file', - '.phpfmt.ini' + 'config', + '', + InputOption::VALUE_REQUIRED, + 'The path to a configuration file', + '.phpfmt.ini' ), new InputOption( - 'constructor', - '', - InputOption::VALUE_REQUIRED, - 'Analyse classes for attributes and generate constructor. Options: camel, snake, golang', - 'camel' + 'constructor', + '', + InputOption::VALUE_REQUIRED, + 'Analyse classes for attributes and generate constructor. Options: camel, snake, golang', + 'camel' ), new InputOption( - 'dry-run', - '', - InputOption::VALUE_NONE, - 'Only shows which files would have been modified.' + 'dry-run', + '', + InputOption::VALUE_NONE, + 'Only shows which files would have been modified.' ), new InputOption( - 'enable_auto_align', - '', - InputOption::VALUE_NONE, - 'Disable auto align of ST_EQUAL and T_DOUBLE_ARROW' + 'enable_auto_align', + '', + InputOption::VALUE_NONE, + 'Disable auto align of ST_EQUAL and T_DOUBLE_ARROW' ), new InputOption( - 'exclude', - '', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Disable specific passes. Ex: --exlude=GeneratePHPDoc,LongArray' - ), - - new InputOption( - 'help-pass', - '', - InputOption::VALUE_REQUIRED, - 'Show specific information for a pass.' - ), - - new InputOption( - 'ignore', - '', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Ignore file names whose names contain any of the patterns.' - ), - - new InputOption( - 'indent_with_space', - '', - InputOption::VALUE_REQUIRED, - 'Use spaces instead of tabs for indentation.', - '4' - ), - - new InputOption( - 'lint-before', - '', - InputOption::VALUE_NONE, - 'Lint files before pretty printing (PHP must be declared in the PATH).' - ), - - new InputOption( - 'list', - '', - InputOption::VALUE_NONE, - 'List all possible transformations.' - ), - - new InputOption( - 'list-simple', - '', - InputOption::VALUE_NONE, - 'List all possible transformations (greppable).' - ), - - // '--no-backup' => 'no backup file (original.php~)', - new InputOption( - 'no-backup', - '', - InputOption::VALUE_NONE, - 'No backup file. (something.php~)' - ), - - // '--passes=pass1,passN,...' => 'call specific compiler pass', - new InputOption( - 'passes', - '', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Call specific compiler pass. Ex --passes=OrderAndRemoveUseClauses' - ), - - // '--profile=NAME' => 'use one of profiles present in configuration file', - new InputOption( - 'profile', - '', - InputOption::VALUE_REQUIRED, - 'Use one of profiles present in configuration file.' - ), - - // '--psr' => 'activate PSR1 and PSR2 styles', - new InputOption( - 'psr', - '', - InputOption::VALUE_NONE, - 'Activate PSR1 and PSR2 styles.' - ), - // '--psr1' => 'activate PSR1 style', - new InputOption( - 'psr1', - '', - InputOption::VALUE_NONE, - 'Activate PSR1 styles.' - ), - // '--psr1-naming' => 'activate PSR1 style - Section 3 and 4.3 - Class and method names case.', - new InputOption( - 'psr1-naming', - '', - InputOption::VALUE_NONE, - 'Activate PSR1 style - Section 3 and 4.3 - Class and method names case.' - ), - // '--psr2' => 'activate PSR2 style', - new InputOption( - 'psr2', - '', - InputOption::VALUE_NONE, - 'Activate PSR2 style.' - ), - // '--setters_and_getters=type' => 'analyse classes for attributes and generate setters and getters - camel, snake, golang', - new InputOption( - 'setters_and_getters', - '', - InputOption::VALUE_REQUIRED, - 'Analyze classes for attributes and generate setters and getters. Options: camel, snake, golang.' - ), - // '--smart_linebreak_after_curly' => 'convert multistatement blocks into multiline blocks', - new InputOption( - 'smart_linebreak_after_curly', - '', - InputOption::VALUE_NONE, - 'Convert multistatement blocks into multiline blocks.' - ), - // '--visibility_order' => 'fixes visibiliy order for method in classes - PSR-2 4.2', - new InputOption( - 'visibility_order', - '', - InputOption::VALUE_NONE, - 'Fixes visibiliy order for method in classes - PSR2 4.2.' - ), - // '--yoda' => 'yoda-style comparisons', - new InputOption( - 'yoda', - '', - InputOption::VALUE_NONE, - 'Yoda style comparisons.' - ), - - new InputOption( - 'interactive', - 'i', - InputOption::VALUE_NONE, - 'Start phpfmt interactive.' + 'exclude', + '', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Disable specific passes. Ex: --exlude=GeneratePHPDoc,LongArray' ), - // '-o=file' => 'output the formatted code to "file"', - // '-o=-' => 'output the formatted code to standard output', - New InputOption( - 'output', - 'o', - InputOption::VALUE_REQUIRED, - 'Output the formatted code. Options: "filename" (a file), "-" (standard output)', - '-' - ), + new InputOption( + 'help-pass', + '', + InputOption::VALUE_REQUIRED, + 'Show specific information for a pass.' + ), + + new InputOption( + 'ignore', + '', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Ignore file names whose names contain any of the patterns.' + ), + + new InputOption( + 'indent_with_space', + '', + InputOption::VALUE_REQUIRED, + 'Use spaces instead of tabs for indentation.', + '4' + ), + + new InputOption( + 'lint-before', + '', + InputOption::VALUE_NONE, + 'Lint files before pretty printing (PHP must be declared in the PATH).' + ), + + new InputOption( + 'list', + '', + InputOption::VALUE_NONE, + 'List all possible transformations.' + ), + + new InputOption( + 'list-simple', + '', + InputOption::VALUE_NONE, + 'List all possible transformations (greppable).' + ), + + // '--no-backup' => 'no backup file (original.php~)', + new InputOption( + 'no-backup', + '', + InputOption::VALUE_NONE, + 'No backup file. (something.php~)' + ), + + // '--passes=pass1,passN,...' => 'call specific compiler pass', + new InputOption( + 'passes', + '', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Call specific compiler pass. Ex --passes=OrderAndRemoveUseClauses' + ), + + // '--profile=NAME' => 'use one of profiles present in configuration file', + new InputOption( + 'profile', + '', + InputOption::VALUE_REQUIRED, + 'Use one of profiles present in configuration file.' + ), + + // '--psr' => 'activate PSR1 and PSR2 styles', + new InputOption( + 'psr', + '', + InputOption::VALUE_NONE, + 'Activate PSR1 and PSR2 styles.' + ), + // '--psr1' => 'activate PSR1 style', + new InputOption( + 'psr1', + '', + InputOption::VALUE_NONE, + 'Activate PSR1 styles.' + ), + // '--psr1-naming' => 'activate PSR1 style - Section 3 and 4.3 - Class and method names case.', + new InputOption( + 'psr1-naming', + '', + InputOption::VALUE_NONE, + 'Activate PSR1 style - Section 3 and 4.3 - Class and method names case.' + ), + // '--psr2' => 'activate PSR2 style', + new InputOption( + 'psr2', + '', + InputOption::VALUE_NONE, + 'Activate PSR2 style.' + ), + // '--setters_and_getters=type' => 'analyse classes + // for attributes and generate setters and getters + // camel, snake, golang', + new InputOption( + 'setters_and_getters', + '', + InputOption::VALUE_REQUIRED, + 'Analyze classes for attributes and generate setters and getters. Options: camel, snake, golang.' + ), + // '--smart_linebreak_after_curly' => 'convert multistatement blocks into multiline blocks', + new InputOption( + 'smart_linebreak_after_curly', + '', + InputOption::VALUE_NONE, + 'Convert multistatement blocks into multiline blocks.' + ), + // '--visibility_order' => 'fixes visibiliy order for method in classes - PSR-2 4.2', + new InputOption( + 'visibility_order', + '', + InputOption::VALUE_NONE, + 'Fixes visibiliy order for method in classes - PSR2 4.2.' + ), + // '--yoda' => 'yoda-style comparisons', + new InputOption( + 'yoda', + '', + InputOption::VALUE_NONE, + 'Yoda style comparisons.' + ), + + new InputOption( + 'interactive', + 'i', + InputOption::VALUE_NONE, + 'Start phpfmt interactive.' + ), + + // '-o=file' => 'output the formatted code to "file"', + // '-o=-' => 'output the formatted code to standard output', + new InputOption( + 'output', + 'o', + InputOption::VALUE_REQUIRED, + 'Output the formatted code. Options: "filename" (a file), "-" (standard output)', + '-' + ), ]) ->setDescription('Fixes a directory or a file.'); } protected function execute(InputInterface $input, OutputInterface $output) { - $this->input = $input; + $this->input = $input; $this->output = $output; $fmt = new CodeFormatter(); @@ -241,10 +243,10 @@ protected function execute(InputInterface $input, OutputInterface $output) protected function processOptions() { - + // } - protected function processArguments() + protected function processArguments() { dump($this->input->getArguments()); foreach ($this->input->getArgument('path') as $arg) { diff --git a/src/PSR2IndentWithSpace.php b/src/Fixers/PSR2/IndentWithSpace.php similarity index 86% rename from src/PSR2IndentWithSpace.php rename to src/Fixers/PSR2/IndentWithSpace.php index 7ba7a49..56a82c4 100644 --- a/src/PSR2IndentWithSpace.php +++ b/src/Fixers/PSR2/IndentWithSpace.php @@ -1,8 +1,11 @@ leftUsefulTokenIs([ - T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST, + T_NS_SEPARATOR, + T_AS, + T_CLASS, + T_EXTENDS, + T_IMPLEMENTS, + T_INSTANCEOF, + T_INTERFACE, + T_NEW, + T_NS_SEPARATOR, + T_PAAMAYIM_NEKUDOTAYIM, + T_USE, + T_TRAIT, + T_INSTEADOF, + T_CONST, ]) && !$this->rightUsefulTokenIs([ - T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST, + T_NS_SEPARATOR, + T_AS, + T_CLASS, + T_EXTENDS, + T_IMPLEMENTS, + T_INSTANCEOF, + T_INTERFACE, + T_NEW, + T_NS_SEPARATOR, + T_PAAMAYIM_NEKUDOTAYIM, + T_USE, + T_TRAIT, + T_INSTEADOF, + T_CONST, ]) ) || in_array($lcText, static::$reservedWords) diff --git a/tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php b/tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php index 815f9e4..f2b5f9d 100644 --- a/tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php +++ b/tests/Unit/Fixers/PSR1/ClassConstants/fileWithoutConstants.php @@ -7,4 +7,4 @@ public function somethingElseHere() { // just to fill something else a part from the constants. } -} \ No newline at end of file +} diff --git a/tests/Unit/Fixers/PSR2/IndentWithSpace/IndentWithSpaceTest.php b/tests/Unit/Fixers/PSR2/IndentWithSpace/IndentWithSpaceTest.php new file mode 100644 index 0000000..9ab71a4 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/IndentWithSpace/IndentWithSpaceTest.php @@ -0,0 +1,35 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + $class = new IndentWithSpace; + + $this->assertTrue($class->candidate($sourceTrue, null)); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + $expectedWithTwoSpaces = $this->getSource(__DIR__ . '/expectedWithTwoSpaces.php'); + + $class = new IndentWithSpace; + $classWithTwoSpaces = new IndentWithSpace(2); + + $fixedSource = $class->format($source); + $fixedSourceWithTwoSpaces = $classWithTwoSpaces->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + $this->assertTrue(strcmp($fixedSourceWithTwoSpaces, $expectedWithTwoSpaces) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/IndentWithSpace/expected.php b/tests/Unit/Fixers/PSR2/IndentWithSpace/expected.php new file mode 100644 index 0000000..d8eea77 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/IndentWithSpace/expected.php @@ -0,0 +1,33 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if ($this->testArray[1] == 'array') { + die(); + } elseif ($this->testArray[1] != 'array') { + echo 'something'; + } else { + $this->something = clone $this->testArray; + } + } + } +} diff --git a/tests/Unit/Fixers/PSR2/IndentWithSpace/expectedWithTwoSpaces.php b/tests/Unit/Fixers/PSR2/IndentWithSpace/expectedWithTwoSpaces.php new file mode 100644 index 0000000..10b6b00 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/IndentWithSpace/expectedWithTwoSpaces.php @@ -0,0 +1,33 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if ($this->testArray[1] == 'array') { + die(); + } elseif ($this->testArray[1] != 'array') { + echo 'something'; + } else { + $this->something = clone $this->testArray; + } + } + } +} diff --git a/tests/Unit/Fixers/PSR2/IndentWithSpace/wrong.php b/tests/Unit/Fixers/PSR2/IndentWithSpace/wrong.php new file mode 100644 index 0000000..cd04110 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/IndentWithSpace/wrong.php @@ -0,0 +1,33 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if ($this->testArray[1] == 'array') { + die(); + } elseif ($this->testArray[1] != 'array') { + echo 'something'; + } else { + $this->something = clone $this->testArray; + } + } + } +} diff --git a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php index 9365f73..9b685de 100644 --- a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php +++ b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/expected.php @@ -21,13 +21,11 @@ public function __construct() // code... break; - if($this->testArray[1] == 'array') { + if ($this->testArray[1] == 'array') { die(); - } - elseif($this->testArray[1] != 'array') { + } elseif ($this->testArray[1] != 'array') { echo 'something'; - } - else { + } else { $this->something = clone $this->testArray; } } diff --git a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php index 9d44d8f..2fd2b30 100644 --- a/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php +++ b/tests/Unit/Fixers/PSR2/KeywordsLowerCase/wrong.php @@ -21,13 +21,11 @@ // code... break; - IF($this->testArray[1] == 'array') { + IF ($this->testArray[1] == 'array') { DIE(); - } - ELSEIF($this->testArray[1] != 'array') { + } ELSEIF ($this->testArray[1] != 'array') { ECHO 'something'; - } - ELSE { + } ELSE { $this->something = CLONE $this->testArray; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 55f9c78..17261cf 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,7 +5,7 @@ // we create a new instance of BaseCodeFormatter // only because I don't want to instantiate or // copy / paste all the constants we need. -$removeFromHerePlease =new Fmt\CodeFormatter; +$removeFromHerePlease = new Fmt\CodeFormatter; // we probably need to find a better way to // accomplish this. \ No newline at end of file From 9da17a1e15371021bb765caecaa330422312bf57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 28 Apr 2017 12:30:48 +0200 Subject: [PATCH 29/50] ignored DS_Store files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4a031f..8d775bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ /build/ +.DS_Store From 8cc259ffd6396b7526f7dadc7ace06e6f45825bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 28 Apr 2017 12:31:24 +0200 Subject: [PATCH 30/50] added tests for psr2 - blank line after namespace --- src/Fixers/FixerInterface.php | 6 ++-- .../PSR2/LnAfterNamespace.php} | 7 ++-- .../LnAfterNamespace/LnAfterNamespaceTest.php | 36 +++++++++++++++++++ .../Fixers/PSR2/LnAfterNamespace/expected.php | 33 +++++++++++++++++ .../LnAfterNamespace/withoutNamespace.php | 6 ++++ .../Fixers/PSR2/LnAfterNamespace/wrong.php | 32 +++++++++++++++++ 6 files changed, 115 insertions(+), 5 deletions(-) rename src/{PSR2LnAfterNamespace.php => Fixers/PSR2/LnAfterNamespace.php} (92%) create mode 100644 tests/Unit/Fixers/PSR2/LnAfterNamespace/LnAfterNamespaceTest.php create mode 100644 tests/Unit/Fixers/PSR2/LnAfterNamespace/expected.php create mode 100644 tests/Unit/Fixers/PSR2/LnAfterNamespace/withoutNamespace.php create mode 100644 tests/Unit/Fixers/PSR2/LnAfterNamespace/wrong.php diff --git a/src/Fixers/FixerInterface.php b/src/Fixers/FixerInterface.php index ddf62b8..b0f938f 100644 --- a/src/Fixers/FixerInterface.php +++ b/src/Fixers/FixerInterface.php @@ -2,10 +2,10 @@ namespace Fmt\Fixers; -interface FixerInterface { - +interface FixerInterface +{ /** - * Accepts a string containing the source that + * Accepts a string containing the source that * the fixer needs to fix. * * @param string $source The source diff --git a/src/PSR2LnAfterNamespace.php b/src/Fixers/PSR2/LnAfterNamespace.php similarity index 92% rename from src/PSR2LnAfterNamespace.php rename to src/Fixers/PSR2/LnAfterNamespace.php index 2d0f66c..12d9e95 100644 --- a/src/PSR2LnAfterNamespace.php +++ b/src/Fixers/PSR2/LnAfterNamespace.php @@ -1,8 +1,11 @@ getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $sourceFalse = $this->getSource(__DIR__ . DIRECTORY_SEPARATOR . 'withoutNamespace.php'); + $tokensSourceFalse = $this->getTokens($sourceFalse); + + $class = new LnAfterNamespace; + + $this->assertFalse($class->candidate($sourceFalse, $tokensSourceFalse[0])); + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new LnAfterNamespace; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/LnAfterNamespace/expected.php b/tests/Unit/Fixers/PSR2/LnAfterNamespace/expected.php new file mode 100644 index 0000000..9b685de --- /dev/null +++ b/tests/Unit/Fixers/PSR2/LnAfterNamespace/expected.php @@ -0,0 +1,33 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if ($this->testArray[1] == 'array') { + die(); + } elseif ($this->testArray[1] != 'array') { + echo 'something'; + } else { + $this->something = clone $this->testArray; + } + } + } +} \ No newline at end of file diff --git a/tests/Unit/Fixers/PSR2/LnAfterNamespace/withoutNamespace.php b/tests/Unit/Fixers/PSR2/LnAfterNamespace/withoutNamespace.php new file mode 100644 index 0000000..fee763d --- /dev/null +++ b/tests/Unit/Fixers/PSR2/LnAfterNamespace/withoutNamespace.php @@ -0,0 +1,6 @@ +testArray) { + case 'an': + // do nothing, because this is only a test class... + break; + + default: + // code... + break; + + if ($this->testArray[1] == 'array') { + die(); + } elseif ($this->testArray[1] != 'array') { + echo 'something'; + } else { + $this->something = clone $this->testArray; + } + } + } +} \ No newline at end of file From 5813dc670d55161a551444db03bf983565e27a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Fri, 28 Apr 2017 15:32:27 +0200 Subject: [PATCH 31/50] added curly-open-next-line and it's tests. Modified formatterpass to accept some params --- .../PSR2/CurlyOpenNextLine.php} | 14 ++++++-- src/FormatterPass.php | 4 +-- .../CurlyOpenNextLineTest.php | 34 +++++++++++++++++++ .../PSR2/CurlyOpenNextLine/expected.php | 13 +++++++ .../Fixers/PSR2/CurlyOpenNextLine/wrong.php | 11 ++++++ .../PSR2/CurlyOpenNextLinefixedSource.php | 13 +++++++ 6 files changed, 84 insertions(+), 5 deletions(-) rename src/{PSR2CurlyOpenNextLine.php => Fixers/PSR2/CurlyOpenNextLine.php} (93%) create mode 100644 tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php create mode 100644 tests/Unit/Fixers/PSR2/CurlyOpenNextLine/expected.php create mode 100644 tests/Unit/Fixers/PSR2/CurlyOpenNextLine/wrong.php create mode 100644 tests/Unit/Fixers/PSR2/CurlyOpenNextLinefixedSource.php diff --git a/src/PSR2CurlyOpenNextLine.php b/src/Fixers/PSR2/CurlyOpenNextLine.php similarity index 93% rename from src/PSR2CurlyOpenNextLine.php rename to src/Fixers/PSR2/CurlyOpenNextLine.php index 4cd9432..b80a44d 100644 --- a/src/PSR2CurlyOpenNextLine.php +++ b/src/Fixers/PSR2/CurlyOpenNextLine.php @@ -1,9 +1,17 @@ indentChar = " "; + } + public function candidate($source, $foundTokens) { return true; @@ -55,7 +63,7 @@ public function format($source) $touchedLn = true; } if (ST_CURLY_OPEN === $id && !$touchedLn) { - $this->appendCode($this->getCrlfIndent()); + $this->appendCode($this->getCrlfIndent(3)); prev($this->tkns); break; } elseif (ST_CURLY_OPEN === $id) { diff --git a/src/FormatterPass.php b/src/FormatterPass.php index 4ef76fc..fec1f66 100644 --- a/src/FormatterPass.php +++ b/src/FormatterPass.php @@ -85,9 +85,9 @@ protected function getCrlf() return $this->newLine; } - protected function getCrlfIndent() + protected function getCrlfIndent($increment = 0) { - return $this->getCrlf().$this->getIndent(); + return $this->getCrlf() . $this->getIndent($increment); } protected function getIndent($increment = 0) diff --git a/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php new file mode 100644 index 0000000..da8ae9c --- /dev/null +++ b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php @@ -0,0 +1,34 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new CurlyOpenNextLine; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new CurlyOpenNextLine; + + $fixedSource = $class->format($source); + + file_put_contents('fixedSource.php', $fixedSource); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/expected.php b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/expected.php new file mode 100644 index 0000000..e3f3b9a --- /dev/null +++ b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/expected.php @@ -0,0 +1,13 @@ + Date: Fri, 28 Apr 2017 23:52:30 +0200 Subject: [PATCH 32/50] ModifierVisibilityStaticOrder Tests --- .../PSR2/ModifierVisibilityStaticOrder.php} | 24 +++++++------- .../PSR2/CurlyOpenNextLinefixedSource.php | 13 -------- .../ModifierVisibilityStaticOrderTest.php | 32 +++++++++++++++++++ .../expected.php | 24 ++++++++++++++ .../withoutNamespace.php | 6 ++++ .../ModifierVisibilityStaticOrder/wrong.php | 24 ++++++++++++++ 6 files changed, 99 insertions(+), 24 deletions(-) rename src/{PSR2ModifierVisibilityStaticOrder.php => Fixers/PSR2/ModifierVisibilityStaticOrder.php} (91%) delete mode 100644 tests/Unit/Fixers/PSR2/CurlyOpenNextLinefixedSource.php create mode 100644 tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/ModifierVisibilityStaticOrderTest.php create mode 100644 tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/expected.php create mode 100644 tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/withoutNamespace.php create mode 100644 tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/wrong.php diff --git a/src/PSR2ModifierVisibilityStaticOrder.php b/src/Fixers/PSR2/ModifierVisibilityStaticOrder.php similarity index 91% rename from src/PSR2ModifierVisibilityStaticOrder.php rename to src/Fixers/PSR2/ModifierVisibilityStaticOrder.php index 83af785..211d218 100644 --- a/src/PSR2ModifierVisibilityStaticOrder.php +++ b/src/Fixers/PSR2/ModifierVisibilityStaticOrder.php @@ -1,20 +1,22 @@ getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new ModifierVisibilityStaticOrder; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new ModifierVisibilityStaticOrder; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/expected.php b/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/expected.php new file mode 100644 index 0000000..e78bc3a --- /dev/null +++ b/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/expected.php @@ -0,0 +1,24 @@ + Date: Sat, 29 Apr 2017 00:12:35 +0200 Subject: [PATCH 33/50] Oops, debuging the fixer... --- src/PSR2SingleEmptyLineAndStripClosingTag.php | 37 ------------------- .../CurlyOpenNextLineTest.php | 2 - .../withoutNamespace.php | 6 --- 3 files changed, 45 deletions(-) delete mode 100644 src/PSR2SingleEmptyLineAndStripClosingTag.php delete mode 100644 tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/withoutNamespace.php diff --git a/src/PSR2SingleEmptyLineAndStripClosingTag.php b/src/PSR2SingleEmptyLineAndStripClosingTag.php deleted file mode 100644 index fdaa30a..0000000 --- a/src/PSR2SingleEmptyLineAndStripClosingTag.php +++ /dev/null @@ -1,37 +0,0 @@ -tkns = token_get_all($source); - $tokenCount = count($this->tkns) - 1; - while (list($index, $token) = each($this->tkns)) { - list($id) = $this->getToken($token); - $this->ptr = $index; - if (T_INLINE_HTML == $id && $this->ptr != $tokenCount) { - return $source; - } - } - - list($id, $text) = $this->getToken(end($this->tkns)); - $this->ptr = key($this->tkns); - - if (T_CLOSE_TAG == $id && $this->leftUsefulTokenIs([ST_CURLY_CLOSE, ST_SEMI_COLON])) { - unset($this->tkns[$this->ptr]); - } elseif (T_INLINE_HTML == $id && '' == trim($text) && $this->leftTokenIs(T_CLOSE_TAG)) { - unset($this->tkns[$this->ptr]); - $ptr = $this->leftTokenIdx([]); - unset($this->tkns[$ptr]); - } - - return rtrim($this->render()).$this->newLine; - } -} diff --git a/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php index da8ae9c..b66fc28 100644 --- a/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php +++ b/tests/Unit/Fixers/PSR2/CurlyOpenNextLine/CurlyOpenNextLineTest.php @@ -26,8 +26,6 @@ public function testFormat() $fixedSource = $class->format($source); - file_put_contents('fixedSource.php', $fixedSource); - $this->assertFalse(strcmp($source, $expected) === 0); $this->assertTrue(strcmp($fixedSource, $expected) === 0); } diff --git a/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/withoutNamespace.php b/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/withoutNamespace.php deleted file mode 100644 index fee763d..0000000 --- a/tests/Unit/Fixers/PSR2/ModifierVisibilityStaticOrder/withoutNamespace.php +++ /dev/null @@ -1,6 +0,0 @@ - Date: Sat, 29 Apr 2017 00:19:48 +0200 Subject: [PATCH 34/50] SingleEmptyLineAndStripClosingTag tests --- .../SingleEmptyLineAndStripClosingTag.php | 40 +++++++++++++++++++ .../SingleEmptyLineAndStripClosingTagTest.php | 32 +++++++++++++++ .../expected.php | 11 +++++ .../wrong.php | 12 ++++++ 4 files changed, 95 insertions(+) create mode 100644 src/Fixers/PSR2/SingleEmptyLineAndStripClosingTag.php create mode 100644 tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php create mode 100644 tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/expected.php create mode 100644 tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/wrong.php diff --git a/src/Fixers/PSR2/SingleEmptyLineAndStripClosingTag.php b/src/Fixers/PSR2/SingleEmptyLineAndStripClosingTag.php new file mode 100644 index 0000000..3e31b1d --- /dev/null +++ b/src/Fixers/PSR2/SingleEmptyLineAndStripClosingTag.php @@ -0,0 +1,40 @@ +tkns = token_get_all($source); + $tokenCount = count($this->tkns) - 1; + while (list($index, $token) = each($this->tkns)) { + list($id) = $this->getToken($token); + $this->ptr = $index; + if (T_INLINE_HTML == $id && $this->ptr != $tokenCount) { + return $source; + } + } + + list($id, $text) = $this->getToken(end($this->tkns)); + $this->ptr = key($this->tkns); + + if (T_CLOSE_TAG == $id && $this->leftUsefulTokenIs([ST_CURLY_CLOSE, ST_SEMI_COLON])) { + unset($this->tkns[$this->ptr]); + } elseif (T_INLINE_HTML == $id && '' == trim($text) && $this->leftTokenIs(T_CLOSE_TAG)) { + unset($this->tkns[$this->ptr]); + $ptr = $this->leftTokenIdx([]); + unset($this->tkns[$ptr]); + } + + return rtrim($this->render()).$this->newLine; + } +} diff --git a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php new file mode 100644 index 0000000..cf388c9 --- /dev/null +++ b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new SingleEmptyLineAndStripClosingTag; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new SingleEmptyLineAndStripClosingTag; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/expected.php b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/expected.php new file mode 100644 index 0000000..801f8eb --- /dev/null +++ b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/expected.php @@ -0,0 +1,11 @@ + \ No newline at end of file From 6cc3945af4bff3a28804ec54558220e49d2caa8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 00:45:26 +0200 Subject: [PATCH 35/50] rollback a change to how to indent with spaces as default, rethinking... --- src/Fixers/PSR2/CurlyOpenNextLine.php | 2 +- src/FormatterPass.php | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Fixers/PSR2/CurlyOpenNextLine.php b/src/Fixers/PSR2/CurlyOpenNextLine.php index b80a44d..9e3d66f 100644 --- a/src/Fixers/PSR2/CurlyOpenNextLine.php +++ b/src/Fixers/PSR2/CurlyOpenNextLine.php @@ -63,7 +63,7 @@ public function format($source) $touchedLn = true; } if (ST_CURLY_OPEN === $id && !$touchedLn) { - $this->appendCode($this->getCrlfIndent(3)); + $this->appendCode($this->getCrlfIndent()); prev($this->tkns); break; } elseif (ST_CURLY_OPEN === $id) { diff --git a/src/FormatterPass.php b/src/FormatterPass.php index fec1f66..b6e5760 100644 --- a/src/FormatterPass.php +++ b/src/FormatterPass.php @@ -11,6 +11,9 @@ abstract class FormatterPass protected $ignoreFutileTokens = [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]; protected $indent = 0; + + // if we are using spaces, how many to indent a tab? + protected $indentBlock = 4; protected $indentChar = "\t"; @@ -85,9 +88,9 @@ protected function getCrlf() return $this->newLine; } - protected function getCrlfIndent($increment = 0) + protected function getCrlfIndent() { - return $this->getCrlf() . $this->getIndent($increment); + return $this->getCrlf() . $this->getIndent(); } protected function getIndent($increment = 0) @@ -842,7 +845,11 @@ protected function scanAndReplaceCurly(&$tkns, &$ptr, $start, $call, $lookFor) protected function setIndent($increment) { + if ($this->indentChar === " ") { + $increment = $this->indentBlock * $increment; + } $this->indent += $increment; + if ($this->indent < 0) { $this->indent = 0; } From 95e7da77345af4ac71d512b5a436ff78c7e5366c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 00:45:50 +0200 Subject: [PATCH 36/50] already moved to it's new place --- src/PSR1BOMMark.php | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 src/PSR1BOMMark.php diff --git a/src/PSR1BOMMark.php b/src/PSR1BOMMark.php deleted file mode 100644 index d10e351..0000000 --- a/src/PSR1BOMMark.php +++ /dev/null @@ -1,18 +0,0 @@ - Date: Sat, 29 Apr 2017 01:14:03 +0200 Subject: [PATCH 37/50] removed some classes that were duplicated --- src/PSR1ClassConstants.php | 41 ---------------- src/PSR1ClassNames.php | 47 ------------------ src/PSR1MethodNames.php | 67 -------------------------- src/PSR2KeywordsLowerCase.php | 90 ----------------------------------- 4 files changed, 245 deletions(-) delete mode 100644 src/PSR1ClassConstants.php delete mode 100644 src/PSR1ClassNames.php delete mode 100644 src/PSR1MethodNames.php delete mode 100644 src/PSR2KeywordsLowerCase.php diff --git a/src/PSR1ClassConstants.php b/src/PSR1ClassConstants.php deleted file mode 100644 index 2c0652c..0000000 --- a/src/PSR1ClassConstants.php +++ /dev/null @@ -1,41 +0,0 @@ -tkns = token_get_all($source); - $this->code = ''; - $ucConst = false; - while (list($index, $token) = each($this->tkns)) { - list($id, $text) = $this->getToken($token); - $this->ptr = $index; - switch ($id) { - case T_CONST: - $ucConst = true; - $this->appendCode($text); - break; - case T_STRING: - if ($ucConst) { - $text = strtoupper($text); - $ucConst = false; - } - $this->appendCode($text); - break; - default: - $this->appendCode($text); - break; - } - } - return $this->code; - } -} diff --git a/src/PSR1ClassNames.php b/src/PSR1ClassNames.php deleted file mode 100644 index addf6d3..0000000 --- a/src/PSR1ClassNames.php +++ /dev/null @@ -1,47 +0,0 @@ -tkns = token_get_all($source); - $this->code = ''; - $foundClass = false; - while (list($index, $token) = each($this->tkns)) { - list($id, $text) = $this->getToken($token); - $this->ptr = $index; - switch ($id) { - case T_CLASS: - if (!$this->leftUsefulTokenIs(T_DOUBLE_COLON)) { - $foundClass = true; - } - $this->appendCode($text); - break; - case T_STRING: - if ($foundClass) { - $count = 0; - $tmp = ucwords(str_replace(['-', '_'], ' ', strtolower($text), $count)); - if ($count > 0) { - $text = str_replace(' ', '', $tmp); - } - $this->appendCode($text); - $foundClass = false; - break; - } - default: - $this->appendCode($text); - break; - } - } - return $this->code; - } -} diff --git a/src/PSR1MethodNames.php b/src/PSR1MethodNames.php deleted file mode 100644 index 9138682..0000000 --- a/src/PSR1MethodNames.php +++ /dev/null @@ -1,67 +0,0 @@ -tkns = token_get_all($source); - $this->code = ''; - $foundMethod = false; - $methodReplaceList = []; - while (list($index, $token) = each($this->tkns)) { - list($id, $text) = $this->getToken($token); - $this->ptr = $index; - switch ($id) { - case T_FUNCTION: - $foundMethod = true; - $this->appendCode($text); - break; - case T_STRING: - if ($foundMethod) { - $count = 0; - $origText = $text; - $tmp = ucwords(str_replace(['-', '_'], ' ', strtolower($text), $count)); - if ($count > 0 && '' !== trim($tmp) && '_' !== substr($text, 0, 1)) { - $text = lcfirst(str_replace(' ', '', $tmp)); - } - $methodReplaceList[$origText] = $text; - $this->appendCode($text); - $foundMethod = false; - break; - } - case ST_PARENTHESES_OPEN: - $foundMethod = false; - default: - $this->appendCode($text); - break; - } - } - $this->tkns = token_get_all($this->code); - $this->code = ''; - while (list($index, $token) = each($this->tkns)) { - list($id, $text) = $this->getToken($token); - $this->ptr = $index; - switch ($id) { - case T_STRING: - if (isset($methodReplaceList[$text]) && $this->rightUsefulTokenIs(ST_PARENTHESES_OPEN)) { - $this->appendCode($methodReplaceList[$text]); - break; - } - default: - $this->appendCode($text); - break; - } - } - return $this->code; - } -} diff --git a/src/PSR2KeywordsLowerCase.php b/src/PSR2KeywordsLowerCase.php deleted file mode 100644 index abb872f..0000000 --- a/src/PSR2KeywordsLowerCase.php +++ /dev/null @@ -1,90 +0,0 @@ - 1, - 'abstract' => 1, 'and' => 1, 'array' => 1, 'as' => 1, - 'break' => 1, - 'callable' => 1, 'case' => 1, 'catch' => 1, 'class' => 1, 'clone' => 1, 'const' => 1, 'continue' => 1, - 'declare' => 1, 'default' => 1, 'die' => 1, 'do' => 1, - 'echo' => 1, 'else' => 1, 'elseif' => 1, 'empty' => 1, 'enddeclare' => 1, 'endfor' => 1, 'endforeach' => 1, 'endif' => 1, 'endswitch' => 1, 'endwhile' => 1, 'eval' => 1, 'exit' => 1, 'extends' => 1, - 'final' => 1, 'for' => 1, 'foreach' => 1, 'function' => 1, - 'global' => 1, 'goto' => 1, - 'if' => 1, 'implements' => 1, 'include' => 1, 'include_once' => 1, 'instanceof' => 1, 'insteadof' => 1, 'interface' => 1, 'isset' => 1, - 'list' => 1, - 'namespace' => 1, 'new' => 1, - 'or' => 1, - 'print' => 1, 'private' => 1, 'protected' => 1, 'public' => 1, - 'require' => 1, 'require_once' => 1, 'return' => 1, - 'static' => 1, 'switch' => 1, - 'throw' => 1, 'trait' => 1, 'try' => 1, - 'unset' => 1, 'use' => 1, 'var' => 1, - 'while' => 1, 'xor' => 1, - ]; - - public function candidate($source, $foundTokens) - { - return true; - } - - public function format($source) - { - $this->tkns = token_get_all($source); - $this->code = ''; - while (list($index, $token) = each($this->tkns)) { - list($id, $text) = $this->getToken($token); - $this->ptr = $index; - if ( - T_WHITESPACE == $id || - T_VARIABLE == $id || - T_INLINE_HTML == $id || - T_COMMENT == $id || - T_DOC_COMMENT == $id || - T_CONSTANT_ENCAPSED_STRING == $id - ) { - $this->appendCode($text); - continue; - } - - if ( - T_STRING == $id - && $this->leftUsefulTokenIs([T_DOUBLE_COLON, T_OBJECT_OPERATOR]) - ) { - $this->appendCode($text); - continue; - } - - if (T_START_HEREDOC == $id) { - $this->appendCode($text); - $this->printUntil(ST_SEMI_COLON); - continue; - } - if (ST_QUOTE == $id) { - $this->appendCode($text); - $this->printUntilTheEndOfString(); - continue; - } - $lcText = strtolower($text); - if ( - ( - ('true' === $lcText || 'false' === $lcText || 'null' === $lcText) && - !$this->leftUsefulTokenIs([ - T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST, - ]) && - !$this->rightUsefulTokenIs([ - T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST, - ]) - ) || - isset(static::$reservedWords[$lcText]) - ) { - $text = $lcText; - } - $this->appendCode($text); - } - - return $this->code; - } -} From cdfe4b0defcbcdf3ac9caf456e63b24c6d8bc3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 01:47:44 +0200 Subject: [PATCH 38/50] Reindent Switch Blocks tests --- src/Fixers/ReindentSwitchBlocks.php | 104 ++++++++++++++++++ .../ReindentSwitchBlocksTest.php | 32 ++++++ .../Fixers/ReindentSwitchBlocks/expected.php | 18 +++ .../Fixers/ReindentSwitchBlocks/wrong.php | 18 +++ 4 files changed, 172 insertions(+) create mode 100644 src/Fixers/ReindentSwitchBlocks.php create mode 100644 tests/Unit/Fixers/ReindentSwitchBlocks/ReindentSwitchBlocksTest.php create mode 100644 tests/Unit/Fixers/ReindentSwitchBlocks/expected.php create mode 100644 tests/Unit/Fixers/ReindentSwitchBlocks/wrong.php diff --git a/src/Fixers/ReindentSwitchBlocks.php b/src/Fixers/ReindentSwitchBlocks.php new file mode 100644 index 0000000..fee47e7 --- /dev/null +++ b/src/Fixers/ReindentSwitchBlocks.php @@ -0,0 +1,104 @@ +indentChar = " "; + } + + public function candidate($source, $foundTokens) + { + if (isset($foundTokens[T_SWITCH])) { + return true; + } + return false; + } + + public function format($source) + { + $this->tkns = token_get_all($source); + + $this->code = ''; + $touchedSwitch = false; + $foundStack = []; + + while (list($index, $token) = each($this->tkns)) { + list($id, $text) = $this->getToken($token); + $this->ptr = $index; + switch ($id) { + case ":": + $this->appendCode($text); + $this->setIndent(+1); + break; + + case T_BREAK: + $this->appendCode($text); + $this->setIndent(-1); + break; + + case ST_QUOTE: + $this->appendCode($text); + $this->printUntilTheEndOfString(); + break; + case T_CLOSE_TAG: + $this->appendCode($text); + $this->printUntil(T_OPEN_TAG); + break; + case T_START_HEREDOC: + $this->appendCode($text); + $this->printUntil(T_END_HEREDOC); + break; + case T_CONSTANT_ENCAPSED_STRING: + $this->appendCode($text); + break; + case T_SWITCH: + $touchedSwitch = true; + $this->appendCode($text); + break; + case T_DOLLAR_OPEN_CURLY_BRACES: + case T_CURLY_OPEN: + case ST_CURLY_OPEN: + $indentToken = $id; + $this->appendCode($text); + if ($touchedSwitch) { + $touchedSwitch = false; + $indentToken = T_SWITCH; + $this->setIndent(+1); + } + $foundStack[] = $indentToken; + break; + case ST_CURLY_CLOSE: + $poppedID = array_pop($foundStack); + if (T_SWITCH === $poppedID) { + $this->setIndent(-1); + } + $this->appendCode($text); + break; + default: + $hasLn = $this->hasLn($text); + if ($hasLn) { + $poppedID = end($foundStack); + if ( + T_SWITCH == $poppedID && + $this->rightTokenIs(ST_CURLY_CLOSE) + ) { + $this->setIndent(-1); + $text = str_replace($this->newLine, $this->newLine . $this->getIndent(), $text); + $this->setIndent(+1); + } else { + $text = str_replace($this->newLine, $this->newLine . $this->getIndent(), $text); + } + } + $this->appendCode($text); + break; + } + } + return $this->code; + } +} diff --git a/tests/Unit/Fixers/ReindentSwitchBlocks/ReindentSwitchBlocksTest.php b/tests/Unit/Fixers/ReindentSwitchBlocks/ReindentSwitchBlocksTest.php new file mode 100644 index 0000000..2a3a0db --- /dev/null +++ b/tests/Unit/Fixers/ReindentSwitchBlocks/ReindentSwitchBlocksTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new ReindentSwitchBlocks; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new ReindentSwitchBlocks; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/ReindentSwitchBlocks/expected.php b/tests/Unit/Fixers/ReindentSwitchBlocks/expected.php new file mode 100644 index 0000000..5d3a9a7 --- /dev/null +++ b/tests/Unit/Fixers/ReindentSwitchBlocks/expected.php @@ -0,0 +1,18 @@ +doSomething($variable); + break; + default: + $this->doDefaultThing($variable); + break; + } + } +} diff --git a/tests/Unit/Fixers/ReindentSwitchBlocks/wrong.php b/tests/Unit/Fixers/ReindentSwitchBlocks/wrong.php new file mode 100644 index 0000000..dbaddda --- /dev/null +++ b/tests/Unit/Fixers/ReindentSwitchBlocks/wrong.php @@ -0,0 +1,18 @@ +doSomething($variable); + break; + default: + $this->doDefaultThing($variable); + break; + } + } +} From f8cd6264da79f711ba06d4adb04fe6f0e586e98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 14:31:59 +0200 Subject: [PATCH 39/50] StripNewLineWithinClassBody class & test --- .../StripNewlineWithinClassBody.php | 35 ++++++------------- .../StripNewlineWithinClassBodyTest.php | 32 +++++++++++++++++ .../StripNewlineWithinClassBody/expected.php | 11 ++++++ .../StripNewlineWithinClassBody/wrong.php | 13 +++++++ 4 files changed, 66 insertions(+), 25 deletions(-) rename src/{ => Fixers}/StripNewlineWithinClassBody.php (86%) create mode 100644 tests/Unit/Fixers/StripNewlineWithinClassBody/StripNewlineWithinClassBodyTest.php create mode 100644 tests/Unit/Fixers/StripNewlineWithinClassBody/expected.php create mode 100644 tests/Unit/Fixers/StripNewlineWithinClassBody/wrong.php diff --git a/src/StripNewlineWithinClassBody.php b/src/Fixers/StripNewlineWithinClassBody.php similarity index 86% rename from src/StripNewlineWithinClassBody.php rename to src/Fixers/StripNewlineWithinClassBody.php index 2a37e3c..36192a7 100644 --- a/src/StripNewlineWithinClassBody.php +++ b/src/Fixers/StripNewlineWithinClassBody.php @@ -1,9 +1,17 @@ code; } - - public function getDescription() - { - return 'Strip empty lines after class opening curly brace.'; - } - - public function getExample() - { - return <<<'EOT' - -EOT; - } } diff --git a/tests/Unit/Fixers/StripNewlineWithinClassBody/StripNewlineWithinClassBodyTest.php b/tests/Unit/Fixers/StripNewlineWithinClassBody/StripNewlineWithinClassBodyTest.php new file mode 100644 index 0000000..38028dd --- /dev/null +++ b/tests/Unit/Fixers/StripNewlineWithinClassBody/StripNewlineWithinClassBodyTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new StripNewlineWithinClassBody; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new StripNewlineWithinClassBody; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/StripNewlineWithinClassBody/expected.php b/tests/Unit/Fixers/StripNewlineWithinClassBody/expected.php new file mode 100644 index 0000000..59dc091 --- /dev/null +++ b/tests/Unit/Fixers/StripNewlineWithinClassBody/expected.php @@ -0,0 +1,11 @@ + Date: Sat, 29 Apr 2017 17:16:51 +0200 Subject: [PATCH 40/50] cleanup --- src/Fixers/ReindentSwitchBlocks.php | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Fixers/ReindentSwitchBlocks.php b/src/Fixers/ReindentSwitchBlocks.php index fee47e7..0e69986 100644 --- a/src/Fixers/ReindentSwitchBlocks.php +++ b/src/Fixers/ReindentSwitchBlocks.php @@ -17,13 +17,13 @@ public function candidate($source, $foundTokens) if (isset($foundTokens[T_SWITCH])) { return true; } + return false; } public function format($source) { $this->tkns = token_get_all($source); - $this->code = ''; $touchedSwitch = false; $foundStack = []; @@ -31,8 +31,9 @@ public function format($source) while (list($index, $token) = each($this->tkns)) { list($id, $text) = $this->getToken($token); $this->ptr = $index; + switch ($id) { - case ":": + case ST_COLON: $this->appendCode($text); $this->setIndent(+1); break; @@ -46,21 +47,26 @@ public function format($source) $this->appendCode($text); $this->printUntilTheEndOfString(); break; + case T_CLOSE_TAG: $this->appendCode($text); $this->printUntil(T_OPEN_TAG); break; + case T_START_HEREDOC: $this->appendCode($text); $this->printUntil(T_END_HEREDOC); break; + case T_CONSTANT_ENCAPSED_STRING: $this->appendCode($text); break; + case T_SWITCH: $touchedSwitch = true; $this->appendCode($text); break; + case T_DOLLAR_OPEN_CURLY_BRACES: case T_CURLY_OPEN: case ST_CURLY_OPEN: @@ -73,6 +79,7 @@ public function format($source) } $foundStack[] = $indentToken; break; + case ST_CURLY_CLOSE: $poppedID = array_pop($foundStack); if (T_SWITCH === $poppedID) { @@ -80,21 +87,30 @@ public function format($source) } $this->appendCode($text); break; + default: $hasLn = $this->hasLn($text); if ($hasLn) { $poppedID = end($foundStack); - if ( - T_SWITCH == $poppedID && + if (T_SWITCH == $poppedID && $this->rightTokenIs(ST_CURLY_CLOSE) - ) { + ) { $this->setIndent(-1); - $text = str_replace($this->newLine, $this->newLine . $this->getIndent(), $text); + $text = str_replace( + $this->newLine, + $this->newLine . $this->getIndent(), + $text + ); $this->setIndent(+1); } else { - $text = str_replace($this->newLine, $this->newLine . $this->getIndent(), $text); + $text = str_replace( + $this->newLine, + $this->newLine . $this->getIndent(), + $text + ); } } + $this->appendCode($text); break; } From a94c4aa152fb56195637c0c61b789ab0c8e2bef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 19:55:05 +0200 Subject: [PATCH 41/50] add missing curly braces - class and tests --- src/AddMissingCurlyBraces.php | 93 --------- src/BaseCodeFormatter.php | 3 + src/Fixers/AddMissingCurlyBraces.php | 176 ++++++++++++++++++ src/{ => Fixers}/ReindentComments.php | 11 +- src/FormatterPass.php | 13 +- .../AddMissingCurlyBracesTest.php | 32 ++++ .../Fixers/AddMissingCurlyBraces/expected.php | 18 ++ .../Fixers/AddMissingCurlyBraces/wrong.php | 12 ++ .../SingleEmptyLineAndStripClosingTagTest.php | 4 + .../wrong.php | 3 +- 10 files changed, 263 insertions(+), 102 deletions(-) delete mode 100644 src/AddMissingCurlyBraces.php create mode 100644 src/Fixers/AddMissingCurlyBraces.php rename src/{ => Fixers}/ReindentComments.php (93%) create mode 100644 tests/Unit/Fixers/AddMissingCurlyBraces/AddMissingCurlyBracesTest.php create mode 100644 tests/Unit/Fixers/AddMissingCurlyBraces/expected.php create mode 100644 tests/Unit/Fixers/AddMissingCurlyBraces/wrong.php diff --git a/src/AddMissingCurlyBraces.php b/src/AddMissingCurlyBraces.php deleted file mode 100644 index a8355e8..0000000 --- a/src/AddMissingCurlyBraces.php +++ /dev/null @@ -1,93 +0,0 @@ -tkns = token_get_all($source); - $this->code = ''; - $touchedCurlyClose = false; - $hasCurlyOnLeft = false; - for ($index = sizeof($this->tkns) - 1; 0 <= $index; --$index) { - $token = $this->tkns[$index]; - list($id) = $this->getToken($token); - $this->ptr = $index; - - $hasCurlyOnLeft = false; - - switch ($id) { - case T_ELSE: - if ($this->rightTokenSubsetIsAtIdx($this->tkns, $this->ptr, [ST_CURLY_OPEN, ST_COLON, T_IF], $this->ignoreFutileTokens)) { - break; - } - $this->insertCurlyBraces(); - break; - - case ST_CURLY_CLOSE: - $touchedCurlyClose = true; - break; - - case T_WHILE: - if ($touchedCurlyClose) { - $touchedCurlyClose = false; - $hasCurlyOnLeft = true; - } - - case T_FOR: - case T_FOREACH: - case T_ELSEIF: - case T_IF: - $this->refWalkUsefulUntil($this->tkns, $this->ptr, ST_PARENTHESES_OPEN); - $this->refWalkBlock($this->tkns, $this->ptr, ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE); - if ( - ($hasCurlyOnLeft && $this->rightTokenSubsetIsAtIdx($this->tkns, $this->ptr, [ST_SEMI_COLON], $this->ignoreFutileTokens)) || - $this->rightTokenSubsetIsAtIdx($this->tkns, $this->ptr, [ST_CURLY_OPEN, ST_COLON, ST_SEMI_COLON], $this->ignoreFutileTokens) - ) { - break; - } - $this->insertCurlyBraces(); - break; - } - } - - return $this->render($this->tkns); - } - - private function addSemicolon() - { - if (T_CLOSE_TAG == $this->tkns[$this->ptr][0]) { - return $this->refInsert($this->tkns, $this->ptr, [ST_SEMI_COLON, ST_SEMI_COLON]); - } - ++$this->ptr; - } - - private function insertCurlyBraces() - { - $this->refSkipIfTokenIsAny($this->tkns, $this->ptr, [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]); - $this->refInsert($this->tkns, $this->ptr, [ST_CURLY_OPEN, ST_CURLY_OPEN]); - $this->refInsert($this->tkns, $this->ptr, [T_WHITESPACE, $this->newLine]); - $this->refSkipBlocks($this->tkns, $this->ptr); - $this->addSemicolon(); - $this->refInsert($this->tkns, $this->ptr, [T_WHITESPACE, $this->newLine]); - $this->refInsert($this->tkns, $this->ptr, [ST_CURLY_CLOSE, ST_CURLY_CLOSE]); - $this->refInsert($this->tkns, $this->ptr, [T_WHITESPACE, $this->newLine]); - } -} diff --git a/src/BaseCodeFormatter.php b/src/BaseCodeFormatter.php index 8f16003..70f6820 100644 --- a/src/BaseCodeFormatter.php +++ b/src/BaseCodeFormatter.php @@ -2,6 +2,9 @@ namespace Fmt; +use Fmt\Fixers\ReindentComments; +use Fmt\Fixers\AddMissingCurlyBraces; + abstract class BaseCodeFormatter { protected $passes = [ diff --git a/src/Fixers/AddMissingCurlyBraces.php b/src/Fixers/AddMissingCurlyBraces.php new file mode 100644 index 0000000..0b7a038 --- /dev/null +++ b/src/Fixers/AddMissingCurlyBraces.php @@ -0,0 +1,176 @@ +tkns = token_get_all($source); + $this->code = ''; + $touchedCurlyClose = false; + $hasCurlyOnLeft = false; + + for ($index = sizeof($this->tkns) - 1; 0 <= $index; --$index) { + $token = $this->tkns[$index]; + list($id) = $this->getToken($token); + $this->ptr = $index; + + $hasCurlyOnLeft = false; + + switch ($id) { + case T_ELSE: + if ($this->rightTokenSubsetIsAtIdx( + $this->tkns, + $this->ptr, + [ST_CURLY_OPEN, ST_COLON, T_IF], + $this->ignoreFutileTokens + )) { + break; + } + $this->insertCurlyBraces(); + break; + + case ST_CURLY_CLOSE: + $touchedCurlyClose = true; + break; + + case T_WHILE: + if ($touchedCurlyClose) { + $touchedCurlyClose = false; + $hasCurlyOnLeft = true; + } + + case T_FOR: + case T_FOREACH: + case T_ELSEIF: + case T_IF: + $this->refWalkUsefulUntil( + $this->tkns, + $this->ptr, + ST_PARENTHESES_OPEN + ); + $this->refWalkBlock( + $this->tkns, + $this->ptr, + ST_PARENTHESES_OPEN, + ST_PARENTHESES_CLOSE + ); + if (($hasCurlyOnLeft && + $this->rightTokenSubsetIsAtIdx( + $this->tkns, + $this->ptr, + [ST_SEMI_COLON], + $this->ignoreFutileTokens + )) || + $this->rightTokenSubsetIsAtIdx( + $this->tkns, + $this->ptr, + [ST_CURLY_OPEN, ST_COLON, ST_SEMI_COLON], + $this->ignoreFutileTokens + ) + ) { + break; + } + $this->insertCurlyBraces(); + break; + } + } + + return $this->render($this->tkns); + } + + private function addSemicolon() + { + if (T_CLOSE_TAG == $this->tkns[$this->ptr][0]) { + return $this->refInsert( + $this->tkns, + $this->ptr, + [ + ST_SEMI_COLON, + ST_SEMI_COLON + ] + ); + } + ++$this->ptr; + } + + private function insertCurlyBraces() + { + $this->refSkipIfTokenIsAny( + $this->tkns, + $this->ptr, + [ + T_WHITESPACE, + T_COMMENT, + T_DOC_COMMENT + ] + ); + + $this->refInsert( + $this->tkns, + $this->ptr, + [ + ST_CURLY_OPEN, + ST_CURLY_OPEN + ] + ); + + $this->refInsert( + $this->tkns, + $this->ptr, + [ + T_WHITESPACE, + $this->newLine, + // add here the indent? + ] + ); + + $this->refSkipBlocks($this->tkns, $this->ptr); + $this->addSemicolon(); + $this->refInsert( + $this->tkns, + $this->ptr, + [ + T_WHITESPACE, + $this->newLine + ] + ); + + $this->refInsert( + $this->tkns, + $this->ptr, + [ + ST_CURLY_CLOSE, + ST_CURLY_CLOSE + ] + ); + $this->refInsert( + $this->tkns, + $this->ptr, + [ + T_WHITESPACE, + $this->newLine + ] + ); + } +} diff --git a/src/ReindentComments.php b/src/Fixers/ReindentComments.php similarity index 93% rename from src/ReindentComments.php rename to src/Fixers/ReindentComments.php index 7e37be1..c2a9978 100644 --- a/src/ReindentComments.php +++ b/src/Fixers/ReindentComments.php @@ -1,8 +1,12 @@ ptr = $index; $this->tkns[$this->ptr] = [$id, $text]; if (T_COMMENT == $id) { + + if (LeftAlignComment::NON_INDENTABLE_COMMENT == $text) { continue; } @@ -32,6 +38,7 @@ public function format($source) $oldComment = current($this->commentStack); next($this->commentStack); if (substr($text, 0, 2) != '/*') { + // this is a doble slash comment continue; } diff --git a/src/FormatterPass.php b/src/FormatterPass.php index b6e5760..4da4272 100644 --- a/src/FormatterPass.php +++ b/src/FormatterPass.php @@ -665,7 +665,11 @@ protected function refWalkCurlyBlockReverse($tkns, &$ptr) protected function refWalkUsefulUntil($tkns, &$ptr, $expectedId) { do { - $ptr = $this->walkRight($tkns, $ptr, $this->ignoreFutileTokens); + $ptr = $this->walkRight( + $tkns, + $ptr, + $this->ignoreFutileTokens + ); } while ($expectedId != $tkns[$ptr][0]); } @@ -1046,10 +1050,9 @@ private function resolveTokenMatch($tkns, $idx, $token) private function walkLeft($tkns, $idx, $ignoreList) { - $i = $idx; - while (--$i >= 0 && isset($ignoreList[$tkns[$i][0]])); - - return $i; + while (--$idx >= 0 && isset($ignoreList[$tkns[$idx][0]])) { + return $idx; + } } private function walkRight($tkns, $idx, $ignoreList) diff --git a/tests/Unit/Fixers/AddMissingCurlyBraces/AddMissingCurlyBracesTest.php b/tests/Unit/Fixers/AddMissingCurlyBraces/AddMissingCurlyBracesTest.php new file mode 100644 index 0000000..656bac6 --- /dev/null +++ b/tests/Unit/Fixers/AddMissingCurlyBraces/AddMissingCurlyBracesTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AddMissingCurlyBraces; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AddMissingCurlyBraces; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AddMissingCurlyBraces/expected.php b/tests/Unit/Fixers/AddMissingCurlyBraces/expected.php new file mode 100644 index 0000000..2a4ef8f --- /dev/null +++ b/tests/Unit/Fixers/AddMissingCurlyBraces/expected.php @@ -0,0 +1,18 @@ +format($source); + + file_put_contents(__DIR__ . 'fixedSource.php', $fixedSource); + + $this->assertFalse(strcmp($source, $expected) === 0); $this->assertTrue(strcmp($fixedSource, $expected) === 0); } diff --git a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/wrong.php b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/wrong.php index d533ff6..81ce42d 100644 --- a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/wrong.php +++ b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/wrong.php @@ -8,5 +8,4 @@ public function __construct() { // Something weird. } -} -?> \ No newline at end of file +} \ No newline at end of file From 9e3b017934360274e409324370ee069f2c691f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 21:37:55 +0200 Subject: [PATCH 42/50] add missing parentheses class & tests --- src/{ => Fixers}/AddMissingParentheses.php | 26 +++++---------- .../AddMissingParenthesesTest.php | 32 +++++++++++++++++++ .../Fixers/AddMissingParentheses/expected.php | 11 +++++++ .../Fixers/AddMissingParentheses/wrong.php | 11 +++++++ 4 files changed, 62 insertions(+), 18 deletions(-) rename src/{ => Fixers}/AddMissingParentheses.php (82%) create mode 100644 tests/Unit/Fixers/AddMissingParentheses/AddMissingParenthesesTest.php create mode 100644 tests/Unit/Fixers/AddMissingParentheses/expected.php create mode 100644 tests/Unit/Fixers/AddMissingParentheses/wrong.php diff --git a/src/AddMissingParentheses.php b/src/Fixers/AddMissingParentheses.php similarity index 82% rename from src/AddMissingParentheses.php rename to src/Fixers/AddMissingParentheses.php index dc7eceb..7141f5f 100644 --- a/src/AddMissingParentheses.php +++ b/src/Fixers/AddMissingParentheses.php @@ -1,8 +1,14 @@ code; } - - public function getDescription() - { - return 'Add extra parentheses in new instantiations.'; - } - - public function getExample() - { - return <<<'EOT' - -EOT; - } } diff --git a/tests/Unit/Fixers/AddMissingParentheses/AddMissingParenthesesTest.php b/tests/Unit/Fixers/AddMissingParentheses/AddMissingParenthesesTest.php new file mode 100644 index 0000000..0044bd4 --- /dev/null +++ b/tests/Unit/Fixers/AddMissingParentheses/AddMissingParenthesesTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AddMissingParentheses; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AddMissingParentheses; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AddMissingParentheses/expected.php b/tests/Unit/Fixers/AddMissingParentheses/expected.php new file mode 100644 index 0000000..b58e984 --- /dev/null +++ b/tests/Unit/Fixers/AddMissingParentheses/expected.php @@ -0,0 +1,11 @@ + Date: Sat, 29 Apr 2017 22:51:30 +0200 Subject: [PATCH 43/50] Align const visibility equals - class and tests --- .../AlignConstVisibilityEquals.php | 33 +++---------------- .../AlignConstVisibilityEqualsTest.php | 32 ++++++++++++++++++ .../AlignConstVisibilityEquals/expected.php | 18 ++++++++++ .../AlignConstVisibilityEquals/wrong.php | 18 ++++++++++ 4 files changed, 73 insertions(+), 28 deletions(-) rename src/{ => Fixers}/AlignConstVisibilityEquals.php (85%) create mode 100644 tests/Unit/Fixers/AlignConstVisibilityEquals/AlignConstVisibilityEqualsTest.php create mode 100644 tests/Unit/Fixers/AlignConstVisibilityEquals/expected.php create mode 100644 tests/Unit/Fixers/AlignConstVisibilityEquals/wrong.php diff --git a/src/AlignConstVisibilityEquals.php b/src/Fixers/AlignConstVisibilityEquals.php similarity index 85% rename from src/AlignConstVisibilityEquals.php rename to src/Fixers/AlignConstVisibilityEquals.php index 4264630..1ef7ca2 100644 --- a/src/AlignConstVisibilityEquals.php +++ b/src/Fixers/AlignConstVisibilityEquals.php @@ -1,11 +1,13 @@ code; } - - public function getDescription() - { - return 'Vertically align "=" of visibility and const blocks.'; - } - - public function getExample() - { - return <<<'EOT' - -EOT; - } } diff --git a/tests/Unit/Fixers/AlignConstVisibilityEquals/AlignConstVisibilityEqualsTest.php b/tests/Unit/Fixers/AlignConstVisibilityEquals/AlignConstVisibilityEqualsTest.php new file mode 100644 index 0000000..c31be19 --- /dev/null +++ b/tests/Unit/Fixers/AlignConstVisibilityEquals/AlignConstVisibilityEqualsTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignConstVisibilityEquals; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignConstVisibilityEquals; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignConstVisibilityEquals/expected.php b/tests/Unit/Fixers/AlignConstVisibilityEquals/expected.php new file mode 100644 index 0000000..36a1dce --- /dev/null +++ b/tests/Unit/Fixers/AlignConstVisibilityEquals/expected.php @@ -0,0 +1,18 @@ + Date: Sat, 29 Apr 2017 23:07:55 +0200 Subject: [PATCH 44/50] Align double arrow - class and tests --- src/{ => Fixers}/AlignDoubleArrow.php | 33 ++++--------------- .../AlignDoubleArrow/AlignDoubleArrowTest.php | 32 ++++++++++++++++++ .../Unit/Fixers/AlignDoubleArrow/expected.php | 15 +++++++++ tests/Unit/Fixers/AlignDoubleArrow/wrong.php | 15 +++++++++ 4 files changed, 68 insertions(+), 27 deletions(-) rename src/{ => Fixers}/AlignDoubleArrow.php (94%) create mode 100644 tests/Unit/Fixers/AlignDoubleArrow/AlignDoubleArrowTest.php create mode 100644 tests/Unit/Fixers/AlignDoubleArrow/expected.php create mode 100644 tests/Unit/Fixers/AlignDoubleArrow/wrong.php diff --git a/src/AlignDoubleArrow.php b/src/Fixers/AlignDoubleArrow.php similarity index 94% rename from src/AlignDoubleArrow.php rename to src/Fixers/AlignDoubleArrow.php index b74dacb..c744a63 100644 --- a/src/AlignDoubleArrow.php +++ b/src/Fixers/AlignDoubleArrow.php @@ -1,8 +1,11 @@ code; } - - public function getDescription() - { - return 'Vertically align T_DOUBLE_ARROW (=>).'; - } - - public function getExample() - { - return <<<'EOT' - 1, - 22 => 22, - 333 => 333, -]; - -$a = [ - 1 => 1, - 22 => 22, - 333 => 333, -]; -?> -EOT; - } - + protected function align($maxContextCounter) { foreach ($maxContextCounter as $level => $entrances) { diff --git a/tests/Unit/Fixers/AlignDoubleArrow/AlignDoubleArrowTest.php b/tests/Unit/Fixers/AlignDoubleArrow/AlignDoubleArrowTest.php new file mode 100644 index 0000000..f59ca67 --- /dev/null +++ b/tests/Unit/Fixers/AlignDoubleArrow/AlignDoubleArrowTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignDoubleArrow; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignDoubleArrow; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignDoubleArrow/expected.php b/tests/Unit/Fixers/AlignDoubleArrow/expected.php new file mode 100644 index 0000000..64afd96 --- /dev/null +++ b/tests/Unit/Fixers/AlignDoubleArrow/expected.php @@ -0,0 +1,15 @@ + 1, + 22 => 22, + 333 => 333, + ]; + } +} diff --git a/tests/Unit/Fixers/AlignDoubleArrow/wrong.php b/tests/Unit/Fixers/AlignDoubleArrow/wrong.php new file mode 100644 index 0000000..2bd9b05 --- /dev/null +++ b/tests/Unit/Fixers/AlignDoubleArrow/wrong.php @@ -0,0 +1,15 @@ + 1, + 22 => 22, + 333 => 333, + ]; + } +} From 8605ac04ef100778279c067b31d23e6965b2ad71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 23:14:14 +0200 Subject: [PATCH 45/50] Align double slash comments - class and tests --- src/{ => Fixers}/AlignDoubleSlashComments.php | 31 ++++-------------- .../AlignDoubleSlashCommentsTest.php | 32 +++++++++++++++++++ .../AlignDoubleSlashComments/expected.php | 13 ++++++++ .../Fixers/AlignDoubleSlashComments/wrong.php | 13 ++++++++ 4 files changed, 64 insertions(+), 25 deletions(-) rename src/{ => Fixers}/AlignDoubleSlashComments.php (80%) create mode 100644 tests/Unit/Fixers/AlignDoubleSlashComments/AlignDoubleSlashCommentsTest.php create mode 100644 tests/Unit/Fixers/AlignDoubleSlashComments/expected.php create mode 100644 tests/Unit/Fixers/AlignDoubleSlashComments/wrong.php diff --git a/src/AlignDoubleSlashComments.php b/src/Fixers/AlignDoubleSlashComments.php similarity index 80% rename from src/AlignDoubleSlashComments.php rename to src/Fixers/AlignDoubleSlashComments.php index 67efe3f..52462ae 100644 --- a/src/AlignDoubleSlashComments.php +++ b/src/Fixers/AlignDoubleSlashComments.php @@ -1,8 +1,12 @@ code; } - - public function getDescription() - { - return 'Vertically align "//" comments.'; - } - - public function getExample() - { - return <<<'EOT' - -EOT; - } } diff --git a/tests/Unit/Fixers/AlignDoubleSlashComments/AlignDoubleSlashCommentsTest.php b/tests/Unit/Fixers/AlignDoubleSlashComments/AlignDoubleSlashCommentsTest.php new file mode 100644 index 0000000..08ffdbc --- /dev/null +++ b/tests/Unit/Fixers/AlignDoubleSlashComments/AlignDoubleSlashCommentsTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignDoubleSlashComments; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignDoubleSlashComments; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignDoubleSlashComments/expected.php b/tests/Unit/Fixers/AlignDoubleSlashComments/expected.php new file mode 100644 index 0000000..02b3660 --- /dev/null +++ b/tests/Unit/Fixers/AlignDoubleSlashComments/expected.php @@ -0,0 +1,13 @@ + Date: Sat, 29 Apr 2017 23:14:44 +0200 Subject: [PATCH 46/50] remove debugging function --- .../SingleEmptyLineAndStripClosingTagTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php index 13d3007..cf388c9 100644 --- a/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php +++ b/tests/Unit/Fixers/PSR2/SingleEmptyLineAndStripClosingTag/SingleEmptyLineAndStripClosingTagTest.php @@ -26,10 +26,6 @@ public function testFormat() $fixedSource = $class->format($source); - - file_put_contents(__DIR__ . 'fixedSource.php', $fixedSource); - - $this->assertFalse(strcmp($source, $expected) === 0); $this->assertTrue(strcmp($fixedSource, $expected) === 0); } From 0894bc330608da4a816b4323409cbc20d8fc5e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sat, 29 Apr 2017 23:24:45 +0200 Subject: [PATCH 47/50] align equals - class and tests --- src/{ => Fixers}/AlignEquals.php | 30 +++-------------- .../Fixers/AlignEquals/AlignEqualsTest.php | 32 +++++++++++++++++++ tests/Unit/Fixers/AlignEquals/expected.php | 13 ++++++++ tests/Unit/Fixers/AlignEquals/wrong.php | 13 ++++++++ 4 files changed, 63 insertions(+), 25 deletions(-) rename src/{ => Fixers}/AlignEquals.php (87%) create mode 100644 tests/Unit/Fixers/AlignEquals/AlignEqualsTest.php create mode 100644 tests/Unit/Fixers/AlignEquals/expected.php create mode 100644 tests/Unit/Fixers/AlignEquals/wrong.php diff --git a/src/AlignEquals.php b/src/Fixers/AlignEquals.php similarity index 87% rename from src/AlignEquals.php rename to src/Fixers/AlignEquals.php index 8089965..1ed444d 100644 --- a/src/AlignEquals.php +++ b/src/Fixers/AlignEquals.php @@ -1,11 +1,13 @@ alignPlaceholders(self::ALIGNABLE_EQUAL, $contextCounter); - return $this->code; } - - public function getDescription() - { - return 'Vertically align "=".'; - } - - public function getExample() - { - return <<<'EOT' - -EOT; - } } diff --git a/tests/Unit/Fixers/AlignEquals/AlignEqualsTest.php b/tests/Unit/Fixers/AlignEquals/AlignEqualsTest.php new file mode 100644 index 0000000..82b6ce1 --- /dev/null +++ b/tests/Unit/Fixers/AlignEquals/AlignEqualsTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignEquals; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignEquals; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignEquals/expected.php b/tests/Unit/Fixers/AlignEquals/expected.php new file mode 100644 index 0000000..8a641e1 --- /dev/null +++ b/tests/Unit/Fixers/AlignEquals/expected.php @@ -0,0 +1,13 @@ + Date: Sat, 29 Apr 2017 23:30:18 +0200 Subject: [PATCH 48/50] fixes test error --- src/AlignGroupDoubleArrow.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AlignGroupDoubleArrow.php b/src/AlignGroupDoubleArrow.php index 486206b..c4083f0 100644 --- a/src/AlignGroupDoubleArrow.php +++ b/src/AlignGroupDoubleArrow.php @@ -2,6 +2,8 @@ namespace Fmt; +use Fmt\Fixers\AlignDoubleArrow; + final class AlignGroupDoubleArrow extends AlignDoubleArrow { public function format($source) From bafbdeed042f9982849d179f6c0ffb8c3b018350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sun, 30 Apr 2017 02:15:25 +0200 Subject: [PATCH 49/50] align group double arrow - test and fixer --- src/{ => Fixers}/AlignGroupDoubleArrow.php | 94 ++++++++++++++----- .../AlignGroupDoubleArrowTest.php | 32 +++++++ .../Fixers/AlignGroupDoubleArrow/expected.php | 17 ++++ .../Fixers/AlignGroupDoubleArrow/wrong.php | 17 ++++ 4 files changed, 135 insertions(+), 25 deletions(-) rename src/{ => Fixers}/AlignGroupDoubleArrow.php (63%) create mode 100644 tests/Unit/Fixers/AlignGroupDoubleArrow/AlignGroupDoubleArrowTest.php create mode 100644 tests/Unit/Fixers/AlignGroupDoubleArrow/expected.php create mode 100644 tests/Unit/Fixers/AlignGroupDoubleArrow/wrong.php diff --git a/src/AlignGroupDoubleArrow.php b/src/Fixers/AlignGroupDoubleArrow.php similarity index 63% rename from src/AlignGroupDoubleArrow.php rename to src/Fixers/AlignGroupDoubleArrow.php index c4083f0..3f35039 100644 --- a/src/AlignGroupDoubleArrow.php +++ b/src/Fixers/AlignGroupDoubleArrow.php @@ -1,11 +1,30 @@ strposFunc = 'strpos'; + $this->substrCountFunc = 'substr_count'; + + if (function_exists('mb_strpos')) { + $this->strposFunc = 'mb_strpos'; + $this->substrCountFunc = 'mb_substr_count'; + } + } + + public function candidate($source, $token) + { + return (new AlignEquals)->candidate($source, $token); + } + public function format($source) { $this->tkns = token_get_all($source); @@ -97,32 +116,57 @@ public function format($source) return $this->code; } + + protected function align($maxContextCounter) + { + foreach ($maxContextCounter as $level => $entrances) { + foreach ($entrances as $entrance => $context) { + for ($j = 0; $j <= $context; ++$j) { + $placeholder = sprintf(self::ALIGNABLE_EQUAL, $level, $entrance, $j); + if (false === $this->strpos($this->code, $placeholder)) { + continue; + } + if (1 === $this->substrCount($this->code, $placeholder)) { + $this->code = str_replace($placeholder, '', $this->code); + continue; + } + + $lines = explode($this->newLine, $this->code); + $linesWithObjop = []; + + foreach ($lines as $idx => $line) { + if (false !== $this->strpos($line, $placeholder)) { + $linesWithObjop[] = $idx; + } + } + + $farthest = 0; + foreach ($linesWithObjop as $idx) { + $farthest = max($farthest, $this->strpos($lines[$idx], $placeholder)); + } + foreach ($linesWithObjop as $idx) { + $line = $lines[$idx]; + $current = $this->strpos($line, $placeholder); + $delta = abs($farthest - $current); + if ($delta > 0) { + $line = str_replace($placeholder, str_repeat(' ', $delta).$placeholder, $line); + $lines[$idx] = $line; + } + } + + $this->code = str_replace($placeholder, '', implode($this->newLine, $lines)); + } + } + } + } - public function getDescription() + private function strpos($code, $placeholder) { - return 'Vertically align T_DOUBLE_ARROW (=>) by line groups.'; + return call_user_func($this->strposFunc, $code, $placeholder); } - public function getExample() + private function substrCount($code, $placeholder) { - return <<<'EOT' - 1, - 22 => 22, - - 333 => 333, - 4444 => 4444, -]; - -$a = [ - 1 => 1, - 22 => 22, - - 333 => 333, - 4444 => 4444, -]; -?> -EOT; + return call_user_func($this->substrCountFunc, $code, $placeholder); } } diff --git a/tests/Unit/Fixers/AlignGroupDoubleArrow/AlignGroupDoubleArrowTest.php b/tests/Unit/Fixers/AlignGroupDoubleArrow/AlignGroupDoubleArrowTest.php new file mode 100644 index 0000000..e9a7ce6 --- /dev/null +++ b/tests/Unit/Fixers/AlignGroupDoubleArrow/AlignGroupDoubleArrowTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignGroupDoubleArrow; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignGroupDoubleArrow; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignGroupDoubleArrow/expected.php b/tests/Unit/Fixers/AlignGroupDoubleArrow/expected.php new file mode 100644 index 0000000..693065c --- /dev/null +++ b/tests/Unit/Fixers/AlignGroupDoubleArrow/expected.php @@ -0,0 +1,17 @@ + 1, + 22 => 22, + + 333 => 333, + 4444 => 4444, + ]; + } +} diff --git a/tests/Unit/Fixers/AlignGroupDoubleArrow/wrong.php b/tests/Unit/Fixers/AlignGroupDoubleArrow/wrong.php new file mode 100644 index 0000000..f8aa5ca --- /dev/null +++ b/tests/Unit/Fixers/AlignGroupDoubleArrow/wrong.php @@ -0,0 +1,17 @@ + 1, + 22 => 22, + + 333 => 333, + 4444 => 4444, + ]; + } +} From ce6c800a75ac01f11aa3553c9fefa616e25017ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Soler?= Date: Sun, 30 Apr 2017 02:56:36 +0200 Subject: [PATCH 50/50] align php code - fixers and tests --- src/{ => Fixers}/AlignPHPCode.php | 25 ++++----------- src/{ => Fixers}/AlignPHPCode2.php | 23 +++---------- .../Fixers/AlignPHPCode/AlignPHPCodeTest.php | 32 +++++++++++++++++++ tests/Unit/Fixers/AlignPHPCode/expected.php | 5 +++ tests/Unit/Fixers/AlignPHPCode/wrong.php | 5 +++ .../AlignPHPCode2/AlignPHPCode2Test.php | 32 +++++++++++++++++++ tests/Unit/Fixers/AlignPHPCode2/expected.php | 5 +++ .../Unit/Fixers/AlignPHPCode2/fixedSource.php | 5 +++ tests/Unit/Fixers/AlignPHPCode2/wrong.php | 5 +++ 9 files changed, 100 insertions(+), 37 deletions(-) rename src/{ => Fixers}/AlignPHPCode.php (91%) rename src/{ => Fixers}/AlignPHPCode2.php (90%) create mode 100644 tests/Unit/Fixers/AlignPHPCode/AlignPHPCodeTest.php create mode 100644 tests/Unit/Fixers/AlignPHPCode/expected.php create mode 100644 tests/Unit/Fixers/AlignPHPCode/wrong.php create mode 100644 tests/Unit/Fixers/AlignPHPCode2/AlignPHPCode2Test.php create mode 100644 tests/Unit/Fixers/AlignPHPCode2/expected.php create mode 100644 tests/Unit/Fixers/AlignPHPCode2/fixedSource.php create mode 100644 tests/Unit/Fixers/AlignPHPCode2/wrong.php diff --git a/src/AlignPHPCode.php b/src/Fixers/AlignPHPCode.php similarity index 91% rename from src/AlignPHPCode.php rename to src/Fixers/AlignPHPCode.php index 8aea75c..2f2d5fa 100644 --- a/src/AlignPHPCode.php +++ b/src/Fixers/AlignPHPCode.php @@ -1,11 +1,14 @@ code; } - - public function getDescription() - { - return 'Align PHP code within HTML block.'; - } - - public function getExample() - { - return <<<'EOT' -
- -
-EOT; - } } diff --git a/src/AlignPHPCode2.php b/src/Fixers/AlignPHPCode2.php similarity index 90% rename from src/AlignPHPCode2.php rename to src/Fixers/AlignPHPCode2.php index 71abf1f..21cfc54 100644 --- a/src/AlignPHPCode2.php +++ b/src/Fixers/AlignPHPCode2.php @@ -1,8 +1,11 @@ code; } - - public function getDescription() - { - return 'Align PHP code within opening and closing php block.'; - } - - public function getExample() - { - return <<<'EOT' -
- -
-EOT; - } } diff --git a/tests/Unit/Fixers/AlignPHPCode/AlignPHPCodeTest.php b/tests/Unit/Fixers/AlignPHPCode/AlignPHPCodeTest.php new file mode 100644 index 0000000..36f2e9b --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode/AlignPHPCodeTest.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignPHPCode; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignPHPCode; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignPHPCode/expected.php b/tests/Unit/Fixers/AlignPHPCode/expected.php new file mode 100644 index 0000000..3a3d784 --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode/expected.php @@ -0,0 +1,5 @@ +
+ +
diff --git a/tests/Unit/Fixers/AlignPHPCode/wrong.php b/tests/Unit/Fixers/AlignPHPCode/wrong.php new file mode 100644 index 0000000..aa09f03 --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode/wrong.php @@ -0,0 +1,5 @@ +
+ +
diff --git a/tests/Unit/Fixers/AlignPHPCode2/AlignPHPCode2Test.php b/tests/Unit/Fixers/AlignPHPCode2/AlignPHPCode2Test.php new file mode 100644 index 0000000..fe2159b --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode2/AlignPHPCode2Test.php @@ -0,0 +1,32 @@ +getSource(__DIR__ . DIRECTORY_SEPARATOR . 'wrong.php'); + $tokensSourceTrue = $this->getTokens($sourceTrue); + + $class = new AlignPHPCode2; + + $this->assertTrue($class->candidate($sourceTrue, $tokensSourceTrue[0])); + } + + public function testFormat() + { + $source = $this->getSource(__DIR__ . '/wrong.php'); + $expected = $this->getSource(__DIR__ . '/expected.php'); + + $class = new AlignPHPCode2; + + $fixedSource = $class->format($source); + + $this->assertFalse(strcmp($source, $expected) === 0); + $this->assertTrue(strcmp($fixedSource, $expected) === 0); + } +} diff --git a/tests/Unit/Fixers/AlignPHPCode2/expected.php b/tests/Unit/Fixers/AlignPHPCode2/expected.php new file mode 100644 index 0000000..abb7684 --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode2/expected.php @@ -0,0 +1,5 @@ +
+ +
diff --git a/tests/Unit/Fixers/AlignPHPCode2/fixedSource.php b/tests/Unit/Fixers/AlignPHPCode2/fixedSource.php new file mode 100644 index 0000000..abb7684 --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode2/fixedSource.php @@ -0,0 +1,5 @@ +
+ +
diff --git a/tests/Unit/Fixers/AlignPHPCode2/wrong.php b/tests/Unit/Fixers/AlignPHPCode2/wrong.php new file mode 100644 index 0000000..e6f7cec --- /dev/null +++ b/tests/Unit/Fixers/AlignPHPCode2/wrong.php @@ -0,0 +1,5 @@ +
+ +