CodeIgniter Forums
Using view layouts echoes void return type. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Using view layouts echoes void return type. (/showthread.php?tid=79630)



Using view layouts echoes void return type. - aschmitz - 07-11-2021

Hi,

while playing around with PHPStan I noticed that the current CI4 analysis level will complain about some basic handling of view layouts.
In the Docs it is stated that views can extend view layouts by writing:

PHP Code:
<?= $this->extend('default'?>

While this in principle is working fine and without any warning, PHPStan complains that we try to output the return value of a method that has a void return type.

Now I know we specifically exclude the 'views' folder in static analysis, however, I feel that PHPStan is correct here.

The correct implementation would be:

PHP Code:
<?php $this->extend('default'?>

I personally feel, that sparing the two characters is not really worth the inconsistency here.

How do others feel about that?


RE: Using view layouts echoes void return type. - InsiteFX - 07-12-2021

It is 6 not 2 extra characters, the = sign replaces echo.


RE: Using view layouts echoes void return type. - aschmitz - 07-12-2021

That's correct in principle, but we are not echoing anything here :-)
The second example works just fine without any echo.


RE: Using view layouts echoes void return type. - includebeer - 07-12-2021

This looks like an error in the user guide. You are right, extend() return void, there's nothing to echo.

PHP Code:
    /**
    * Specifies that the current view should extend an existing layout.
    * @param string $layout
    * @return void
    */
    public function extend(string $layout)
    {
        $this->layout $layout;
    


Source: https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/View/View.php#L398

...and it's the same with section() and endSection().

This:
PHP Code:
<?= $this->extend('default'?>

<?= $this->section('content'?>
    <h1>Hello World!</h1>
<?= $this->endSection() ?>

...should be this:
PHP Code:
<?php $this->extend('default'?>

<?php $this->section('content'?>
    <h1>Hello World!</h1>
<?php $this->endSection() ?>



RE: Using view layouts echoes void return type. - paulbalandan - 07-12-2021

This should be changed, regardless of how many characters are involved. A detected error should not be neglected IMO.

We should be really trying to uncover the suppressed errors of phpstan. ?