Repository: PHP-CS-Fixer/PHP-CS-Fixer (PHP)

Issue:
foreach declaration SpacingBeforeAs and  SpacingAfterAs expected 1 space after "as" 
```diff
--- Original
+++ New
-        foreach ( ([1, 2, 3])as        $key => $item) {
+        foreach (([1, 2, 3]) as $key => $item) {
```



Discussion:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4435 fixes the after part




Patch:
diff --git a/doc/list.rst b/doc/list.rst
index cd1e933bf11..b12eac7b517 100644
--- a/doc/list.rst
+++ b/doc/list.rst
@@ -2960,8 +2960,8 @@ List of Available Rules
      | Default value: ``['yield_from']``
    - | ``constructs_preceded_by_a_single_space``
      | List of constructs which must be preceded by a single space.
-     | Allowed values: a subset of ``['use_lambda']``
-     | Default value: ``['use_lambda']``
+     | Allowed values: a subset of ``['as', 'use_lambda']``
+     | Default value: ``['as', 'use_lambda']``
    - | ``constructs_followed_by_a_single_space``
      | List of constructs which must be followed by a single space.
      | Allowed values: a subset of ``['abstract', 'as', 'attribute', 'break', 'case', 'catch', 'class', 'clone', 'comment', 'const', 'const_import', 'continue', 'do', 'echo', 'else', 'elseif', 'enum', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'function_import', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'match', 'named_argument', 'namespace', 'new', 'open_tag_with_echo', 'php_doc', 'php_open', 'print', 'private', 'protected', 'public', 'readonly', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'type_colon', 'use', 'use_lambda', 'use_trait', 'var', 'while', 'yield', 'yield_from']``
diff --git a/doc/rules/language_construct/single_space_around_construct.rst b/doc/rules/language_construct/single_space_around_construct.rst
index dddb1e1e9a6..096e822413e 100644
--- a/doc/rules/language_construct/single_space_around_construct.rst
+++ b/doc/rules/language_construct/single_space_around_construct.rst
@@ -21,9 +21,9 @@ Default value: ``['yield_from']``
 
 List of constructs which must be preceded by a single space.
 
-Allowed values: a subset of ``['use_lambda']``
+Allowed values: a subset of ``['as', 'use_lambda']``
 
-Default value: ``['use_lambda']``
+Default value: ``['as', 'use_lambda']``
 
 ``constructs_followed_by_a_single_space``
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php b/src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php
index 9d93fba2bae..2e7fa04b641 100644
--- a/src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php
+++ b/src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php
@@ -46,7 +46,7 @@ final class SingleSpaceAroundConstructFixer extends AbstractFixer implements Con
      * @var array<string, null|int>
      */
     private static array $tokenMapPrecededByASingleSpace = [
-        // for now, only one case - but we are ready to extend it, when we learn about new cases to cover
+        'as' => T_AS,
         'use_lambda' => CT::T_USE_LAMBDA,
     ];
 


Test patch:
diff --git a/tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php b/tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php
index b65bcab0c6f..3892470ddbd 100644
--- a/tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php
+++ b/tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php
@@ -204,14 +204,12 @@ public static function provideFixWithBreakCases(): array
 
     /**
      * @dataProvider provideFixWithAsCases
+     *
+     * @param array<string, string[]> $config
      */
-    public function testFixWithAs(string $expected, ?string $input = null): void
+    public function testFixWithAs(string $expected, ?string $input = null, array $config = []): void
     {
-        $this->fixer->configure([
-            'constructs_followed_by_a_single_space' => [
-                'as',
-            ],
-        ]);
+        $this->fixer->configure($config);
 
         $this->doTest($expected, $input);
     }
@@ -222,24 +220,110 @@ public static function provideFixWithAsCases(): array
             [
                 '<?php foreach ($foo as $bar) {}',
                 '<?php foreach ($foo as$bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
             ],
             [
                 '<?php foreach ($foo as $bar) {}',
                 '<?php foreach ($foo as  $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
+            ],
+            [
+                '<?php foreach ($foo as $bar) {}',
+                '<?php foreach ($foo  as $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
             ],
             [
                 '<?php foreach ($foo as $bar) {}',
                 '<?php foreach ($foo as
 
 $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
+            ],
+            [
+                '<?php foreach ($foo as $bar) {}',
+                '<?php foreach ($foo
+as $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
             ],
             [
                 '<?php foreach ($foo as /* foo */$bar) {}',
                 '<?php foreach ($foo as/* foo */$bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
+            ],
+            [
+                '<?php foreach ($foo/* foo */ as $bar) {}',
+                '<?php foreach ($foo/* foo */as $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
             ],
             [
                 '<?php foreach ($foo as /* foo */$bar) {}',
                 '<?php foreach ($foo as  /* foo */$bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
+            ],
+            [
+                '<?php foreach ($foo /* foo */ as $bar) {}',
+                '<?php foreach ($foo /* foo */    as $bar) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php foreach (range(1, 12) as $num) {}',
+                '<?php foreach (range(1, 12)as $num) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php foreach (range(1, 12) as $num) {}',
+                '<?php foreach (range(1, 12)   as $num) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php foreach ([1, 2, 3, 4] as $int) {}',
+                '<?php foreach ([1, 2, 3, 4]as $int) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php foreach ([1, 2, 3, 4] as $int) {}',
+                '<?php foreach ([1, 2, 3, 4]
+                as $int) {}',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
             ],
             [
                 '<?php
@@ -258,6 +342,10 @@ class Foo
         Bar::baz as  bar;
     }
 }',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
             ],
             [
                 '<?php
@@ -278,6 +366,10 @@ class Foo
 bar;
     }
 }',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
             ],
             [
                 '<?php
@@ -296,6 +388,11 @@ class Foo
         Bar::baz as/* foo */bar;
     }
 }',
+
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
             ],
             [
                 '<?php
@@ -314,6 +411,138 @@ class Foo
         Bar::baz as  /* foo */bar;
     }
 }',
+                [
+                    'constructs_preceded_by_a_single_space' => [],
+                    'constructs_followed_by_a_single_space' => ['as'],
+                ],
+            ],
+            [
+                '<?php
+namespace Foo;
+
+use Bar as Baz;
+
+final class Qux extends Baz {}
+',
+                '<?php
+namespace Foo;
+
+use Bar    as Baz;
+
+final class Qux extends Baz {}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php
+namespace Foo;
+
+use Bar as Baz;
+
+final class Qux extends Baz {}
+',
+                '<?php
+namespace Foo;
+
+use Bar
+    as Baz;
+
+final class Qux extends Baz {}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php
+namespace Foo;
+
+use Bar /** foo */ as Baz;
+
+final class Qux extends Baz {}
+',
+                '<?php
+namespace Foo;
+
+use Bar /** foo */as Baz;
+
+final class Qux extends Baz {}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz as bar;
+    }
+}
+',
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz    as bar;
+    }
+}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz as bar;
+    }
+}
+',
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz
+as bar;
+    }
+}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
+            ],
+            [
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz/** foo */ as bar;
+    }
+}
+',
+                '<?php
+class Foo
+{
+    use Bar {
+        Bar::baz/** foo */as bar;
+    }
+}
+',
+                [
+                    'constructs_preceded_by_a_single_space' => ['as'],
+                    'constructs_followed_by_a_single_space' => [],
+                ],
             ],
         ];
     }