Browse Source

BlankLineBeforeStatementFixer - add "yield from"

SpacePossum 5 years ago
parent
commit
8fb8bb6404

+ 3 - 3
README.rst

@@ -318,9 +318,9 @@ Choose from the list of available rules:
   - ``statements`` (a subset of ``['break', 'case', 'continue', 'declare',
     'default', 'die', 'do', 'exit', 'for', 'foreach', 'goto', 'if',
     'include', 'include_once', 'require', 'require_once', 'return',
-    'switch', 'throw', 'try', 'while', 'yield']``): list of statements which
-    must be preceded by an empty line; defaults to ``['break', 'continue',
-    'declare', 'return', 'throw', 'try']``
+    'switch', 'throw', 'try', 'while', 'yield', 'yield_from']``): list of
+    statements which must be preceded by an empty line; defaults to
+    ``['break', 'continue', 'declare', 'return', 'throw', 'try']``
 
 * **braces** [@PSR2, @Symfony, @PhpCsFixer]
 

+ 1 - 0
php-cs-fixer

@@ -57,6 +57,7 @@ if (class_exists('Phar')) {
     // Maybe this file is used as phar-stub? Let's try!
     try {
         Phar::mapPhar('php-cs-fixer.phar');
+
         require_once 'phar://php-cs-fixer.phar/vendor/autoload.php';
         $require = false;
     } catch (PharException $e) {

+ 25 - 15
src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php

@@ -40,7 +40,7 @@ final class BlankLineBeforeStatementFixer extends AbstractFixer implements Confi
         'continue' => T_CONTINUE,
         'declare' => T_DECLARE,
         'default' => T_DEFAULT,
-        'die' => T_EXIT,
+        'die' => T_EXIT, // TODO remove this alias 3.0, use `exit`
         'do' => T_DO,
         'exit' => T_EXIT,
         'for' => T_FOR,
@@ -64,6 +64,19 @@ final class BlankLineBeforeStatementFixer extends AbstractFixer implements Confi
      */
     private $fixTokenMap = [];
 
+    /**
+     * Dynamic yield from option set on constructor.
+     */
+    public function __construct()
+    {
+        parent::__construct();
+
+        // To be moved back to compile time property declaration when PHP support of PHP CS Fixer will be 7.0+
+        if (\defined('T_YIELD_FROM')) {
+            self::$tokenMap['yield_from'] = T_YIELD_FROM;
+        }
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -74,6 +87,10 @@ final class BlankLineBeforeStatementFixer extends AbstractFixer implements Confi
         $this->fixTokenMap = [];
 
         foreach ($this->configuration['statements'] as $key) {
+            if ('die' === $key) {
+                @trigger_error('Option "die" is deprecated, use "exit" instead.', E_USER_DEPRECATED);
+            }
+
             $this->fixTokenMap[$key] = self::$tokenMap[$key];
         }
     }
@@ -123,19 +140,6 @@ foreach ($foo as $bar) {
                 ),
                 new CodeSample(
                     '<?php
-if ($foo === false) {
-    die(0);
-} else {
-    $bar = 9000;
-    die(1);
-}
-',
-                    [
-                        'statements' => ['die'],
-                    ]
-                ),
-                new CodeSample(
-                    '<?php
 $i = 0;
 do {
     echo $i;
@@ -313,10 +317,16 @@ if (true) {
      */
     protected function createConfigurationDefinition()
     {
+        $allowed = self::$tokenMap;
+        $allowed['yield_from'] = true; // TODO remove this when update to PHP7.0
+        ksort($allowed);
+
+        $allowed = array_keys($allowed);
+
         return new FixerConfigurationResolver([
             (new FixerOptionBuilder('statements', 'List of statements which must be preceded by an empty line.'))
                 ->setAllowedTypes(['array'])
-                ->setAllowedValues([new AllowedValueSubset(array_keys(self::$tokenMap))])
+                ->setAllowedValues([new AllowedValueSubset($allowed)])
                 ->setDefault([
                     'break',
                     'continue',

+ 19 - 1
src/RuleSet.php

@@ -250,7 +250,25 @@ final class RuleSet implements RuleSetInterface
             '@Symfony' => true,
             'align_multiline_comment' => true,
             'array_indentation' => true,
-            'blank_line_before_statement' => true,
+            'blank_line_before_statement' => [
+                'statements' => [
+                    'break',
+                    'case',
+                    'continue',
+                    'declare',
+                    'default',
+                    'exit',
+                    'goto',
+                    'include',
+                    'include_once',
+                    'require',
+                    'require_once',
+                    'return',
+                    'switch',
+                    'throw',
+                    'try',
+                ],
+            ],
             'combine_consecutive_issets' => true,
             'combine_consecutive_unsets' => true,
             'compact_nullable_typehint' => true,

+ 84 - 0
tests/Fixer/Whitespace/BlankLineBeforeStatementFixerTest.php

@@ -389,6 +389,9 @@ switch ($a) {
     }
 
     /**
+     * @group legacy
+     * @expectedDeprecation Option "die" is deprecated, use "exit" instead.
+     *
      * @dataProvider provideFixWithDieCases
      *
      * @param string      $expected
@@ -1272,6 +1275,19 @@ function foo() {
             ],
             [
                 '<?php
+function foo() {
+    yield \'b\' => $a;
+
+    yield "a" => $b;
+}',
+                '<?php
+function foo() {
+    yield \'b\' => $a;
+    yield "a" => $b;
+}',
+            ],
+            [
+                '<?php
 function foo() {
     $a = $a;
 
@@ -1294,6 +1310,74 @@ function foo() {
         ];
     }
 
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixWithYieldFromCases
+     * @requires PHP 7.0
+     */
+    public function testFixWithYieldFrom($expected, $input = null)
+    {
+        $this->fixer->configure([
+            'statements' => ['yield_from'],
+        ]);
+
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @yield array
+     */
+    public function provideFixWithYieldFromCases()
+    {
+        return [
+            [
+                '<?php
+function foo() {
+    yield from $a;
+}',
+            ],
+            [
+                '<?php
+function foo() {
+    yield from $a;
+
+    yield from $b;
+}',
+                '<?php
+function foo() {
+    yield from $a;
+    yield from $b;
+}',
+            ],
+            [
+                '<?php
+function foo() {
+    $a = $a;
+
+    yield from $a;
+
+    yield $a;
+    yield $b;
+}',
+            ],
+            [
+                '<?php
+function foo() {
+    $a = $a;
+
+    yield from $a;
+}',
+                '<?php
+function foo() {
+    $a = $a;
+    yield from $a;
+}',
+            ],
+        ];
+    }
+
     /**
      * @dataProvider provideFixWithMultipleConfigStatementsCases
      *