Passed
Push — 2.1 ( 5e848f...bea350 )
by Vincent
08:06 queued 01:53
created

CompilableTrait::getBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Bdf\Prime\Query\Extension;
4
5
use Bdf\Prime\Exception\PrimeException;
6
use Bdf\Prime\Query\Compiler\CompilerState;
7
use Bdf\Prime\Query\Compiler\DeleteCompilerInterface;
8
use Bdf\Prime\Query\Compiler\InsertCompilerInterface;
9
use Bdf\Prime\Query\Compiler\SelectCompilerInterface;
10
use Bdf\Prime\Query\Compiler\UpdateCompilerInterface;
11
use Bdf\Prime\Query\Contract\Compilable;
12
13
/**
14
 * Simple implementation for @see Compilable
15
 *
16
 * @property CompilerState $compilerState
17
 * @property object $compiler
18
 *
19
 * @psalm-require-implements Compilable
20
 */
21
trait CompilableTrait
22
{
23
    /**
24
     * @var Compilable::TYPE_*
25
     */
26
    protected $type = Compilable::TYPE_SELECT;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 954
    public function compile(bool $forceRecompile = false)
32
    {
33 954
        if ($forceRecompile) {
34 4
            $this->compilerState->invalidate('prepared');
35 954
        } elseif ($this->compilerState->compiled) {
36 173
            return $this->compilerState->compiled;
37
        }
38
39 947
        return $this->compilerState->compiled = $this->doCompilation($this->type, $this->compiler);
0 ignored issues
show
Bug introduced by
$this->type of type Bdf\Prime\Query\Contract\Compilable is incompatible with the type string expected by parameter $type of Bdf\Prime\Query\Extensio...eTrait::doCompilation(). ( Ignorable by Annotation )

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

39
        return $this->compilerState->compiled = $this->doCompilation(/** @scrutinizer ignore-type */ $this->type, $this->compiler);
Loading history...
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getBindings(): array
46
    {
47
        return $this->compiler->getBindings($this);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @return Compilable::TYPE_*
54
     */
55 852
    public function type(): string
56
    {
57 852
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->type returns the type Bdf\Prime\Query\Contract\Compilable which is incompatible with the type-hinted return string.
Loading history...
58
    }
59
60
    /**
61
     * Change the query type
62
     * This action will invalidate the current query
63
     *
64
     * @param Compilable::TYPE_* $type One of the Compilable::TYPE_* constant
65
     *
66
     * @return void
67
     */
68 842
    protected function setType(string $type): void
69
    {
70 842
        if ($this->type !== $type) {
0 ignored issues
show
introduced by
The condition $this->type !== $type is always true.
Loading history...
71 709
            $this->compilerState->invalidate();
72 709
            $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type of type string is incompatible with the declared type Bdf\Prime\Query\Contract\Compilable of property $type.

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...
73
        }
74
    }
75
76
    /**
77
     * Perform the query compilation
78
     * Can be overridden for perform custom compilation process
79
     *
80
     * @param Compilable::TYPE_* $type The query type
81
     * @param object $compiler The related compiler
82
     *
83
     * @return mixed The compiled query
84
     *
85
     * @throws PrimeException When the compilation fail
86
     * @throws \LogicException If type is not supported by the query or the compiler
87
     */
88 947
    protected function doCompilation(string $type, object $compiler)
89
    {
90
        switch (true) {
91 947
            case $type === Compilable::TYPE_SELECT && $compiler instanceof SelectCompilerInterface:
92 901
                return $compiler->compileSelect($this);
0 ignored issues
show
Bug introduced by
$this of type Bdf\Prime\Query\Extension\CompilableTrait is incompatible with the type Bdf\Prime\Query\CompilableClause expected by parameter $query of Bdf\Prime\Query\Compiler...erface::compileSelect(). ( Ignorable by Annotation )

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

92
                return $compiler->compileSelect(/** @scrutinizer ignore-type */ $this);
Loading history...
93
94 713
            case $type === Compilable::TYPE_UPDATE && $compiler instanceof UpdateCompilerInterface:
95 91
                return $compiler->compileUpdate($this);
0 ignored issues
show
Bug introduced by
The method compileUpdate() does not exist on Bdf\Prime\Query\Compiler\SelectCompilerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Compiler\SelectCompilerInterface. ( Ignorable by Annotation )

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

95
                return $compiler->/** @scrutinizer ignore-call */ compileUpdate($this);
Loading history...
Bug introduced by
$this of type Bdf\Prime\Query\Extension\CompilableTrait is incompatible with the type Bdf\Prime\Query\CompilableClause expected by parameter $query of Bdf\Prime\Query\Compiler...erface::compileUpdate(). ( Ignorable by Annotation )

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

95
                return $compiler->compileUpdate(/** @scrutinizer ignore-type */ $this);
Loading history...
96
97 680
            case $type === Compilable::TYPE_INSERT && $compiler instanceof InsertCompilerInterface:
98 662
                return $compiler->compileInsert($this);
0 ignored issues
show
Bug introduced by
$this of type Bdf\Prime\Query\Extension\CompilableTrait is incompatible with the type Bdf\Prime\Query\CompilableClause expected by parameter $query of Bdf\Prime\Query\Compiler...erface::compileInsert(). ( Ignorable by Annotation )

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

98
                return $compiler->compileInsert(/** @scrutinizer ignore-type */ $this);
Loading history...
Bug introduced by
The method compileInsert() does not exist on Bdf\Prime\Query\Compiler\SelectCompilerInterface. Did you maybe mean compileSelect()? ( Ignorable by Annotation )

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

98
                return $compiler->/** @scrutinizer ignore-call */ compileInsert($this);

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...
Bug introduced by
The method compileInsert() does not exist on Bdf\Prime\Query\Compiler\UpdateCompilerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Compiler\UpdateCompilerInterface. ( Ignorable by Annotation )

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

98
                return $compiler->/** @scrutinizer ignore-call */ compileInsert($this);
Loading history...
99
100 81
            case $type === Compilable::TYPE_DELETE && $compiler instanceof DeleteCompilerInterface:
101 79
                return $compiler->compileDelete($this);
0 ignored issues
show
Bug introduced by
The method compileDelete() does not exist on Bdf\Prime\Query\Compiler\SelectCompilerInterface. Did you maybe mean compileSelect()? ( Ignorable by Annotation )

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

101
                return $compiler->/** @scrutinizer ignore-call */ compileDelete($this);

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...
Bug introduced by
The method compileDelete() does not exist on Bdf\Prime\Query\Compiler\InsertCompilerInterface. Did you maybe mean compileInsert()? ( Ignorable by Annotation )

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

101
                return $compiler->/** @scrutinizer ignore-call */ compileDelete($this);

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...
Bug introduced by
The method compileDelete() does not exist on Bdf\Prime\Query\Compiler\UpdateCompilerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Compiler\UpdateCompilerInterface. ( Ignorable by Annotation )

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

101
                return $compiler->/** @scrutinizer ignore-call */ compileDelete($this);
Loading history...
Bug introduced by
$this of type Bdf\Prime\Query\Extension\CompilableTrait is incompatible with the type Bdf\Prime\Query\CompilableClause expected by parameter $query of Bdf\Prime\Query\Compiler...erface::compileDelete(). ( Ignorable by Annotation )

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

101
                return $compiler->compileDelete(/** @scrutinizer ignore-type */ $this);
Loading history...
102
103
            default:
104 2
                throw new \LogicException('The query ' . static::class . ' do not supports type ' . $type);
105
        }
106
    }
107
}
108