Completed
Push — work-fleets ( eaef65...a4e57a )
by SuperNova.WS
05:17
created

DbSqlLiteral::buildSingleArgument()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 13
rs 9.4285
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
class DbSqlLiteral extends DbSqlAware {
4
5
  const SQL_LITERAL_ALIAS_NONE = null;
6
  const SQL_LITERAL_ALIAS_AUTO = '';
7 1
8 1
  public $literal = '';
9 1
10
  /**
11
   * DbSqlLiteral constructor.
12
   *
13
   * @param db_mysql|null $db
14
   * @param string        $literal
15
   */
16
  public function __construct($db = null, $literal = '') {
17
    parent::__construct($db);
18
    $this->literal = $literal;
19
  }
20
21
  /**
22
   * Renders single argument function
23
   * - supports autobuild alias
24
   * - supports direct alias
25
   * - supports dotted field names like 'table.field'
26
   * - supports all fields with '*' and 'table.*'
27
   *
28
   * @param string      $functionName
29
   * @param string      $field
30
   *  - '*' - all fields
31
   *  - string - quoted as field name
32
   * @param null|string $alias
33
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias
34
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field
35
   * - other value: using supplied alias
36
   *
37
   * @return $this
38
   */
39
  protected function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
40
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
41
      $alias = $this->makeAliasFromField($functionName, $field);
42
    }
43
44
    $this->literal = strtoupper($functionName) . '(' . $this->makeFieldFromString($field) . ')';
45
46
    if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type null|string 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...
47
      $this->literal .= ' AS `' . $alias . '`';
48
    }
49
50
    return $this;
51
  }
52
53
  /**
54
   * @param string      $field
55
   * @param null|string $alias
56
   *
57
   * @return $this
58
   *
59
   * @see buildSingleArgument
60
   */
61
  public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
62
    $this->buildSingleArgument('max', $field, $alias);
63
64
    return $this;
65
  }
66
67
68
  /**
69
   * @return string
70
   */
71
  public function __toString() {
72
    return $this->literal;
73
  }
74
75
}
76