Completed
Push — master ( 17ec4b...672085 )
by Adeniyi
9s
created

Relationships::whereClause()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 18
rs 8.8571
cc 6
eloc 13
nc 4
nop 3
1
<?php
2
/**
3
 * PotatoORM manages the persistence of database CRUD operations.
4
 *
5
 * @package Ibonly\PotatoORM\Model
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use Ibonly\PotatoORM\DatabaseQuery;
13
use Ibonly\PotatoORM\RelationshipsInterface;
14
15
class Relationships extends DatabaseQuery implements RelationshipsInterface
16
{
17
    public function joinClause($con = null)
18
    {
19
        $connection = self::checkConnection($con);
20
21
        $data = self::query('SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME="'.self::getTableName($connection).'"')->all();
22
23
        array_shift($data);
24
        $output = "";
25
        $i = 0;
26
27
        if (! isset($data[0]) || $data[0]->REFERENCED_TABLE_NAME !== null) {
28
            $arraySize  = count($data);
0 ignored issues
show
Unused Code introduced by
$arraySize 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...
29
30
            foreach($data as $key => $value) {
31
                $output .= ' JOIN '.$value->REFERENCED_TABLE_NAME;
32
            }
33
            foreach($data as $key => $value) {
34
                $i++;
35
                $whereAnd = $i > 1 ? 'AND' : 'WHERE';
36
                $output .= ' '.$whereAnd.' '.self::getTableName($connection).'.'.$value->COLUMN_NAME.'='.$value->REFERENCED_TABLE_NAME.'.'.$value->REFERENCED_COLUMN_NAME.' ';
37
            }
38
         } else {
39
            $output = false;
40
         }
41
        return $output;
42
    }
43
44
    public function whereClause($data = null, $condition = null, $con = null)
45
    {
46
        $joinClause = self::joinClause();
47
        $connection = self::checkConnection($con);
48
        $tableName  = self::getTableName($connection);
49
        $columnName = self::whereAndClause($tableName, $data, $condition);
50
51
        $query = 'SELECT * FROM '.$tableName;
52
53
        if ($joinClause == false && $data === null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $joinClause of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
54
            $query .= $columnName;
55
        } else if ($joinClause == false && $data !== null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $joinClause of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
56
            $query .= ' WHERE '.$columnName;
57
        } else {
58
            $query .= ($data === null) ? $joinClause : $joinClause .' AND '.$columnName;
59
        }
60
        return $query;
61
    }
62
}