Passed
Push — master ( edcad2...a41dc2 )
by Nícollas
01:49
created

SimplePHP::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
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 string */
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
    protected $excepts = [];
46
47
    /** @var string */
48
    protected $primary;
49
50
    /**
51
     * Get tablename of children model
52
     * @param string|null $tableName
53
     */
54
    function __construct(?string $tableName, ?string $primaryKey)
55
    {
56
        parent::__construct(); $this->table = $tableName; $this->primary = $primaryKey;
57
    }
58
59
    /**
60
     * @param int|null $id
61
     * @return SimplePHP
62
     */
63
    public function find(?int $id = null): SimplePHP
64
    {
65
        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

65
        is_int($id) ? self::/** @scrutinizer ignore-call */ where('id', $id) : null;
Loading history...
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $condition
71
     * @param string $value
72
     * @return SimplePHP|null
73
     */
74
    public function where(string $condition, string $value): SimplePHP
75
    {
76
        $this->where = "WHERE " . (mb_strlen($this->where > 6) ? "&& {$condition} = '{$value}'" : "{$condition} = '{$value}'");
77
        return $this;
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
     * @param int $limit
92
     * @return SimplePHP|null
93
     */
94
    public function limit(int $limit): SimplePHP
95
    {
96
        $this->limit = "LIMIT $limit";
97
        return $this;
98
    }
99
100
    /**
101
     * @param int $offset
102
     * @return SimplePHP|null
103
     */
104
    public function offset(int $offset): SimplePHP
105
    {
106
        $this->offset = "OFFSET $offset";
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $order
112
     * @return SimplePHP|null
113
     */
114
    public function orderBy(string $order): SimplePHP
115
    {
116
        $this->order = "ORDER BY {$order}";
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $name
122
     * @param mixed $arguments
123
     * @return string error
124
     */
125
    public function __call(string $name, $arguments)
126
    {
127
        return "This method does not exist at the SimplePHP: \"<b>{$name}</b>\".";
128
    }
129
130
    /**
131
     * @param bool $bool
132
     * @return SimplePHP
133
     */
134
    public function asAttribute(bool $bool = false): SimplePHP
135
    {
136
        $this->type = $bool;
137
        return $this;
138
    }
139
140
    /**
141
     * @param array $deniable
142
     * @return SimplePHP
143
     */
144
    public function except(array $deniable)
145
    {
146
        $this->excepts = $deniable;
147
        return $this;
148
    }
149
150
    /**
151
     * Method to destroy @except method
152
     */
153
    private function deny()
154
    {
155
        if(!empty($this->excepts)) {
156
            switch (!is_object($this->data) && $count = count($this->data)) {
157
                case (!isset($this->data[0]) && !empty($this->data)):
158
                    foreach($this->excepts as $except) {
159
                        if(isset($this->data[$except])) unset($this->data[$except]);
160
                    }
161
                    break;
162
                case ($count >= 2 && isset($this->data[0])):
163
                    foreach($this->excepts as $except) {
164
                        for($i = 0; $i < $count; $i++) {
165
                            if(isset($this->data[$i][$except])) unset($this->data[$i][$except]);
166
                        }
167
                    }
168
                    break;
169
            default:
170
                return [];
171
            }
172
        }
173
    }
174
175
    /**
176
     * @return array|mixed
177
     */
178
    public function execute()
179
    {
180
        try {
181
            $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

181
            /** @scrutinizer ignore-call */ 
182
            $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...
182
            $execute->rowCount() > 1 ? 
183
                    $this->data = ($this->type ? $execute->fetchAll(PDO::FETCH_CLASS, static::class) : $execute->fetchAll(PDO::FETCH_ASSOC)) :
184
                    $this->data = ($this->type ? $execute->fetchObject(static::class) : $execute->fetch(PDO::FETCH_ASSOC));
185
            $this->deny();
186
            return $this->data;
187
        } catch(PDOException $exc) {
188
            return $exc->getMessage();
189
        }
190
    }
191
}