Welcome Guest, Not a member yet? Register   Sign In
Using view layouts echoes void return type.
#1

(This post was last modified: 07-11-2021, 02:21 PM by aschmitz.)

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?
Reply
#2

It is 6 not 2 extra characters, the = sign replaces echo.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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

(This post was last modified: 07-12-2021, 07:32 AM by includebeer.)

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/CodeIgni...w.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() ?>
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

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. ?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB