Completed
Pull Request — master (#1)
by Rougin
02:14
created

ObjectTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.51%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 142
ccs 38
cts 51
cp 0.7451
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
C createObject() 0 31 7
find() 0 1 ?
A getModelProperties() 0 18 4
A setForeignField() 0 16 2
B getModel() 0 18 5
getTableName() 0 1 ?
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
use Rougin\Describe\Column;
6
7
use Rougin\Wildfire\CodeigniterModel;
8
9
/**
10
 * Object Trait
11
 *
12
 * @package Wildfire
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 *
15
 * @property \Rougin\Describe\Describe $describe
16
 * @property string                    $table
17
 */
18
trait ObjectTrait
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $tables = [];
24
25
    /**
26
     * Creates an object from the specified table and row.
27
     *
28
     * @param  string  $table
29
     * @param  object  $row
30
     * @param  boolean $isForeignKey
31
     * @return array
32
     */
33 27
    protected function createObject($table, $row, $isForeignKey = false)
34
    {
35 27
        list($model, $newTable) = $this->getModel($table, $isForeignKey);
36
37 27
        if (! array_key_exists($newTable, $this->tables)) {
38 21
            $tableInfo = $this->describe->getTable($newTable);
39
40 21
            $this->tables[$newTable] = $tableInfo;
41 21
        } else {
42 12
            $tableInfo = $this->tables[$newTable];
43
        }
44
45 27
        $parameters = $this->getModelProperties($model);
0 ignored issues
show
Unused Code introduced by
$parameters 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...
46
47 12
        foreach ($tableInfo as $column) {
48 12
            $key = $column->getField();
49
50 12
            $inHiddenColumns = ! empty($properties['hidden']) && in_array($key, $properties['hidden']);
0 ignored issues
show
Bug introduced by
The variable $properties seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
51 12
            $inColumns       = ! empty($properties['columns']) && ! in_array($key, $properties['columns']);
52
53 12
            if ($inColumns || $inHiddenColumns) {
54
                continue;
55
            }
56
57 12
            $model->$key = $row->$key;
58
59 12
            $this->setForeignField($model, $column);
60 12
        }
61
62 12
        return $model;
63
    }
64
65
    /**
66
     * Finds the row from the specified ID or with the list of delimiters from
67
     * the specified table.
68
     *
69
     * @param  string         $table
70
     * @param  array|integer  $delimiters
71
     * @param  boolean        $isForeignKey
72
     * @return object|boolean
73
     */
74
    abstract protected function find($table, $delimiters = [], $isForeignKey = false);
75
76
    /**
77
     * Returns the values from the model's properties.
78
     *
79
     * @param  \Rougin\Wildfire\CodeigniterModel $model
80
     * @return array
81
     */
82 12
    protected function getModelProperties(CodeigniterModel $model)
83
    {
84 12
        $properties = [ 'column' => [], 'hidden' => [] ];
85
86 12
        if (method_exists($model, 'getColumns')) {
87 12
            $properties['columns'] = $model->getColumns();
88 12
        } elseif (property_exists($model, 'columns')) {
89
            // NOTE: To be removed in v1.0.0
90
            $properties['columns'] = $model->columns;
0 ignored issues
show
Documentation introduced by
The property $columns is declared protected in Rougin\Wildfire\Traits\ModelTrait. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
        }
92
93
        // NOTE: To be removed in v1.0.0 (if condition only)
94 12
        if (method_exists($model, 'getHiddenColumns')) {
95 12
            $properties['hidden'] = $model->getHiddenColumns();
96 12
        }
97
98 12
        return $properties;
99
    }
100
101
    /**
102
     * Sets the foreign field of the column, if any.
103
     *
104
     * @param  \CI_Model               $model
105
     * @param  \Rougin\Describe\Column $column
106
     * @return void
107
     */
108 12
    protected function setForeignField(\CI_Model $model, Column $column)
109
    {
110 12
        if (! $column->isForeignKey()) {
111 12
            return;
112
        }
113
114
        $columnName    = $column->getField();
115
        $foreignColumn = $column->getReferencedField();
116
        $foreignTable  = $column->getReferencedTable();
117
118
        $delimiters  = [ $foreignColumn => $model->$columnName ];
119
        $foreignData = $this->find($foreignTable, $delimiters, true);
120
        $newColumn   = $this->getTableName($foreignTable, true);
121
122
        $model->$newColumn = $foreignData;
123
    }
124
125
    /**
126
     * Gets the model class of the said table.
127
     *
128
     * @param  string|null $table
129
     * @param  boolean     $isForeignKey
130
     * @return array
131
     */
132 36
    protected function getModel($table = null, $isForeignKey = false)
133
    {
134 36
        if (empty($table) && empty($this->table)) {
135 6
            return [ null, '' ];
136
        }
137
138 36
        $newTable = $this->getTableName($table, $isForeignKey);
139 36
        $newModel = new $newTable;
140
141 36
        if (method_exists($newModel, 'getTableName')) {
142 15
            $newTable = $newModel->getTableName();
143 36
        } elseif (property_exists($newModel, 'table')) {
144
            // NOTE: To be removed in v1.0.0
145
            $newTable = $newModel->table;
146
        }
147
148 36
        return [ $newModel, strtolower($newTable) ];
149
    }
150
151
    /**
152
     * Parses the table name from Describe class.
153
     *
154
     * @param  string  $table
155
     * @param  boolean $isForeignKey
156
     * @return string
157
     */
158
    abstract protected function getTableName($table, $isForeignKey = false);
159
}
160