Completed
Push — master ( c891e1...8fa5d0 )
by Beniamin
06:55
created

AbstractBuilder::objectToString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of UnderQuery package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\UnderQuery\QueryBuilder;
13
14
use Phuria\UnderQuery\Parameter\ParameterCollection;
15
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface;
16
use Phuria\UnderQuery\Query\Query;
17
use Phuria\UnderQuery\Reference\ReferenceCollection;
18
use Phuria\UnderQuery\Reference\ReferenceCollectionInterface;
19
use Phuria\UnderQuery\Statement\StatementInterface;
20
use Phuria\UnderQuery\Table\AbstractTable;
21
22
/**
23
 * @author Beniamin Jonatan Šimko <[email protected]>
24
 */
25
abstract class AbstractBuilder implements BuilderInterface
26
{
27
    /**
28
     * @var QueryBuilderFacade
29
     */
30
    private $facade;
31
32
    /**
33
     * @var ReferenceCollectionInterface
34
     */
35
    private $referenceCollection;
36
37
    /**
38
     * @var ParameterCollectionInterface
39
     */
40
    private $parameterCollection;
41
42
    /**
43
     * @var AbstractTable[] $tables
44
     */
45
    private $rootTables = [];
46
47
    /**
48
     * @param QueryBuilderFacade $facade
49
     */
50 7
    public function __construct(QueryBuilderFacade $facade)
51
    {
52 7
        $this->facade = $facade;
53 7
        $this->parameterCollection = new ParameterCollection();
54 7
        $this->referenceCollection = new ReferenceCollection();
55 7
    }
56
57
    /**
58
     * @return BuilderInterface
59
     */
60 1
    public function getQueryBuilder()
61
    {
62 1
        return $this;
63
    }
64
65
    /**
66
     * @return ParameterCollectionInterface
67
     */
68 2
    public function getParameters()
69
    {
70 2
        return $this->parameterCollection;
71
    }
72
73
    /**
74
     * @return ReferenceCollectionInterface
75
     */
76 4
    public function getReferences()
77
    {
78 4
        return $this->referenceCollection;
79
    }
80
81
    /**
82
     * @param mixed  $table
83
     * @param string $alias
0 ignored issues
show
Documentation introduced by
Should the type for parameter $alias not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
84
     *
85
     * @return AbstractTable
86
     */
87 1
    public function addRootTable($table, $alias = null)
88
    {
89 1
        $this->rootTables[] = $table = $this->createTable($table, $alias);
90
91 1
        return $table;
92
    }
93
94
    /**
95
     * @return AbstractTable[]
96
     */
97 3
    public function getRootTables()
98
    {
99 3
        return $this->rootTables;
100
    }
101
102
    /**
103
     * @param mixed       $table
104
     * @param string|null $alias
105
     *
106
     * @return AbstractTable
107
     */
108 1
    public function createTable($table, $alias = null)
109
    {
110 1
        $table = $this->facade->createTable($this, $table);
111
112 1
        if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113 1
            $table->setAlias($alias);
114 1
        }
115
116 1
        return $table;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 1
    public function objectToString($object)
123
    {
124 1
        return $this->getReferences()->createReference($object);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 1
    public function setParameter($name, $value)
131
    {
132 1
        $parameter = $this->getParameters()->getParameter($name);
133 1
        $parameter->setValue($value);
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * @return Query
140
     */
141 1
    public function buildQuery()
142
    {
143 1
        return new Query($this->buildSQL(), $this->parameterCollection->toArray());
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 2
    public function buildSQL()
150
    {
151 2
        return $this->facade->buildSQL($this);
152
    }
153
154
    /**
155
     * @param mixed|null $connectionHint
156
     *
157
     * @return StatementInterface
158
     */
159
    public function buildStatement($connectionHint = null)
160
    {
161
        return $this->facade->buildStatement($this->buildSQL(), $this->getParameters()->toArray(), $connectionHint);
0 ignored issues
show
Unused Code introduced by
The call to QueryBuilderFacade::buildStatement() has too many arguments starting with $connectionHint.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
162
    }
163
}