Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Model/Admin/Content/FormCategoryDelete.php (1 issue)

Checks whether a method/function call has too many arguments.

Unused Code Minor
1
<?php
2
3
namespace Apps\Model\Admin\Content;
4
5
use Apps\ActiveRecord\Content;
6
use Apps\ActiveRecord\ContentCategory;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Exception\SyntaxException;
9
use Ffcms\Core\Helper\Type\Arr;
10
11
/**
12
 * Class FormCategoryDelete. Delete category business logic model
13
 * @package Apps\Model\Admin\Content
14
 */
15
class FormCategoryDelete extends Model
16
{
17
    public $title;
18
    public $path;
19
20
    public $moveTo;
21
22
    private $_record;
23
24
    /**
25
     * ForumCategoryDelete constructor. Pass active record object inside
26
     * @param ContentCategory $record
27
     */
28
    public function __construct(ContentCategory $record)
29
    {
30
        $this->_record = $record;
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Set model public attributes from record object
36
     */
37
    public function before()
38
    {
39
        $this->title = $this->_record->getLocaled('title');
40
        $this->path = $this->_record->path;
41
    }
42
43
    /**
44
     * Form display labels
45
     * @return array
46
     */
47
    public function labels(): array
48
    {
49
        return [
50
            'title' => __('Title'),
51
            'path' => __('Path slug'),
52
            'moveTo' => __('Move to')
53
        ];
54
    }
55
56
    /**
57
     * Validation rules
58
     * @return array
59
     */
60
    public function rules(): array
61
    {
62
        return [
63
            ['moveTo', 'required'],
64
            ['moveTo', 'int'],
65
            ['moveTo', 'in', $this->categoryIds()]
66
        ];
67
    }
68
69
    /**
70
     * Make delete category
71
     * @throws SyntaxException
72
     * @throws \Exception
73
     */
74
    public function make()
75
    {
76
        // get new category object
77
        $newRecord = ContentCategory::find($this->moveTo);
78
        if ($newRecord === null || $newRecord === false) {
79
            throw new SyntaxException();
80
        }
81
82
        // get all depended category ids
83
        $cats = ContentCategory::where('path', 'like', $this->_record->path . '%')->get(['id'])->toArray();
0 ignored issues
show
The call to Ffcms\Core\Arch\ActiveModel::get() has too many arguments starting with array('id'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        $cats = ContentCategory::where('path', 'like', $this->_record->path . '%')->/** @scrutinizer ignore-call */ get(['id'])->toArray();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
84
        $allCategoryIds = Arr::pluck('id', $cats);
85
86
        // update category_id in content
87
        $find = Content::whereIn('category_id', $allCategoryIds);
88
        if ($find->count() > 0) {
89
            $find->update(['category_id' => $newRecord->id]);
90
        }
91
92
        // remove category
93
        $this->_record->delete();
94
    }
95
96
    /**
97
     * List categories
98
     * @return array
99
     */
100
    public function categoryList()
101
    {
102
        $result = ContentCategory::getSortedCategories();
103
        unset($result[$this->_record->id]);
104
        return $result;
105
    }
106
107
108
    /**
109
     * Get allowed category ids as array
110
     * @return array
111
     */
112
    public function categoryIds()
113
    {
114
        $data = ContentCategory::getSortedCategories();
115
        $response = array_keys($data);
116
        // remove current category id from 'moveTo" list
117
        foreach ($response as $k => $v) {
118
            if ($this->_record->id === $v) {
119
                unset($response[$k]);
120
            }
121
        }
122
        return $response;
123
    }
124
}
125