Insert::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries;
6
7
use Puzzle\QueryBuilder\Query;
8
use Puzzle\QueryBuilder\Traits\EscaperAware;
9
10
class Insert implements Query
11
{
12
    use EscaperAware;
13
14
    private
15
        $insertPart,
1 ignored issue
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $insertPart.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
        $valuesPart;
17
18 3
    public function __construct(?string $table = null)
19
    {
20 3
        $this->valuesPart = new Snippets\Values();
21
22 3
        if(! empty($table))
23
        {
24 2
            $this->insert($table);
25
        }
26 3
    }
27
28 3
    public function toString(): string
29
    {
30
        $queryParts = array(
31 3
            $this->buildInsertString(),
32 3
            $this->buildValuesString(),
33
        );
34
35 3
        return implode(' ', $queryParts);
36
    }
37
38 3
    public function insert(?string $table): self
39
    {
40 3
        $this->insertPart = new Snippets\TableName($table);
41
42 3
        return $this;
43
    }
44
45 3
    public function values(array $values): self
46
    {
47 3
        $this->valuesPart->values($values);
48
49 3
        return $this;
50
    }
51
52 3
    private function buildInsertString(): string
53
    {
54 3
        return sprintf('INSERT INTO %s', $this->insertPart->toString());
55
    }
56
57 3
    private function buildValuesString(): string
58
    {
59 3
        $this->valuesPart->setEscaper($this->escaper);
60
61 3
        return $this->valuesPart->toString();
62
    }
63
}
64