Completed
Push — work-fleets ( b0e897...355465 )
by SuperNova.WS
13:11 queued 07:54
created

DbSqlLiteral   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 48.48%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 139
ccs 16
cts 33
cp 0.4848
rs 10
wmc 13
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sum() 0 3 1
A __construct() 0 3 1
A literal() 0 5 1
A buildSingleArgument() 0 13 4
A max() 0 3 1
A count() 0 3 1
A isNull() 0 15 3
A __toString() 0 3 1
1
<?php
2
3
/**
4
 * Class DbSqlLiteral
5
 */
6
class DbSqlLiteral extends DbSqlAware {
7
8
  const SQL_LITERAL_ALIAS_NONE = null;
9
  const SQL_LITERAL_ALIAS_AUTO = '';
10
11
  /**
12
   * ALWAYS STRING!
13
   *
14
   * @var string $literal
15
   */
16
  public $literal = '';
17
18
  /**
19
   * DbSqlLiteral constructor.
20
   *
21
   * @param db_mysql|null $db
22
   * @param string        $literal
23
   */
24 1
  public function __construct($db = null, $literal = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $literal is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25 1
    parent::__construct($db);
26 1
  }
27
28
  /**
29
   * @param mixed $value
30
   *
31
   * @return $this
32
   */
33
  public function literal($value) {
34
    $this->literal = (string)$value;
35
36
    return $this;
37
  }
38
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
39
//  public static function __callStatic($name, $arguments) {
40
//    // method_exists(get_called_class(), $name)
41
//    return call_user_func_array(array(static::build(), '_' .$name), $arguments);
42
//  }
43
44
  /**
45
   * Renders single argument function
46
   * - supports autobuild alias
47
   * - supports direct alias
48
   * - supports dotted field names like 'table.field'
49
   * - supports all fields with '*' and 'table.*'
50
   *
51
   * @param string      $functionName
52
   * @param string      $field
53
   *  - '*' - all fields
54
   *  - string - quoted as field name
55
   * @param null|string $alias
56
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias
57
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field
58
   * - other value: using supplied alias
59
   *
60
   * @return $this
61
   */
62 12
  public function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
63 12
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
64 4
      $alias = $this->aliasFromField($functionName, $field);
65 4
    }
66
67 12
    $this->literal = strtoupper($functionName) . '(' . $this->quoteField($field) . ')';
68
69 12
    if (self::SQL_LITERAL_ALIAS_NONE !== $alias && !empty($alias)) {
70 8
      $this->literal .= ' AS `' . $alias . '`';
71 8
    }
72
73 12
    return $this;
74
  }
75
76
  /**
77
   * @param string      $field
78
   * @param null|string $alias
79
   *
80
   * @return static
81
   *
82
   * @see buildSingleArgument
83
   */
84 12
  public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
85 12
    return $this->buildSingleArgument('max', $field, $alias);
86
  }
87
88
  /**
89
   * @param string      $field
90
   * @param null|string $alias
91
   *
92
   * @return $this
93
   *
94
   * @see buildSingleArgument
95
   */
96
  public function count($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
97
    return $this->buildSingleArgument('count', $field, $alias);
98
  }
99
100
  /**
101
   * @param string      $field
102
   * @param null|string $alias
103
   *
104
   * @return $this
105
   *
106
   * @see buildSingleArgument
107
   */
108
  public function sum($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
109
    return $this->buildSingleArgument('sum', $field, $alias);
110
  }
111
112
  /**
113
   * @param string      $field
114
   * @param null|string $alias
115
   *
116
   * @return $this
117
   *
118
   * @see buildSingleArgument
119
   */
120
  public function isNull($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
121
    $functionName = 'isNull';
122
123
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
124
      $alias = $this->aliasFromField($functionName, $field);
125
    }
126
127
    $this->literal = $this->quoteField($field) . ' IS NULL';
128
129
    if (!empty($alias)) {
130
      $this->literal .= ' AS `' . $alias . '`';
131
    }
132
133
    return $this;
134
  }
135
136
137
  /**
138
   * @return string
139
   */
140 12
  public function __toString() {
141 12
    return $this->literal;
142
  }
143
144
}
145