Completed
Push — master ( 439406...4b2726 )
by Hong
03:09
created

ClauseTrait::isRaw()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 8.8571
cc 5
eloc 8
nc 4
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Interfaces\RawInterface;
18
use Phossa2\Query\Interfaces\ClauseInterface;
19
use Phossa2\Query\Interfaces\StatementInterface;
20
21
22
/**
23
 * ClauseTrait
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ClauseInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait ClauseTrait
32
{
33
    /**
34
     * storage for clauses
35
     *
36
     * @var    array
37
     * @access protected
38
     */
39
    protected $clause = [];
40
41
    /**
42
     * Is $str a raw string ?
43
     *
44
     * @param  mixed $str
45
     * @param  bool $rawMode
46
     * @access protected
47
     */
48
    protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */
49
    {
50
        if ($rawMode) {
51
            return true;
52
        } elseif (is_string($str)) {
53
            return (bool) preg_match('/[^0-9a-zA-Z\$_.]/', $str);
54
        } elseif (is_object($str) && $str instanceof RawInterface) {
55
            return true;
56
        }
57
        return false;
58
    }
59
60
    /**
61
     * Quote an alias if not an int
62
     *
63
     * @param  int|string $alias
64
     * @return string
65
     * @access protected
66
     */
67
    protected function quoteAlias($alias)/*# : string */
68
    {
69
        return is_int($alias) ? '' : (' AS ' . $this->quoteSpace($alias));
70
    }
71
72
    /**
73
     * Quote an item with spaces allowed in between
74
     *
75
     * @param  string|StatementInterface $item
76
     * @param  bool $rawMode
77
     * @access protected
78
     */
79
    protected function quoteItem(
80
        $item, /*# bool */ $rawMode = false
81
    )/*# : string */ {
82
        if (is_object($item) && $item instanceof StatementInterface) {
83
            // @TODO quoteItem check settings
84
            return '(' . $item->getStatement([], false) . ')';
0 ignored issues
show
Unused Code introduced by
The call to StatementInterface::getStatement() has too many arguments starting with false.

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...
85
        } else {
86
            return $rawMode ? (string) $item : $this->quote($item);
87
        }
88
    }
89
90
    /**
91
     * Return specific clause part
92
     *
93
     * @param  string $clauseName
94
     * @param  array
95
     * @access protected
96
     */
97
    protected function &getClause(/*# string */ $clauseName)/*# : array */
98
    {
99
        if (empty($clauseName)) {
100
            return $this->clause;
101
        } else {
102
            if (!isset($this->clause[$clauseName])) {
103
                $this->clause[$clauseName] = [];
104
            }
105
            return $this->clause[$clauseName];
106
        }
107
    }
108
109
    /**
110
     * Convert a template 'COUNT(%s)' to 'COUNT(`score`)'
111
     *
112
     * @param  string $template
113
     * @param  string|string[] $col column[s]
114
     * @return string
115
     * @access protected
116
     */
117
    protected function clauseTpl(/*# string */ $template, $col)/*# : string */
118
    {
119
        $quoted = [];
120
        foreach ((array) $col as $c) {
121
            $quoted[] = $this->quote($c);
122
        }
123
        return vsprintf($template, $quoted);
124
    }
125
126
    protected function quote(/*# string */ $str)/*# : string */
127
    {
128
        // @TODO quote()
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
        return '"' . $str . '"';
130
    }
131
132
    protected function quoteSpace(/*# string */ $str)/*# : string */
133
    {
134
        // @TODO quoteSpace
135
        return $this->quote($str);
136
    }
137
138
    /**
139
     * Process value
140
     *
141
     * @param  mixed $value
142
     * @return string
143
     * @access protected
144
     */
145
    protected function processValue($value)/*# : string */
146
    {
147
        // @TODO processValue
148
        return (string) $value;
149
    }
150
151
    /**
152
     * Join each clause
153
     *
154
     * @param  string $prefix
155
     * @param  string $seperator
156
     * @param  array $clause
157
     * @param  array $settings
158
     * @return string
159
     * @access protected
160
     */
161
    protected function joinClause(
162
        /*# : string */ $prefix,
163
        /*# : string */ $seperator,
164
        array $clause,
165
        array $settings
166
    )/*# : string */ {
167
        if (empty($clause)) {
168
            return '';
169
        } else {
170
            $join = $settings['seperator'] . $settings['indent'];
171
            $pref = empty($prefix) ? '' : ($prefix . $join);
172
            return $settings['seperator'] . $pref . join($seperator . $join, $clause);
173
        }
174
    }
175
}
176