spatie / laravel-model-cleanup

Clean up unneeded records
https://freek.dev/410-a-laravel-package-to-clean-up-models
MIT License
399 stars 42 forks source link

Can not pass with PHP Static Analysis Tool #26

Closed bolechen closed 5 years ago

bolechen commented 5 years ago

Laravel v5.8.35

  82     Call to an undefined method Illuminate\Database\Eloquent\Builder::onlyTrashed().

the code is

    public static function cleanUp(Builder $query): Builder
    {
        return $query->onlyTrashed()->where('deleted_at', '<', Carbon::now()->subMonth());
    }

ref link https://github.com/nunomaduro/larastan/issues/40

bolechen commented 5 years ago

Step to reproduce

new laravel setup

$ laravel new model-cleanup && cd model-cleanup
$ composer require spatie/laravel-model-cleanup
$ composer require --dev nunomaduro/larastan

change app\User.php to

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Spatie\ModelCleanup\GetsCleanedUp;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable implements GetsForcedCleanedUp
{
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

     public static function cleanUp(Builder $query) : Builder
     {
        // Delete all records older than a year
        return $query->onlyTrashed()->where('deleted_at', '<', Carbon::now()->subDay());
     }
}

and run

$ php artisan code:analyse
freekmurze commented 5 years ago

Feel free to PR improvements.