Completed
Push — master ( 999b16...1321f0 )
by Patrick
03:02
created

SQLDataTable::search()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
nc 4
dl 0
loc 13
c 1
b 0
f 0
cc 4
eloc 7
nop 6
rs 9.2
1
<?php
2
namespace Data;
3
4
class SQLDataTable extends DataTable
5
{
6
    protected $dataset;
7
    protected $tablename;
8
9
    /**
10
     * @param SQLDataSet $dataset The dataset to create this datatable in
11
     * @param string $tablename The name of the table in the dataset
12
     */
13
    public function __construct($dataset, $tablename)
14
    {
15
        $this->dataset   = $dataset;
16
        $this->tablename = $tablename;
17
    }
18
19
    function get_primary_key()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        $res = $this->dataset->raw_query("SHOW INDEX FROM $this->tablename WHERE Key_name='PRIMARY'");
22
        if($res === false)
23
        {
24
            return false;
25
        }
26
        return $res[0]['Column_name'];
27
    }
28
29
    function count($filter = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        $where = false;
32
        if($filter !== false)
33
        {
34
            $where = $filter->to_sql_string();
0 ignored issues
show
Bug introduced by
The method to_sql_string cannot be called on $filter (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
35
        }
36
        $ret = $this->dataset->read($this->tablename, $where, 'COUNT(*)');
37
        if($ret === false || !isset($ret[0]) || !isset($ret[0]['COUNT(*)']))
38
        {
39
            return false;
40
        }
41
        else
42
        {
43
            return $ret[0]['COUNT(*)'];
44
        }
45
    }
46
  
47
    function search($filter = false, $select = false, $count = false, $skip = false, $sort = false, $params = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49
        $where = false;
50
        if($filter !== false)
51
        {
52
            $where = $filter->to_sql_string();
0 ignored issues
show
Bug introduced by
The method to_sql_string cannot be called on $filter (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
53
        }
54
        if($select !== false && is_array($select))
55
        {
56
            $select = implode(',', $select);
57
        }
58
        return $this->dataset->read($this->tablename, $where, $select, $count, $skip, $sort);
0 ignored issues
show
Bug introduced by
It seems like $select defined by parameter $select on line 47 can also be of type boolean; however, Data\SQLDataSet::read() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
    }
60
61
    function update($filter, $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        $where = $filter->to_sql_string();
64
        return $this->dataset->update($this->tablename, $where, $data);
65
    }
66
67
    function create($data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    {
69
        return $this->dataset->create($this->tablename, $data);
70
    }
71
72
    function delete($filter)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
    {
74
        $where = false;
75
        if($filter !== false)
76
        {
77
            $where = $filter->to_sql_string();
78
        }
79
        return $this->dataset->delete($this->tablename, $where);
80
    }
81
82
    function raw_query($sql)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        return $this->dataset->raw_query($sql);
85
    }
86
}
87
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
88