Create::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace database;
5
6
7
class Create
8
{
9
10
    private $table;
11
    private $create;
12
    private $result;
13
    private $error;
14
    private $data;
15
    private $statements;
16
17
18
    public function __construct()
19
    {
20
21
    }
22
23
    /**
24
     * @return mixed
25
     */
26
    public function getResult()
27
    {
28
        return $this->result;
29
    }
30
31
32
33
    public function create(string $table, array $data)
34
    {
35
        $this->data = $data;
36
        $this->table = $table;
37
        $this->data = array_map('strip_tags', $this->data);
38
        $this->data = array_map('trim', $this->data);
39
40
        if ($this->data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
            $this->setdata();
42
        }
43
    }
44
45
46
    private function setdata()
47
    {
48
        $fields = implode(', ', array_keys($this->data));
49
        $places = ':'.implode(', :', array_keys($this->data));
50
        $this->create = "INSERT INTO {$this->table} ({$fields}) VALUES ({$places})";
51
52
        try{
53
54
            $this->statements = DB::connect()->prepare($this->create);
55
            $this->statements->execute($this->data);
56
            $this->result = DB::connect()->lastInsertId();
57
            $this->error = true;
58
59
        }catch (\PDOException $e){
60
            echo $e->getMessage();
61
62
        }
63
64
    }
65
66
}