laravelphpcodesniffer

Why squizlabs/php_codesniffer marked as error code with(new Vote)?


I laravel 9 project with squizlabs/php_codesniffer my phpstorm 2021 shows error :

Expected parameter of type '\TValue', 'Vote' provided 

in model when I use table name in scope condition :

class QuizQualityResult extends Model
{
    protected $table       = 'quiz_quality_results';

    public function scopeGetByVoteCategories($query, $voteCategoryId= null)
    {
        // “new Vote” is marked as error
        $voteTable = with(new Vote)->getTable(); 
        if (!empty($voteCategoryId)) {
            if ( is_array($voteCategoryId) ) {
                $query->whereIn( $voteTable . '.vote_category_id', $voteCategoryId);
            } else {
                $query->where( $voteTable . ' . vote_category_id', $voteCategoryId);
            }
        }
        return $query;
    }

If there is a way to fix this error ? Or maybe to use better syntax here ?

Thanks!


Solution

  • There is no need for helper with() here

    $voteTable = (new Vote())->getTable()
    

    Ps: there is a feeling that your method does not work the way you intended. Perhaps you meant to do the following (I could be wrong):

    public function scopeGetByVoteCategories($query, $voteCategoryId = null)
    {
        if (empty($voteCategoryId)) {
            return $query;
        }
    
        return $query->whereHas('vote', static function ($query) use ($voteCategoryId) {
            if (is_array($voteCategoryId)) {
                return $query->whereIn('vote_category_id', $voteCategoryId);
            }
    
            return $query->where('vote_category_id', $voteCategoryId);
        });
    }
    
    public function vote()
    {
        // your relationship
    }