Passed
Push — master ( c8976e...eafcc8 )
by guilherme
02:02
created

Read::page()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 8
nop 2
1
<?php
2
3
4
namespace database;
5
6
use database\DB;
7
8
/**
9
 * Class Read
10
 * @package src
11
 */
12
class Read
13
{
14
15
    /**
16
     * @var
17
     */
18
    private $db;
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
    /**
20
     * @var
21
     */
22
    private $table;
23
    /**
24
     * @var
25
     */
26
    private $read;
27
    /**
28
     * @var
29
     */
30
    private $statement;
31
    /**
32
     * @var
33
     */
34
    private $terms;
35
36
    /**
37
     * @var \PDOStatement
38
     */
39
    private $select;
40
41
    private $query;
42
43
    /**
44
     * @var
45
     */
46
    private $limit;
47
48
    /**
49
     * @var
50
     */
51
    private $offset;
52
53
    private $page;
54
    private $link;
0 ignored issues
show
introduced by
The private property $link is not used, and could be removed.
Loading history...
55
    private $rows;
0 ignored issues
show
introduced by
The private property $rows is not used, and could be removed.
Loading history...
56
    private $maxLinks;
0 ignored issues
show
introduced by
The private property $maxLinks is not used, and could be removed.
Loading history...
57
    private $first;
0 ignored issues
show
introduced by
The private property $first is not used, and could be removed.
Loading history...
58
    private $last;
0 ignored issues
show
introduced by
The private property $last is not used, and could be removed.
Loading history...
59
60
    /**
61
     * @var
62
     */
63
    private $result;
64
65
66
    /**
67
     * Read constructor.
68
     */
69
    public function __construct()
70
    {
71
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77
    public function getResult()
78
    {
79
        return $this->result;
80
    }
81
82
    /**
83
     * @param $table
84
     * @param null $parse
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parse is correct as it would always require null to be passed?
Loading history...
85
     * @return array
86
     */
87
88
    public function query(string $query, string $parse = null)
89
    {
90
        if ($parse) {
91
            parse_str($parse, $this->statement);
92
93
        }
94
95
        $this->query = $query;
96
        $this->read = DB::connect()->prepare($this->query);
97
        $this->read->execute($this->statement);
98
        $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ);
99
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type database\Read which is incompatible with the documented return type array.
Loading history...
100
101
102
    }
103
104
    public function all($table)
105
    {
106
        $this->table = (string)$table;
107
108
109
        $this->read = DB::connect()->prepare("SELECT * FROM {$this->table}");
110
        $this->read->execute();
111
        return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ);
112
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
113
114
    }
115
116
    /**
117
     * @param string $table
118
     * @param string $terms
119
     * @param string $parse
120
     * @return mixed
121
     */
122
    public function find(string $table, string $terms, string $parse)
123
    {
124
        $this->table = $table;
125
        $this->terms = $terms;
126
127
        if ($parse) {
128
            parse_str($parse, $this->statement);
129
        }
130
131
        try {
132
            $this->read = DB::connect()->prepare("SELECT * FROM {$this->table} {$this->terms}");
133
            $this->read->execute($this->statement);
134
            return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ);
135
        } catch (\PDOException $e) {
136
            echo $e->getMessage() . " in " . $e->getFile();
137
        }
138
139
        return $this;
140
    }
141
142
    public function getRowCount()
143
    {
144
        return $this->select->rowCount();
145
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
146
    }
147
148
    public function page(int $pag = null, int $limit)
149
    {
150
        $this->page = ($pag ? $pag : 1);
151
        $this->limit = $limit;
152
        $this->offset = ($this->page * $this->limit) - $this->limit;
153
154
        try {
155
156
            $this->read = DB::connect()->prepare("SELECT * FROM {$this->table} LIMIT {$this->limit} OFFSET {$this->offset}");
157
            $this->read->execute();
158
            $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ);
159
        }catch (\PDOException $e){
160
            echo $e->getMessage() . " in " . $e->getFile();
161
162
        }
163
164
165
    }
166
167
168
}