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

Issue:
`method_argument_space` fixer brakes formatting in heredoc
## Bug report

php 8.0.1
php-cs-fixer 2.18.2

### Code snippet that reproduces the problem
#### Before run cs-fixer
  ```php
<?php
echo <<<TEST
    some text some text
        {$test->test(
            'qwe',
            'rty'
        )}
    some text some text
    TEST;
  ```

#### After run cs-fixer
  ```php
<?php
echo <<<TEST
    some text some text
        {$test->test(
    'qwe',
    'rty'
)}
    some text some text
    TEST;
  ```

#### Working example

https://3v4l.org/pZgo8


Discussion:
thanks for report, @Etherlord !
as maintainers capacity is limited, would you like to give it a try and propose a PR for it ?
> thanks for report, @Etherlord !
> as maintainers capacity is limited, would you like to give it a try and propose a PR for it ?

Perhaps I can, but not in the near future 
Reproduced on `3.16`:

<img width="897" alt="image" src="https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/assets/600668/56979e7b-f3da-4ec1-8fcd-cbbddaf30a86">


Since this issue has not had any activity within the last 90 days, I have marked it as stale.

I will close it if no further activity occurs within the next 30 days.
it's still actual

---

Maintainer's note: it is reproducible on `3.22.1-DEV (145bc2fa)`.
**Maintainer's note**: reproducible on `3.38.1-DEV`.

---

Since this issue has not had any activity within the last 90 days, I have marked it as stale.

The purpose of this action is to enforce backlog review once in a while. This is mostly for maintainers and helps with keeping repository in good condition, because stale issues and PRs can accumulate over time and make it harder for others to find relevant information. It is also possible that some changes has been made to the repo already, and issue or PR became outdated, but wasn't closed for some reason. This action helps with periodic review and closing of such stale items in automated way.

You may let maintainers handle this or verify current relevancy by yourself, to help with re-triage. Any activity will remove stale label so it won't be automatically closed at this point.


I will close it if no further activity occurs within the next 30 days.
Since this issue has not had any activity within the last 90 days, I have marked it as stale.

The purpose of this action is to enforce backlog review once in a while. This is mostly for maintainers and helps with keeping repository in good condition, because stale issues and PRs can accumulate over time and make it harder for others to find relevant information. It is also possible that some changes has been made to the repo already, and issue or PR became outdated, but wasn't closed for some reason. This action helps with periodic review and closing of such stale items in automated way.

You may let maintainers handle this or verify current relevancy by yourself, to help with re-triage. Any activity will remove stale label so it won't be automatically closed at this point.

I will close it if no further activity occurs within the next 30 days.
still actual



Patch:
diff --git a/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php b/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
index 5f47784e7a9..2b6e3f2c72f 100644
--- a/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
+++ b/src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
@@ -301,7 +301,7 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, int $startFunction
         do {
             $prevWhitespaceTokenIndex = $tokens->getPrevTokenOfKind(
                 $searchIndex,
-                [[T_WHITESPACE]]
+                [[T_ENCAPSED_AND_WHITESPACE], [T_WHITESPACE]],
             );
 
             $searchIndex = $prevWhitespaceTokenIndex;
@@ -311,6 +311,8 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, int $startFunction
 
         if (null === $prevWhitespaceTokenIndex) {
             $existingIndentation = '';
+        } elseif (!$tokens[$prevWhitespaceTokenIndex]->isGivenKind(T_WHITESPACE)) {
+            return;
         } else {
             $existingIndentation = $tokens[$prevWhitespaceTokenIndex]->getContent();
             $lastLineIndex = strrpos($existingIndentation, "\n");


Test patch:
diff --git a/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php b/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php
index 7e5dc1caaf0..d2ea0cce64b 100644
--- a/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php
+++ b/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php
@@ -974,6 +974,48 @@ function foo(
 );
             ',
         ];
+
+        yield [<<<'PHP'
+            <?php
+            function f()
+            {
+                echo <<<TEXT
+                    some text
+                    {$object->method(
+                        42
+                    )}
+                    some text
+                TEXT;
+            }
+            PHP];
+
+        yield [<<<'PHP'
+            <?php
+            function f()
+            {
+                echo <<<'TEXT'
+                    some text
+                    {$object->method(
+                        42
+                    )}
+                    some text
+                TEXT;
+            }
+            PHP];
+
+        yield [<<<'PHP'
+            <?php
+            function f()
+            {
+                echo <<<TEXT
+                    some text
+                    some value: {$object->method(
+                        42
+                    )}
+                    some text
+                TEXT;
+            }
+            PHP];
     }
 
     /**