1 | <?php |
||||||
2 | |||||||
3 | namespace SleepingOwl\Admin\Display\Column; |
||||||
4 | |||||||
5 | use Illuminate\Database\Eloquent\Model; |
||||||
6 | use Illuminate\Routing\Router; |
||||||
7 | use SleepingOwl\Admin\Contracts\WithRoutesInterface; |
||||||
8 | use SleepingOwl\Admin\Display\TableColumn; |
||||||
9 | use SleepingOwl\Admin\Model\ModelConfiguration; |
||||||
10 | use SleepingOwl\Admin\Section; |
||||||
11 | use SleepingOwl\Admin\Traits\OrderableModel; |
||||||
12 | |||||||
13 | class Order extends TableColumn implements WithRoutesInterface |
||||||
14 | { |
||||||
15 | /** |
||||||
16 | * @var bool |
||||||
17 | */ |
||||||
18 | protected $orderable = false; |
||||||
19 | |||||||
20 | /** |
||||||
21 | * @var string |
||||||
22 | */ |
||||||
23 | protected $view = 'column.order'; |
||||||
24 | |||||||
25 | /** |
||||||
26 | * @var null|int |
||||||
27 | */ |
||||||
28 | 285 | protected $totalCountValue = null; |
|||||
29 | |||||||
30 | 285 | /** |
|||||
31 | 285 | * Register routes. |
|||||
32 | 285 | * |
|||||
33 | 285 | * @param Router $router |
|||||
34 | 285 | */ |
|||||
35 | 285 | public static function registerRoutes(Router $router) |
|||||
36 | 285 | { |
|||||
37 | $routeName = 'admin.display.column.move-up'; |
||||||
38 | 285 | if (! $router->has($routeName)) { |
|||||
39 | 285 | $router->group(['namespace' => 'SleepingOwl\Admin\Http\Controllers'], |
|||||
40 | 285 | function ($router) use ($routeName) { |
|||||
41 | 285 | $router->post('{adminModel}/{adminModelId}/up', [ |
|||||
42 | 285 | 'as' => $routeName, |
|||||
43 | 285 | 'uses' => 'DisplayColumnController@orderUp', |
|||||
44 | 285 | ]); |
|||||
45 | 285 | }); |
|||||
46 | } |
||||||
47 | |||||||
48 | $routeName = 'admin.display.column.move-down'; |
||||||
49 | if (! $router->has($routeName)) { |
||||||
50 | $router->group(['namespace' => 'SleepingOwl\Admin\Http\Controllers'], |
||||||
51 | function ($router) use ($routeName) { |
||||||
52 | $router->post('{adminModel}/{adminModelId}/down', [ |
||||||
53 | 'as' => $routeName, |
||||||
54 | 'uses' => 'DisplayColumnController@orderDown', |
||||||
55 | ]); |
||||||
56 | }); |
||||||
57 | } |
||||||
58 | } |
||||||
59 | |||||||
60 | /** |
||||||
61 | * Order constructor. |
||||||
62 | */ |
||||||
63 | public function __construct() |
||||||
64 | { |
||||||
65 | parent::__construct(); |
||||||
66 | $this->setHtmlAttribute('class', 'row-order'); |
||||||
67 | } |
||||||
68 | |||||||
69 | /** |
||||||
70 | * @return Model $model |
||||||
71 | * @throws \Exception |
||||||
72 | */ |
||||||
73 | public function getModel() |
||||||
74 | { |
||||||
75 | if (! in_array(OrderableModel::class, trait_uses_recursive($class = get_class($this->model)))) { |
||||||
76 | throw new \Exception("Model [$class] should uses trait [SleepingOwl\\Admin\\Traits\\OrderableModel]"); |
||||||
77 | } |
||||||
78 | |||||||
79 | return $this->model; |
||||||
80 | } |
||||||
81 | |||||||
82 | /** |
||||||
83 | * @return mixed |
||||||
84 | * @throws \Exception |
||||||
85 | */ |
||||||
86 | protected function getOrderValue() |
||||||
87 | { |
||||||
88 | return $this->getModel()->getOrderValue(); |
||||||
89 | } |
||||||
90 | |||||||
91 | /** |
||||||
92 | * @return int |
||||||
93 | * @throws \Exception |
||||||
94 | */ |
||||||
95 | protected function totalCount() |
||||||
96 | { |
||||||
97 | if ($this->totalCountValue !== null) { |
||||||
98 | return $this->totalCountValue; |
||||||
99 | } |
||||||
100 | |||||||
101 | //$request = \Request::capture(); |
||||||
0 ignored issues
–
show
|
|||||||
102 | $request = \Illuminate\Http\Request::capture(); |
||||||
103 | $modelConfiguration = $this->getModelConfiguration(); |
||||||
104 | $query = $modelConfiguration->getRepository()->getQuery(); |
||||||
105 | if ($modelConfiguration instanceof Section) { |
||||||
106 | $onDisplay = $modelConfiguration->onDisplay(); |
||||||
0 ignored issues
–
show
The method
onDisplay() does not exist on SleepingOwl\Admin\Section . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
107 | } elseif ($modelConfiguration instanceof ModelConfiguration) { |
||||||
108 | $onDisplay = $modelConfiguration->getDisplay(); |
||||||
109 | $onDisplay = call_user_func($onDisplay, ['payload' => $request->get('payload')]); |
||||||
110 | } else { |
||||||
111 | /* |
||||||
112 | * @see https://sleepingowladmin.ru/docs/model_configuration |
||||||
113 | * @see https://sleepingowladmin.ru/docs/model_configuration_section |
||||||
114 | */ |
||||||
115 | throw new \Exception('Unknown type of the Model Configuration. Use Section or AdminSection::registerModel()'); |
||||||
116 | } |
||||||
117 | $onDisplay->getExtensions()->modifyQuery($query); |
||||||
118 | $onDisplay->applySearch($query, $request); |
||||||
119 | $onDisplay->applyOffset($query, $request); |
||||||
120 | $this->totalCountValue = $query->count(); |
||||||
0 ignored issues
–
show
It seems like
$query->count() can also be of type Illuminate\Database\Eloquent\Builder . However, the property $totalCountValue is declared as type integer|null . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||||||
121 | |||||||
122 | return $this->totalCountValue; |
||||||
0 ignored issues
–
show
|
|||||||
123 | } |
||||||
124 | |||||||
125 | /** |
||||||
126 | * @return bool |
||||||
127 | * @throws \Exception |
||||||
128 | */ |
||||||
129 | protected function movableUp() |
||||||
130 | { |
||||||
131 | return $this->getOrderValue() > 0; |
||||||
132 | } |
||||||
133 | |||||||
134 | /** |
||||||
135 | * @return string |
||||||
136 | * @throws \Exception |
||||||
137 | */ |
||||||
138 | protected function moveUpUrl() |
||||||
139 | { |
||||||
140 | return route('admin.display.column.move-up', [ |
||||||
141 | $this->getModelConfiguration()->getAlias(), |
||||||
142 | $this->getModel()->getKey(), |
||||||
143 | ]); |
||||||
144 | } |
||||||
145 | |||||||
146 | /** |
||||||
147 | * @return bool |
||||||
148 | * @throws \Exception |
||||||
149 | */ |
||||||
150 | protected function movableDown() |
||||||
151 | { |
||||||
152 | return $this->getOrderValue() < $this->totalCount() - 1; |
||||||
153 | } |
||||||
154 | |||||||
155 | /** |
||||||
156 | * @return string |
||||||
157 | * @throws \Exception |
||||||
158 | */ |
||||||
159 | protected function moveDownUrl() |
||||||
160 | { |
||||||
161 | return route('admin.display.column.move-down', [ |
||||||
162 | $this->getModelConfiguration()->getAlias(), |
||||||
163 | $this->getModel()->getKey(), |
||||||
164 | ]); |
||||||
165 | } |
||||||
166 | |||||||
167 | /** |
||||||
168 | * @return array |
||||||
169 | * @throws \Exception |
||||||
170 | */ |
||||||
171 | public function toArray() |
||||||
172 | { |
||||||
173 | return parent::toArray() + [ |
||||||
174 | 'movableUp' => $this->movableUp(), |
||||||
175 | 'moveUpUrl' => $this->moveUpUrl(), |
||||||
176 | 'movableDown' => $this->movableDown(), |
||||||
177 | 'moveDownUrl' => $this->moveDownUrl(), |
||||||
178 | ]; |
||||||
179 | } |
||||||
180 | } |
||||||
181 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.