Просмотр исходного кода

minor #1616 IncludeFixer - fix and test more cases (SpacePossum)

This PR was merged into the 1.12 branch.

Discussion
----------

IncludeFixer - fix and test more cases

replaces https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/1584 which I cannot reopen

Commits
-------

7829099 Include fixer, fix and test  more cases.
Dariusz Ruminski 9 лет назад
Родитель
Сommit
d1c2010d3e
3 измененных файлов с 172 добавлено и 122 удалено
  1. 4 4
      README.rst
  2. 2 2
      Symfony/CS/Fixer/Symfony/IncludeFixer.php
  3. 166 116
      Symfony/CS/Tests/Fixer/Symfony/IncludeFixerTest.php

+ 4 - 4
README.rst

@@ -338,10 +338,10 @@ Choose from the list of available fixers:
                         typehint.
                         typehint.
 
 
 * **include** [symfony]
 * **include** [symfony]
-                        Include and file path should
-                        be divided with a single
-                        space. File path should not be
-                        placed under brackets.
+                        Include/Require and file path
+                        should be divided with a
+                        single space. File path should
+                        not be placed under brackets.
 
 
 * **join_function** [symfony]
 * **join_function** [symfony]
                         Implode function should be
                         Implode function should be

+ 2 - 2
Symfony/CS/Fixer/Symfony/IncludeFixer.php

@@ -39,7 +39,7 @@ class IncludeFixer extends AbstractFixer
      */
      */
     public function getDescription()
     public function getDescription()
     {
     {
-        return 'Include and file path should be divided with a single space. File path should not be placed under brackets.';
+        return 'Include/Require and file path should be divided with a single space. File path should not be placed under brackets.';
     }
     }
 
 
     private function clearIncludies(Tokens $tokens, array $includies)
     private function clearIncludies(Tokens $tokens, array $includies)
@@ -74,7 +74,7 @@ class IncludeFixer extends AbstractFixer
 
 
             if ($nextToken->isWhitespace()) {
             if ($nextToken->isWhitespace()) {
                 $nextToken->setContent(' ');
                 $nextToken->setContent(' ');
-            } elseif ($braces) {
+            } elseif ($braces || $tokens[$nextIndex]->isGivenKind(array(T_VARIABLE, T_CONSTANT_ENCAPSED_STRING, T_COMMENT))) {
                 $tokens->insertAt($includy['begin'] + 1, new Token(array(T_WHITESPACE, ' ')));
                 $tokens->insertAt($includy['begin'] + 1, new Token(array(T_WHITESPACE, ' ')));
             }
             }
         }
         }

+ 166 - 116
Symfony/CS/Tests/Fixer/Symfony/IncludeFixerTest.php

