Passed
Push — master ( e2745c...f14e7e )
by Nícollas
01:10
created

SimplePHP::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
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 SimplePHP\Model\CRUD as Actions;
9
10
/**
11
 * Class SimplePHP
12
 * @package NicollasSilva\SimplePHP
13
 */
14
class SimplePHP extends Connection {
15
    use Actions;
16
17
    /** @var string */
18
    protected $sentence = '';
19
20
    /** @var int */
21
    protected $offset;
22
23
    /** @var string */
24
    protected $order;
25
26
    /** @var string */
27
    protected $params = '*';
28
29
    /** @var string */
30
    protected $where;
31
32
    /** @var string */
33
    protected $limit;
34
35
    /** @var string */
36
    protected $table;
37
38
    /** @var object|null */
39
    protected $data;
40
41
    /** @var bool */
42
    protected $type;
43
44
    /** @var array */
45
    public $excepts = [];
46
47
    /**
48
     * Get tablename of children model
49
     * @param string|null $tableName
50
     */
51
    function __construct(?string $tableName) {
52
53
        parent::__construct(); $this->table = $tableName;
54
55
    }
56
57
    /**
58
     * @param int|null $id
59
     * @return SimplePHP
60
     */
61
    public function find(?int $id = null): SimplePHP {
62
63
        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

63
        is_int($id) ? self::/** @scrutinizer ignore-call */ where('id', $id) : null;
Loading history...
64
        return $this;
65
66
    }
67
68
    /**
69
     * @param string $condition
70
     * @param string $value
71
     * @return SimplePHP|null
72
     */
73
    public function where(string $condition, string $value): SimplePHP {
74
75
        $this->where = "WHERE " . (mb_strlen($this->where > 6) ? "&& {$condition} = '{$value}'" : "{$condition} = '{$value}'");
76
        return $this;
77
78
    }
79
80
    /**
81
     * @param array $params
82
     * @return SimplePHP|null
83
     */
84
    public function only(array $params): SimplePHP {
85
86
        $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

86
        $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...
87
        return $this;
88
89
    }
90
91
    /**
92
     * @param int $limit
93
     * @return SimplePHP|null
94
     */
95
    public function limit(int $limit): SimplePHP {
96
        
97
        $this->limit = "LIMIT $limit";
98
        return $this;
99
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";
0 ignored issues
show
Documentation Bug introduced by
The property $offset was declared of type integer, but 'OFFSET '.$offset is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
109
        return $this;
110
111
    }
112
113
    /**
114
     * @param string $order
115
     * @return SimplePHP|null
116
     */
117
    public function orderBy(string $order): SimplePHP {
118
        
119
        $this->order = "ORDER BY {$order}";
120
        return $this;
121
122
    }
123
124
    /**
125
     * @param string $name
126
     * @param mixed $arguments
127
     * @return string error
128
     */
129
    public function __call(string $name, $arguments) {
130
131
        return "This method does not exist at the SimplePHP: \"<b>{$name}</b>\".";
132
133
    }
134
135
    /**
136
     * @param bool $bool
137
     * @return SimplePHP
138
     */
139
    public function asAttribute(bool $bool = false): SimplePHP {
140
141
        $this->type = $bool;
142
        return $this;
143
144
    }
145
146
    /**
147
     * @param array $deniable
148
     * @return SimplePHP
149
     */
150
    public function except(array $deniable) {
151
152
            $this->excepts = $deniable;
153
154
        return $this;
155
156
    }
157
158
    /**
159
     * Method to destroy @except method
160
     */
161
    private function deny() {
162
163
        if(!empty($this->excepts)) {
164
            foreach($this->excepts as $except) {
165
                if(isset($this->data[$except])) unset($this->data[$except]);
166
            }
167
        }
168
169
    }
170
171
    /**
172
     * @return array|mixed|null
173
     */
174
    public function execute() {
175
176
        try {
177
            $execute = $this->conn->query("SELECT {$this->params} FROM {$this->table} {$this->where} {$this->order} {$this->limit} {$this->offset}");
178
            $execute->rowCount() > 1 ? 
179
                    $this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) :
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->type ? $execute->...chAll(PDO::FETCH_ASSOC) of type array is incompatible with the declared type null|object of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
180
                    $this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC));
181
            self::deny();
0 ignored issues
show
Bug Best Practice introduced by
The method SimplePHP\Model\SimplePHP::deny() 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

181
            self::/** @scrutinizer ignore-call */ 
182
                  deny();
Loading history...
182
            return $this->data;
183
        } catch(PDOException $exc) {
184
            return $exc->getMessage();
185
        }
186
187
    }
188
189
    public function save() {
190
191
        
192
193
    }
194
195
}