Completed
Push — master ( 1cc92c...b9814d )
by Maxime
502:17 queued 499:55
created

OrderStateTrait::postOrder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Distilleries\Expendable\States;
4
5
use Illuminate\Http\Request;
6
7
trait OrderStateTrait
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 4
    public function getOrder()
13
    {
14 4
        $rows = $this->model->orderBy($this->model->orderFieldName(), 'asc')->get();
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
16 4
        $this->layoutManager->add([
0 ignored issues
show
Bug introduced by
The property layoutManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17 4
            'content' => view('expendable::admin.form.state.order', [
18 4
                'rows' => $rows,
19
            ]),
20
        ]);
21
22 4
        return $this->layoutManager->render();
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function postOrder(Request $request)
29
    {
30 4
        $ids = $request->input('ids');
31
32 4
        $order = 1;
33 4
        foreach ($ids as $id) {
0 ignored issues
show
Bug introduced by
The expression $ids of type string|array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
34 4
            $instance = $this->model->find($id);
35 4
            if (! empty($instance)) {
36 4
                $instance->{$this->model->orderFieldName()} = $order;
37 4
                $instance->save();
38 4
                $order++;
39
            }
40
        }
41
42 4
        return response()->json(['error' => false]);
43
    }
44
}
45