Passed
Push — master ( b5c61c...484b54 )
by Nícollas
02:18 queued 11s
created

SimplePHP::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SimplePHP\Model;
4
5
use SimplePHP\Root\Connection;
6
use SimplePHP\Root\Functions;
7
use PDO;
8
use PDOException;
9
use Exception;
10
use Error;
11
use stdClass;
12
use SimplePHP\Model\CRUD as Actions;
13
14
/**
15
 * Class SimplePHP
16
 * @package NicollasSilva\SimplePHP
17
 */
18
class SimplePHP extends Connection {
19
    use Actions, Functions;
20
21
    /** @var string */
22
    protected $sentence = '';
23
24
    /** @var string */
25
    protected $offset;
26
27
    /** @var string */
28
    protected $order;
29
30
    /** @var string */
31
    protected $params = '*';
32
33
    /** @var string */
34
    protected $where;
35
36
    /** @var string */
37
    protected $limit;
38
39
    /** @var string */
40
    protected $table;
41
42
    /** @var object|null */
43
    protected $data;
44
45
    /** @var bool */
46
    protected $type;
47
48
    /** @var string */
49
    protected $primary;
50
51
    /** @var array */
52
    protected $request = [];
53
54
    /** @var array */
55
    protected $excepts = [];
56
57
    /** @var bool */
58
    protected $count = false;
59
60
    /**
61
     * Get tablename of children model
62
     * @param string|null $tableName
63
     */
64
    function __construct(String $tableName, String $primaryKey)
65
    {
66
        parent::__construct(); $this->table = $tableName; $this->primary = $primaryKey;
67
    }
68
69
    /**
70
     * @param int|null $id
71
     * @return SimplePHP|null
72
     */
73
    public function find(int $id = null): ?SimplePHP
74
    {
75
        is_int($id) ? $this->where('id', $id) : null;
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $condition
81
     * @param string $value
82
     * @return SimplePHP|null
83
     */
84
    public function where(String $condition, String $value): ?SimplePHP
85
    {
86
        $this->where = "WHERE " . (mb_strlen($this->where > 6) ? "&& {$condition} = '{$value}'" : "{$condition} = '{$value}'");
87
        return $this;
88
    }
89
90
    /**
91
     * @param array $params
92
     * @return SimplePHP|null
93
     */
94
    public function only(Array $params): ?SimplePHP
95
    {
96
        $this->params = implode($params, ',');
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ','. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $this->params = /** @scrutinizer ignore-call */ implode($params, ',');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
        return $this;
98
    }
99
100
    /**
101
     * @param int $limit
102
     * @return SimplePHP|null
103
     */
104
    public function limit(int $limit): ?SimplePHP
105
    {
106
        $this->limit = "LIMIT $limit";
107
        return $this;
108
    }
109
110
    /**
111
     * @param int $offset
112
     * @return SimplePHP|null
113
     */
114
    public function offset(int $offset): ?SimplePHP
115
    {
116
        $this->offset = "OFFSET $offset";
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $order
122
     * @return SimplePHP|null
123
     */
124
    public function orderBy(String $order): ?SimplePHP
125
    {
126
        $this->order = "ORDER BY {$order}";
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $name
132
     * @param mixed $arguments
133
     * @return null
134
     */
135
    public function __call(String $name, $arguments)
136
    {
137
        return $this->writeLog("This method does not exist at the SimplePHP: \"<b>{$name}</b>\".");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog('This me...\"<b>'.$name.'</b>\".') targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
138
    }
139
140
    /**
141
     * @param array $deniable
142
     * @return SimplePHP|null
143
     */
144
    public function except(Array $deniable)
145
    {
146
        $this->excepts = $deniable;
147
        return $this;
148
    }
149
150
    /**
151
     * @return null
152
     */
153
    private function deny()
154
    {
155
        if (!empty($this->excepts)) {
156
            foreach ($this->excepts as $except) {
157
                if (isset($this->data[$except])) unset($this->data[$except]);
158
            }
159
        }
160
    }
161
162
    /**
163
     * @param $prop
164
     * @param $value
165
     * @return null
166
     */
167
    public function __set($prop, $value)
168
    {
169
        if (empty($this->data)) {
170
            $this->data = new stdClass();
171
        }
172
173
        $this->data->$prop = $value;
174
    }
175
176
    /**
177
     * @param $attribute
178
     * @return bool
179
     */
180
    public function __isset($attribute): bool
181
    {
182
        return isset($this->data->$attribute);
183
    }
184
185
    /**
186
     * @param $prop
187
     * @return string|null
188
     */
189
    public function __get($prop)
190
    {
191
        return $this->data->$prop ?? null;
192
    }
193
194
    /**
195
     * @return SimplePHP|null
196
     */
197
    public function count(): ?SimplePHP
198
    {
199
        $this->count = true;
200
        return $this;
201
    }
202
203
    /**
204
     * @return array|mixed
205
     */
206
    public function execute(bool $type = false)
207
    {
208
        $this->type = $type;
209
        try {
210
            $execute = $this->conn->query("SELECT {$this->params} FROM {$this->table} {$this->where} {$this->order} {$this->limit} {$this->offset}");
0 ignored issues
show
Bug introduced by
The method query() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
            /** @scrutinizer ignore-call */ 
211
            $execute = $this->conn->query("SELECT {$this->params} FROM {$this->table} {$this->where} {$this->order} {$this->limit} {$this->offset}");

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
211
            $execute->rowCount() > 1 ? 
212
                    $this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) : $this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC));
213
        $this->deny();
214
        return !$this->count ? $this->data : $execute->rowCount();
215
        } catch (PDOException $exc) {
216
            return $this->writeLog($exc->getCode(), true);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog($exc->getCode(), true) targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
217
        }
218
    }
219
220
    /**
221
     * @return null|bool
222
     */
223
    public function destroy()
224
    {
225
        $primary = $this->primary;
226
        if (!isset($this->data->$primary)) {
227
            return $this->writeLog("The primary index was not found.");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog('The pri... index was not found.') targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
228
        }
229
230
        return $this->delete($this->data->$primary);
231
    }
232
233
    /**
234
     * @return null|bool
235
     */
236
    public function save()
237
    {
238
        $primary = $this->primary;
239
        $data = json_decode(json_encode($this->data), true);
240
        if (empty($primary) || !isset($data[$primary])) {
241
            return $this->writeLog("The primary index was not found.");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog('The pri... index was not found.') targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
242
        } else if (!$this->find($data[$primary])->execute()) {
243
            return $this->writeLog("The primary index was not found in the database.");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog('The pri...ound in the database.') targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
244
        }
245
246
        $otherPrimary = $data[$primary];
247
        unset($data[$primary]);
248
        $parameters = implode(',', array_keys($data));
249
        $values = array_values($data);
250
251
        return $this->update($parameters, $values, $otherPrimary);
252
    }
253
254
    /**
255
     * @return SimplePHP|null
256
     */
257
    public function request(Array $request): ?SimplePHP
258
    {
259
        $this->request = $request;
260
        return $this;
261
    }
262
263
    /**
264
     * @return null|bool
265
     */
266
    public function create()
267
    {
268
        $request = $this->request;
269
        if (empty($request)) {
270
            return $this->writeLog("No information was passed to record.");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->writeLog('No info...was passed to record.') targeting SimplePHP\Model\SimplePHP::writeLog() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
271
        }
272
273
        $parameters = implode(',', array_keys($request));
274
        $values = array_values($request);
275
        
276
        return $this->insert($parameters, $values);
277
    }
278
279
    /**
280
     * @param string $message
281
     * @return null
282
     */
283
    protected function writeLog($message, $pdo = false)
284
    {
285
        $message = $pdo ? "Error: PDOCode " . $message : $message;
286
        $archive = fopen(dirname(__DIR__) . DIRECTORY_SEPARATOR . "Logs" . DIRECTORY_SEPARATOR . "Logs.txt", 'a+');
287
        fwrite($archive, "-----SimplePHPLog-----\n" . date("d/m/Y H:i:s", time()) . " -> ". $message ."\n-------\n");
0 ignored issues
show
Bug introduced by
It seems like $archive can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

287
        fwrite(/** @scrutinizer ignore-type */ $archive, "-----SimplePHPLog-----\n" . date("d/m/Y H:i:s", time()) . " -> ". $message ."\n-------\n");
Loading history...
288
        fclose($archive);
0 ignored issues
show
Bug introduced by
It seems like $archive can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
        fclose(/** @scrutinizer ignore-type */ $archive);
Loading history...
289
        return null;
290
    }
291
}