Completed
Push — master ( 5c183f...7b5197 )
by Rougin
02:37
created

ObjectTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.97%

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 9
c 8
b 2
f 1
lcom 1
cbo 0
dl 0
loc 92
ccs 32
cts 33
cp 0.9697
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
find() 0 1 ?
B createObject() 0 37 6
A getModel() 0 15 3
getTableName() 0 1 ?
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * Object Trait
7
 * 
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \Rougin\Describe\Describe $describe
12
 */
13
trait ObjectTrait
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $tables = [];
19
20
    /**
21
     * Creates an object from the specified table and row.
22
     *
23
     * @param  string $table
24
     * @param  object $row
25
     * @return array
26
     */
27 18
    protected function createObject($table, $row)
28
    {
29 18
        list($model, $newTable) = $this->getModel($table);
30
31 18
        if ( ! array_key_exists($newTable, $this->tables)) {
32 18
            $tableInfo = $this->describe->getTable($newTable);
33
34 18
            $this->tables[$newTable] = $tableInfo;
35 18
        }
36
37 18
        if (isset($this->tables[$newTable])) {
38 18
            $tableInfo = $this->tables[$newTable];
39 18
        }
40
41 18
        foreach ($tableInfo as $column) {
0 ignored issues
show
Bug introduced by
The variable $tableInfo does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
42 18
            if ( ! property_exists($row, $column->getField())) {
43
                continue;
44
            }
45
46 18
            $key = $column->getField();
47
48 18
            $model->$key = $row->$key;
49
50 18
            if ($column->isForeignKey()) {
51 15
                $foreignColumn = $column->getReferencedField();
52 15
                $foreignTable = $column->getReferencedTable();
53
54 15
                $delimiters = [ $foreignColumn => $model->$key ];
55 15
                $foreignData = $this->find($foreignTable, $delimiters);
56 15
                $newColumn = $this->getTableName($foreignTable);
57
58 15
                $model->$newColumn = $foreignData;
59 15
            }
60 18
        }
61
62 18
        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
     * @return object|boolean
72
     */
73
    abstract protected function find($table, $delimiters = []);
74
75
    /**
76
     * Gets the modal class of the said table.
77
     * 
78
     * @param  string|null $table
79
     * @return array
80
     */
81 24
    protected function getModel($table = null)
82
    {
83 24
        if ($table == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $table of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
84 6
            return [ null, '' ];
85
        }
86
87 24
        $newTable = $this->getTableName($table);
88 24
        $model = new $newTable;
89
90 24
        if (property_exists($model, 'table')) {
91 3
            $newTable = $model->table;
92 3
        }
93
94 24
        return [ $model, strtolower($newTable) ];
95
    }
96
97
    /**
98
     * Parses the table name from Describe class.
99
     * 
100
     * @param  string $table
101
     * @return string
102
     */
103
    abstract protected function getTableName($table);
104
}
105