Repository: laravel/framework (php)

Issue:
Blade renderer breaks when mixing 2 @php-syntaxes in the same view
<!-- DO NOT THROW THIS AWAY -->
<!-- Fill out the FULL versions with patch versions -->

- Laravel Version: 9.43.0
- PHP Version: 8.1.13
- Database Driver & Version: n/a

### Description:

When mixing `@php(..)` and `@php`/`@endphp` later in the view the renderer breaks and gives preference to the @php/endphp pair above the parentheses.

Same issue as just posted, but tested in V9. @driesvints your excuse of "don't use it" is from my perspective not really an excuse. If you have a view of 400+ lines add `@php/@endphp` somewhere in the bottom and you didn't notice that someone else added `@php(..)` somewhere higher up in that same file you pull your hair out on why it doesn't work. Because the errors get quite weird when mixing things with other directives in between.

### Steps To Reproduce:

See https://github.com/rikvdh/laravel-broken-blade-view/blob/v9/resources/views/welcome.blade.php

You expect 'goodbye' as output. But this breaks.

The rendered view:
```php
<?php($hello = 'welcome')
@php
    $hello = 'goodbye';
?>

<?php echo e($hello); ?>

<?php /**PATH /Users/rikvdh/tmp/broken-view/resources/views/welcome.blade.php ENDPATH**/ ?>%                  
```

<!-- laravel new bug-report --github="--public" -->


Pull request:
fix single line @php statements to not be parsed as php blocks
when @endphp is also in the same file, + unit test, fixes #45330

<!--
Please only send a pull request to branches that are currently supported: https://laravel.com/docs/releases#support-policy 

If you are unsure which branch your pull request should be sent to, please read: https://laravel.com/docs/contributions#which-branch

Pull requests without a descriptive title, thorough description, or tests will be closed.

In addition, please describe the benefit to end users; the reasons it does not break any existing features; how it makes building web applications easier, etc.
-->


Patch:
diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php
index 21f70b35a7..51b5abd69c 100644
--- a/src/Illuminate/View/Compilers/BladeCompiler.php
+++ b/src/Illuminate/View/Compilers/BladeCompiler.php
@@ -380,7 +380,7 @@ protected function storeVerbatimBlocks($value)
      */
     protected function storePhpBlocks($value)
     {
-        return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
+        return preg_replace_callback('/(?<!@)@php(?! ?\()(.*?)@endphp/s', function ($matches) {
             return $this->storeRawBlock("<?php{$matches[1]}?>");
         }, $value);
     }


Test patch:
diff --git a/tests/View/Blade/BladePhpStatementsTest.php b/tests/View/Blade/BladePhpStatementsTest.php
index 8f7a9f7079..131a2686db 100644
--- a/tests/View/Blade/BladePhpStatementsTest.php
+++ b/tests/View/Blade/BladePhpStatementsTest.php
@@ -84,4 +84,11 @@ public function testStringWithEscapingDataValue()
 
         $this->assertEquals($expected, $this->compiler->compileString($string));
     }
+
+    public function testCompilationOfMixedPhpStatements()
+    {
+        $string = '@php($set = true) @php ($hello = \'hi\') @php echo "Hello world" @endphp';
+        $expected = '<?php ($set = true); ?> <?php ($hello = \'hi\'); ?> <?php echo "Hello world" ?>';
+        $this->assertEquals($expected, $this->compiler->compileString($string));
+    }
 }