Larastan: Ternary operator condition is always true

Created on 15 Jan 2021  路  11Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.6.12
  • --level used: 5

Description

Bit like https://github.com/nunomaduro/larastan/issues/150 but the short syntax, so it needs a truthy evaluation to work as expected

Laravel code where the issue was found

Ternary operator condition is always true. in line ->whereDate('starts_at', '<=', $assignment->ends_at ?: Carbon::now())

    public static function checkForCollisions(Assignment $assignment): void
    {
        $query = Assignment::where('id', '!=', $assignment->id)
                         ->whereDate('starts_at', '<=', $assignment->ends_at ?: Carbon::now()) 
                         ->where('user_id', $assignment->user_id);
       // ...
   }

Although I guess from PHP 7 null coalescing could be used instead...

->whereDate('starts_at', '<=', $assignment->ends_at ?? Carbon::now())

It's just worth noting that $a ?: $b is a perfectly valid use case that requires a truthy $a to behave correctly (or as expected)

$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

Most helpful comment

Sorry, I should have been clearer in the explanation.

There were 2 competing phpdoc definitions for the property, one was of Carbon the other of Carbon|null, removing the the incorrect definition resolved the issue.

All 11 comments

Hi,

I guess this happens because $assignment->ends_at is not nullable. What is the type of $assignment->ends_at?

That's not quite correct, it is nullable

The migration definition:
$table->date('ends_at')->nullable();

and it's set to auto cast to Carbon

    protected $dates = [
        'ends_at'
    ];

and is defined in it's @property as nullable too

     * @property Carbon|null $ends_at

You can check what PHPStan thinks about its type with \PHPStan\dumpType($assignment->ends_at) Can you add this to checkForCollisions method, and see what is the type? Also please try with ends_at in the $dates cast array, and without in the array. There might be a bug in there.

Dumped type: Carbon\Carbon

That got me going hmmmmm! So did a bit of hunting. I've found a competing phpdoc in _ide_helper_models.php. So stripped all the php docs, and regenerated Laravel Idea's helper file.

Error no longer appears.

Nice!. sorry for the bogus report.

That's good! Are you including the _ide_helper_models.php file as a stub to PHPStan config?

I'm not! I wasn't aware that was a thing

parameters:
    stubFiles:
        - _ide_helper_models.php

I do now, Thank you !

That unfortunately fails with the Laravel Idea generated _ide_helper_models.php file (not tested the barryv ide-helper version of this file)

In ConstExprParser.php line 66:

  *  

Looking into it and will report back any fixes etc.

Sorry, I don't understand how changing _ide_helper_models.php helped you to fix the issue.

As a side note, although adding _ide_helper_models.php as a stub file will work on it's own, it might not work as expected together with Larastan. So use at your own risk.

Sorry, I should have been clearer in the explanation.

There were 2 competing phpdoc definitions for the property, one was of Carbon the other of Carbon|null, removing the the incorrect definition resolved the issue.

Issue with the Laravel Idea _ide_helper_models as noted above is caused by Laravel Idea wrapping phpdocs to new lines

* @method static _ContractAdjustmentQueryBuilder join(string $table, string $first, null|string $operator = null, null|string $second = null, string $type = 'inner', bool 
* $where = false)

vs

* @method static _ContractAdjustmentQueryBuilder join(string $table, string $first, null|string $operator = null, null|string $second = null, string $type = 'inner', bool $where = false)

I'll log a issue with Laravel Idea

Laravel Idea issue raised to address incompatibility with LaraStan https://github.com/laravel-idea/plugin/issues/181

Was this page helpful?
0 / 5 - 0 ratings