Passed
Push — master ( e4ea99...6d7b4b )
by Nícollas
01:12
created

SimplePHP::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SimplePHP\Model;
4
5
use SimplePHP\Root\Connection;
6
use PDO;
7
use PDOException;
8
use Exception;
9
use stdClass;
10
use SimplePHP\Model\CRUD as Actions;
11
12
/**
13
 * Class SimplePHP
14
 * @package NicollasSilva\SimplePHP
15
 */
16
class SimplePHP extends Connection {
17
    use Actions;
18
19
    /** @var string */
20
    protected $sentence = '';
21
22
    /** @var string */
23
    protected $offset;
24
25
    /** @var string */
26
    protected $order;
27
28
    /** @var string */
29
    protected $params = '*';
30
31
    /** @var string */
32
    protected $where;
33
34
    /** @var string */
35
    protected $limit;
36
37
    /** @var string */
38
    protected $table;
39
40
    /** @var object|null */
41
    protected $data;
42
43
    /** @var bool */
44
    protected $type;
45
46
    /** @var array */
47
    protected $excepts = [];
48
49
    /** @var string */
50
    protected $primary;
51
52
    /**
53
     * Get tablename of children model
54
     * @param string|null $tableName
55
     */
56
    function __construct(?string $tableName, ?string $primaryKey)
57
    {
58
        parent::__construct(); $this->table = $tableName; $this->primary = $primaryKey;
59
    }
60
61
    /**
62
     * @param int|null $id
63
     * @return SimplePHP
64
     */
65
    public function find(?int $id = null): SimplePHP
66
    {
67
        is_int($id) ? self::where('id', $id) : null;
0 ignored issues
show
Bug Best Practice introduced by
The method SimplePHP\Model\SimplePHP::where() is not static, but was called statically. ( Ignorable by Annotation )

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

67
        is_int($id) ? self::/** @scrutinizer ignore-call */ where('id', $id) : null;
Loading history...
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $condition
73
     * @param string $value
74
     * @return SimplePHP|null
75
     */
76
    public function where(string $condition, string $value): SimplePHP
77
    {
78
        $this->where = "WHERE " . (mb_strlen($this->where > 6) ? "&& {$condition} = '{$value}'" : "{$condition} = '{$value}'");
79
        return $this;
80
    }
81
82
    /**
83
     * @param array $params
84
     * @return SimplePHP|null
85
     */
86
    public function only(array $params): SimplePHP
87
    {
88
        $params !== null ? $this->params = implode($params, ',') : $this->params = '*';
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
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

88
        $params !== null ? $this->params = /** @scrutinizer ignore-call */ implode($params, ',') : $this->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...
89
        return $this;
90
    }
91
92
    /**
93
     * @param int $limit
94
     * @return SimplePHP|null
95
     */
96
    public function limit(int $limit): SimplePHP
97
    {
98
        $this->limit = "LIMIT $limit";
99
        return $this;
100
    }
101
102
    /**
103
     * @param int $offset
104
     * @return SimplePHP|null
105
     */
106
    public function offset(int $offset): SimplePHP
107
    {
108
        $this->offset = "OFFSET $offset";
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $order
114
     * @return SimplePHP|null
115
     */
116
    public function orderBy(string $order): SimplePHP
117
    {
118
        $this->order = "ORDER BY {$order}";
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $name
124
     * @param mixed $arguments
125
     * @return string error
126
     */
127
    public function __call(string $name, $arguments)
128
    {
129
        return "This method does not exist at the SimplePHP: \"<b>{$name}</b>\".";
130
    }
131
132
    /**
133
     * @param bool $bool
134
     * @return SimplePHP
135
     */
136
    public function asAttribute(bool $bool = false): SimplePHP
137
    {
138
        $this->type = $bool;
139
        return $this;
140
    }
141
142
    /**
143
     * @param array $deniable
144
     * @return SimplePHP
145
     */
146
    public function except(array $deniable)
147
    {
148
        $this->excepts = $deniable;
149
        return $this;
150
    }
151
152
    public function __set($prop, $value)
153
    {
154
        if (empty($this->data)) {
155
            $this->data = new stdClass();
156
        }
157
158
        $this->data->$prop = $value;
159
    }
160
161
    /**
162
     * @param $name
163
     * @return bool
164
     */
165
    public function __isset($attribute): bool
166
    {
167
        return isset($this->data->$attribute);
168
    }
169
170
    /**
171
     * Method to destroy @except method
172
     */
173
    private function deny()
174
    {
175
        if(!empty($this->excepts)) {
176
            switch (!is_object($this->data) && $count = count($this->data)) {
177
                case (!isset($this->data[0]) && !empty($this->data)):
178
                    foreach($this->excepts as $except) {
179
                        if(isset($this->data[$except])) unset($this->data[$except]);
180
                    }
181
                    break;
182
                case ($count >= 2 && isset($this->data[0])):
183
                    foreach($this->excepts as $except) {
184
                        for($i = 0; $i < $count; $i++) {
185
                            if(isset($this->data[$i][$except])) unset($this->data[$i][$except]);
186
                        }
187
                    }
188
                    break;
189
            default:
190
                return [];
191
            }
192
        }
193
    }
194
195
    /**
196
     * @return array|mixed
197
     */
198
    public function execute()
199
    {
200
        try {
201
            $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

201
            /** @scrutinizer ignore-call */ 
202
            $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...
202
            $execute->rowCount() > 1 ? 
203
                    $this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) :
204
                    $this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC));
205
            $this->deny();
206
            return $this->data;
207
        } catch(PDOException $exc) {
208
            return $exc->getMessage();
209
        }
210
    }
211
212
    /**
213
     * @return Exception|bool
214
     */
215
    public function destroy() {
216
        $primary = $this->primary;
217
        if(!isset($this->data->$primary)) {
218
            throw new Exception("|| Índice primário não encontrado: {$primary} ||");
219
        }
220
        return $this->delete($this->data->$primary);
221
    }
222
}