Completed
Push — master ( fa85af...17ec4b )
by Adeniyi
8s
created

Relationships::whereClause()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 11
nc 3
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 ($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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $joinClause of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54
    		$query .= ' WHERE '.$columnName;
55
    	} else {
56
    		$query .= ($data === null) ? $joinClause : $joinClause .' AND '.$columnName;
57
    	}
58
    	return $query;
59
    }
60
}