framework: [5.8] Path of the view in compiled template causes regression with declare(strict_types=1) in templates

  • Laravel Version: 5.8.2
  • PHP Version: 7.2
  • Database Driver & Version: n/a

Description:

[5.8] Provide a path of the view in compiled view added a new feature so that the first line of a compiled template contains the path to the view.

Unfortunately this breaks templates using <?php declare(strict_types = 1); in the first line.

PHP requires strict_types to be the very first statement in the PHP file. The error:


In b4b770a497bc79e9d39868f8567850fb50c4f817.php line 2:
                                                                           
  strict_types declaration must be the very first statement in the script  

Steps To Reproduce:

Template:

<?php declare(strict_types = 1);
// anything you want

Generated template in 5.7:

<?php declare(strict_types = 1);
// anything you want

Generated template in 5.8:

<?php /* /vagrant/project/resources/views/emails/template.blade.php */ ?>
<?php declare(strict_types = 1);
// anything you want

Were I work there’s a strict policy about this requirement with no exceptions, same for type declarations. I’ve been using them since Laravel 5.1 and never had issues so far.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 18 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Well, for me it’s not exactly clear how this behavior could be caused by the change mentioned above since I would expect in failed test something like:

--- Expected
+++ Actual
@@ @@
-'bar'
+'bar\n
+<?php /*  */ ?>
+'

if no path specified in the view. But probably I missed something.

But I totally agree that we should not add the path if it’s not specified in the view. Please find the pull-request with the fix: #27976 I hope it helps to resolve the issue above.

First of all, sorry for the long silence.

@driesvints if I understand you right, you suggest to insert a path to the first <?php …> tag in the compiled view if it exists. The solution might work indeed, but it’s a little bit harder than just adding the path at the end of the compiled view since it requires Blade compiler as well as PhpStorm to go through the file and find this first PHP tag.

Besides, there is a couple of other minor things. It’s easier for an eye to find a debug info in a permanent place like at the end of the file. Furthermore, in the future, we will also need to provide line mappings in the debug info. If we insert those lines somewhere in the middle of the compiled view, it will look strange.

So I would prefer to just move the comment with a view path to the end of the file (please take a look at pull-request https://github.com/laravel/framework/pull/27914) However, all mentioned above concerns are not very essential and may be subjective and if you insist I will implement the fix in the suggested way.