@@ -15,6 +15,7 @@ use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
 
 
 /**
 /**
  * @author Саша Стаменковић <umpirsky@gmail.com>
  * @author Саша Стаменковић <umpirsky@gmail.com>
+ * @author SpacePossum
  */
  */
 class IncludeFixerTest extends AbstractFixerTestBase
 class IncludeFixerTest extends AbstractFixerTestBase
 {
 {
@@ -28,121 +29,170 @@ class IncludeFixerTest extends AbstractFixerTestBase
 
 
     public function testFixProvider()
     public function testFixProvider()
     {
     {
-        return array(
-            array(
-                "<?php include 'foo.php';",
-                "<?php include   'foo.php';",
-            ),
-            array(
-                "<?php include 'foo.php';",
-                "<?php include   'foo.php'  ;",
-            ),
-            array(
-                "<?php include 'foo.php';",
-                "<?php include   ('foo.php')  ;",
-            ),
-            array(
-                '<?php include "Buzz/foo-Bar.php";',
-                '<?php include (  "Buzz/foo-Bar.php" );',
-            ),
-            array(
-                '<?php include "$buzz/foo-Bar.php";',
-                '<?php include (  "$buzz/foo-Bar.php" );',
-            ),
-            array(
-                '<?php include "{$buzz}/foo-Bar.php";',
-                '<?php include (  "{$buzz}/foo-Bar.php" );',
-            ),
-            array(
-                "<?php include 'foo.php';",
-                "<?php include('foo.php');",
-            ),
-            array(
-                "<?php include_once 'foo.php';",
-                "<?php include_once( 'foo.php' );",
-            ),
-            array(
-                '<?php require $foo ? "foo.php" : "bar.php";',
-                '<?php require($foo ? "foo.php" : "bar.php");',
-            ),
-            array(
-                '<?php require $foo  ?  "foo.php"  :  "bar.php";',
-                '<?php require($foo  ?  "foo.php"  :  "bar.php");',
-            ),
-            array(
-                "<?php return require_once __DIR__.'foo.php';",
-                "<?php return require_once  __DIR__.'foo.php';",
-            ),
-            array(
-                "<?php \$foo = require_once __DIR__.('foo.php');",
-                "<?php \$foo = require_once  __DIR__.('foo.php');",
-            ),
-            array(
-                "<?php     require_once __DIR__.('foo.php');",
-                "<?php     require_once  (__DIR__.('foo.php'));",
-            ),
-            array(
-                "<?php     require_once __DIR__ . ('foo.php');",
-                "<?php     require_once  (__DIR__ . ('foo.php'));",
-            ),
-            array(
-                "<?php require_once dirname(__FILE__).'foo.php';",
-                "<?php require_once (dirname(__FILE__).'foo.php');",
-            ),
-            array(
-                '<?php ClassCollectionLoader::load(include($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);',
-            ),
-            array(
-                "<?php require_once '\".__DIR__.\"/../bootstrap.php';",
-            ),
-            array(
-                '// require foo',
-            ),
-            array(
-                '<?php // require foo',
-            ),
-            array(
-                '* require foo',
-            ),
-            array(
-                '<?php /* require foo */',
-            ),
-            array(
-                'exit(\'POST must include "file"\');',
-            ),
-            array(
-                '<?php include_once "foo/".CONSTANT."/bar.php";',
-                '<?php include_once("foo/".CONSTANT."/bar.php");',
-            ),
-            array(
-                '<?php include_once "foo/".CONSTANT."/bar.php"; include_once "foo/".CONSTANT."/bar.php";',
-                '<?php include_once("foo/".CONSTANT."/bar.php"); include_once("foo/".CONSTANT."/bar.php");',
-            ),
-            array(
-                '<?php include_once "foo/".CONSTANT."/bar.php"; $foo = "bar";',
-                '<?php include_once("foo/".CONSTANT."/bar.php"); $foo = "bar";',
-            ),
-            array(
-                '<?php include_once "foo/".CONSTANT."/bar.php"; foo();',
-                '<?php include_once("foo/".CONSTANT."/bar.php"); foo();',
-            ),
-            array(
-                '<?php include_once "foo/" . CONSTANT . "/bar.php";',
-                '<?php include_once("foo/" . CONSTANT . "/bar.php");',
-            ),
-            array(
-                '<?php require ($a ? $b : $c) . $d;',
-            ),
-            array(
-                '<?php require($a ? $b : $c) . $d;',
-            ),
-            array(
-                '<?php $foo = (false === include($zfLibraryPath."/Zend/Loader/StandardAutoloader.php")) {$foo = "asd";};',
-            ),
-            array(
-                '<?php require_once SOME_CONST . "file.php"; require Foo::Bar($baz);',
-                '<?php require_once( SOME_CONST . "file.php" ); require Foo::Bar($baz);',
-            ),
-        );
+        $template = '<?php %s';
+        $tests = array();
+        foreach (array('require', 'require_once', 'include', 'include_once') as $statement) {
+            $tests[] = array(
+                sprintf($template.' $a;', $statement),
+                sprintf($template.'$a;', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' $a;', $statement),
+                sprintf($template.'            $a;', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." /**/'foo.php';", $statement),
+                sprintf($template."/**/'foo.php';", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." 'foo.php';", $statement),
+                sprintf($template."'foo.php';", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." 'foo.php';", $statement),
+                sprintf($template."           'foo.php';", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." 'foo.php';", $statement),
+                sprintf($template."('foo.php');", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." 'foo.php';", $statement),
+                sprintf($template."(           'foo.php');", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." 'foo.php';", $statement),
+                sprintf($template."          ( 'foo.php' );", $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php // %s foo', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php /* %s foo */', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php /** %s foo */', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.'($a ? $b : $c) . $d;', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' ($a ? $b : $c) . $d;', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php exit("POST must %s "file"");', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php")) {$foo = "asd";};', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' "Buzz/foo-Bar.php";', $statement),
+                sprintf($template.' (  "Buzz/foo-Bar.php" );', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' "$buzz/foo-Bar.php";', $statement),
+                sprintf($template.' (  "$buzz/foo-Bar.php" );', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
+                sprintf($template.' (  "{$buzz}/foo-Bar.php" );', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
+                sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
+            );
+
+            $tests[] = array(
+                sprintf($template.' $foo  ?  "foo.php"  :  "bar.php";', $statement),
+                sprintf($template.'($foo  ?  "foo.php"  :  "bar.php");', $statement),
+            );
+
+            $tests[] = array(
+                sprintf("<?php return %s __DIR__.'foo.php';", $statement),
+                sprintf("<?php return %s  __DIR__.'foo.php';", $statement),
+            );
+
+            $tests[] = array(
+                sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
+                sprintf("<?php \$foo = %s  __DIR__.('foo.php');", $statement),
+            );
+
+            $tests[] = array(
+                sprintf("<?php     %s __DIR__.('foo.php');", $statement),
+                sprintf("<?php     %s  (__DIR__.('foo.php'));", $statement),
+            );
+
+            $tests[] = array(
+                sprintf("<?php     %s __DIR__ . ('foo.php');", $statement),
+                sprintf("<?php     %s  (__DIR__ . ('foo.php'));", $statement),
+            );
+
+            $tests[] = array(
+                sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
+                sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
+                sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s "foo/".CONSTANT."/bar.php"; %s "foo/".CONSTANT."/bar.php";', $statement, $statement),
+                sprintf('<?php %s("foo/".CONSTANT."/bar.php"); %s("foo/".CONSTANT."/bar.php");', $statement, $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
+                sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
+                sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
+                sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
+            );
+
+            $tests[] = array(
+                sprintf('<?php %s SOME_CONST . "file.php"; %s Foo::Bar($baz);', $statement, $statement),
+                sprintf('<?php %s( SOME_CONST . "file.php" ); %s Foo::Bar($baz);', $statement, $statement),
+            );
+            $tests[] = array(
+                sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
+                sprintf('<?php %s          SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
+            );
+        }
+
+        return $tests;
     }
     }
 }
 }