Completed
Push — master ( 64123e...952eff )
by Rougin
01:47
created

ResultTrait::resetQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * Result Trait
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
trait ResultTrait
12
{
13
    use ObjectTrait;
14
15
    /**
16
     * @var string
17
     */
18
    protected $table = '';
19
20
    /**
21
     * Lists all data in dropdown format.
22
     * NOTE: To be removed in v1.0.0. Use $this->dropdown instead.
23
     *
24
     * @param  string $description
25
     * @return array
26
     */
27 3
    public function asDropdown($description = 'description')
28
    {
29 3
        return $this->dropdown($description);
30
    }
31
32
    /**
33
     * Returns result data in array dropdown format.
34
     *
35
     * @param  string $column
36
     * @return array
37
     */
38 3
    public function dropdown($column = 'description')
39
    {
40 3
        $data = array();
41
42 3
        $id = $this->describe->primary($this->table);
0 ignored issues
show
Bug introduced by
The property describe 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...
43
44 3
        $result = $this->query->result();
0 ignored issues
show
Bug introduced by
The property query 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...
45
46 3
        foreach ((array) $result as $row) {
47 3
            $text = ucwords($row->$column);
48
49 3
            $identifier = $row->$id;
50
51 3
            $data[$identifier] = $text;
52 2
        }
53
54 3
        $this->reset();
55
56 3
        return $data;
57
    }
58
59
    /**
60
     * Returns the result.
61
     *
62
     * @return array
63
     */
64 24
    public function result()
65
    {
66 24
        $models = array();
67
68 24
        $exists = method_exists($this->query, 'result');
69
70 24
        $query = $this->query;
71
72 24
        $data = $exists ? $query->result() : $query;
73
74 24
        $this->get($this->table);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
76 24
        foreach ((array) $data as $row) {
77 24
            $model = $this->make($this->table, $row);
78
79 24
            array_push($models, $model);
80 16
        }
81
82 24
        $this->reset();
83
84 24
        return $models;
85
    }
86
87
    /**
88
     * Resets the entire query and table name.
89
     *
90
     * @return void
91
     */
92 27
    protected function reset()
93
    {
94 27
        $this->table = null;
95
96 27
        $this->query = null;
97 27
    }
98
}
99