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

ObjectTrait::setModelFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 4
crap 3
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\Describe\Column;
6
use Rougin\Wildfire\CodeigniterModel;
7
use Rougin\Wildfire\Helpers\TableHelper;
8
9
/**
10
 * Object Trait
11
 *
12
 * @package Wildfire
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
trait ObjectTrait
16
{
17
    use RelationshipTrait;
18
19
    /**
20
     * Creates an object from the specified table and row.
21
     *
22
     * @param  \Rougin\Wildfire\CodeigniterModel\string $table
23
     * @param  object                                   $row
24
     * @return array
25
     */
26 36
    protected function make($table, $row)
27
    {
28 36
        list($table, $model) = $this->model($table);
0 ignored issues
show
Bug introduced by
It seems like model() 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...
29
30 36
        $properties = array('relationships' => array());
31
32 36
        $properties['hidden'] = array();
33
34 36
        $properties['columns'] = array();
35
36 36
        if ($model instanceof CodeigniterModel === true) {
37 36
            $properties = (array) $model->properties();
38
39 36
            $properties = $model->relationships($properties);
40 24
        }
41
42 36
        $columns = (array) $this->describe->columns($table);
43
44 36
        return $this->fields($columns, $properties, $model, $row);
45
    }
46
47
    /**
48
     * Sets the foreign field of the column, if any.
49
     *
50
     * @param  \CI_Model               $model
51
     * @param  \Rougin\Describe\Column $column
52
     * @return \CI_Model
53
     */
54 6
    protected function foreign(\CI_Model $model, Column $column)
55
    {
56 6
        $field = $column->getReferencedField();
57
58 6
        $name = (string) $column->getField();
59
60 6
        $table = $column->getReferencedTable();
61
62 6
        $delimiters = array($field => $model->$name);
63
64 6
        $foreign = $this->find($table, $delimiters);
0 ignored issues
show
Bug introduced by
It seems like find() 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...
65
66 6
        $name = (string) TableHelper::name($foreign);
67
68 6
        is_object($foreign) && $model->$name = $foreign;
69
70 6
        return $model;
71
    }
72
73
    /**
74
     * Sets the model values based on the result row.
75
     *
76
     * @param  array     $columns
77
     * @param  array     $properties
78
     * @param  \CI_Model &$model
79
     * @param  object    $row
80
     * @return \CI_Model
81
     */
82 36
    protected function fields($columns, $properties, $model, $row)
83
    {
84 36
        foreach ((array) $columns as $column) {
85 36
            $field = (string) $column->getField();
86
87 36
            $allowed = in_array($field, $properties['columns']);
88
89 36
            $hidden = in_array($field, $properties['hidden']);
90
91 36
            $foreign = in_array($field, $properties['hidden']);
0 ignored issues
show
Unused Code introduced by
$foreign is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
93 36
            if ($allowed === true || $hidden === false) {
94 36
                $table = (string) $column->getReferencedTable();
95
96 36
                $model->$field = $row->$field;
97
98 36
                $exists = in_array($table, $properties['relationships']);
99
100 36
                $exists && $this->foreign($model, $column, $properties);
0 ignored issues
show
Unused Code introduced by
The call to ObjectTrait::foreign() has too many arguments starting with $properties.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101 24
            }
102 24
        }
103
104 36
        return $model;
105
    }
106
}
107