Passed
Push — master ( cc7747...d2190f )
by Mihail
10:36
created

FormContentClear   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A labels() 0 6 1
A make() 0 13 3
1
<?php
2
3
namespace Apps\Model\Admin\Content;
4
5
use Apps\ActiveRecord\Content;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Helper\FileSystem\Directory;
8
use Illuminate\Database\Eloquent\Collection;
9
10
class FormContentClear extends Model
11
{
12
    public $count = 0;
13
14
    /** @var Content|Collection */
15
    private $_records;
16
17
    /**
18
     * FormContentClear constructor. Pass content records collection object inside (can be empty collection)
19
     * @param Content|Collection $records
20
     */
21
    public function __construct($records)
22
    {
23
        $this->_records = $records;
24
        $this->count = $this->_records->count();
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Form display labels
30
     * @return array
31
     */
32
    public function labels()
33
    {
34
        return [
35
            'count' => __('Trashed content')
36
        ];
37
    }
38
39
    /**
40
     * Finally delete content item
41
     */
42
    public function make()
43
    {
44
        // remove gallery files if exists
45
        foreach ($this->_records->get() as $record) {
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $key.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Collection, but not in Apps\ActiveRecord\Content.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
            $galleryPath = '/upload/gallery/' . (int)$record->id;
47
            if (Directory::exist($galleryPath)) {
48
                Directory::remove($galleryPath);
49
            }
50
        }
51
52
        // finally remove from db
53
        $this->_records->forceDelete();
0 ignored issues
show
Bug introduced by
The method forceDelete does only exist in Apps\ActiveRecord\Content, but not in Illuminate\Database\Eloquent\Collection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
    }
55
}