Completed
Push — master ( 87a229...1012d8 )
by Restu
15:25
created

GrammarMySql::where()   C

Complexity

Conditions 7
Paths 19

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 19
nop 0
dl 0
loc 31
rs 6.7272
1
<?php
2
namespace JayaCode\Framework\Core\Database\Query\Grammar;
3
4
use JayaCode\Framework\Core\Database\Query\Query;
5
use Stringy\Stringy;
6
7
/**
8
 * Class GrammarMySql
9
 * @package JayaCode\Framework\Core\Database\Query\Grammar
10
 */
11
class GrammarMySql extends Grammar
12
{
13
14
    /**
15
     * @return string
16
     */
17
    public function build()
18
    {
19
        switch ($this->query->getType()) {
20
            case Query::TYPE_SELECT:
21
                return $this->select();
22
23
            case Query::TYPE_QUERY:
24
                return $this->query->query;
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<JayaCode\Framewor...e\Database\Query\Query>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
        }
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    private function select()
32
    {
33
        $this->queryString = "SELECT ";
34
35
        $this->queryString .= $this->selectColumn();
36
37
        $this->queryString .= " FROM `{$this->query->table}`";
38
39
        $this->queryString .= $this->where();
40
41
        return $this->queryString;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    private function selectColumn()
48
    {
49
        $columns = $this->query->columns;
0 ignored issues
show
Documentation introduced by
The property columns does not exist on object<JayaCode\Framewor...e\Database\Query\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
        if (is_null($columns)) {
51
            $columns = [Query::sql("*")];
52
        }
53
54
        if (is_string($columns)) {
55
            $columns = array($columns);
56
        }
57
58
        foreach ($columns as $key => $val) {
59
            if ($columns[$key] instanceof Query) {
60
                $columns[$key] = $columns[$key]->query;
61
            } else {
62
                $columns[$key] = "`{$val}`";
63
            }
64
        }
65
66
        return implode(', ', $columns);
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    private function where()
73
    {
74
        if (count($this->query->where) <= 0) {
0 ignored issues
show
Documentation introduced by
The property where does not exist on object<JayaCode\Framewor...e\Database\Query\Query>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
            return "";
76
        }
77
78
        $q = Stringy::create("");
79
80
        foreach ($this->query->where as $where) {
0 ignored issues
show
Documentation introduced by
The property where does not exist on object<JayaCode\Framewor...e\Database\Query\Query>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
            $type = $where[0];
82
            $arr = $where[1];
83
84
            if ($q->count() > 1) {
85
                $q = $q->append(" {$type} ");
86
            }
87
88
            if (is_string($arr)) {
89
                $q = $q->append($arr);
90
            }
91
92
            if (is_array($arr)) {
93
                $q = $q->append("`{$arr[0]}`");
94
                $q = $q->append(" {$arr[1]} ");
95
                $q = $q->append("?");
96
97
                $this->params[] = $arr[2];
98
            }
99
        }
100
101
        return $q->count()?$q->prepend(" WHERE ")->__toString():"";
102
    }
103
}
104