Passed
Push — master ( b2bdac...3a7f22 )
by Ondřej
03:22
created

ConstrainedRelationDefinition   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A toSql() 0 13 2
1
<?php
2
namespace Ivory\Query;
3
4
use Ivory\Lang\Sql\ISqlPredicate;
5
use Ivory\Lang\SqlPattern\SqlPattern;
6
use Ivory\Type\ITypeDictionary;
7
8
class ConstrainedRelationDefinition extends RelationDefinition implements IRelationDefinition
9
{
10
    private $baseRelDef;
11
    private $cond;
12
    private $args;
13
14
    /**
15
     * @param IRelationDefinition $relationDefinition definition of relation to constrain
16
     * @param ISqlPredicate|SqlPattern|string $cond
17
     * @param array $args
18
     */
19
    public function __construct(IRelationDefinition $relationDefinition, $cond, ...$args)
20
    {
21
        if ($cond instanceof ISqlPredicate && $args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args 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...
22
            throw new \InvalidArgumentException('$args not supported for ' . ISqlPredicate::class . ' condition');
23
        }
24
25
26
        $this->baseRelDef = $relationDefinition;
27
        $this->cond = $cond;
28
        $this->args = $args;
29
    }
30
31
    public function toSql(ITypeDictionary $typeDictionary): string
32
    {
33
        $relSql = $this->baseRelDef->toSql($typeDictionary);
34
35
        if ($this->cond instanceof ISqlPredicate) {
36
            $condSql = $this->cond->getSql();
37
        } else {
38
            $condDef = SqlRelationDefinition::fromPattern($this->cond, ...$this->args);
39
            $condSql = $condDef->toSql($typeDictionary);
40
        }
41
42
        return "SELECT *\nFROM (\n$relSql\n) t\nWHERE $condSql";
43
    }
44
}
45