Completed
Push — work-fleets ( 98be23...4e14e1 )
by SuperNova.WS
05:25
created

DbSqlLiteral::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class DbSqlLiteral
5
 */
6
7
class DbSqlLiteral extends DbSqlAware {
8
9
  const SQL_LITERAL_ALIAS_NONE = null;
10
  const SQL_LITERAL_ALIAS_AUTO = '';
11
12
  public $literal = '';
13
14
  /**
15
   * DbSqlLiteral constructor.
16
   *
17
   * @param db_mysql|null $db
18
   * @param string        $literal
19
   */
20 1
  public function __construct($db = null, $literal = '') {
21 1
    parent::__construct($db);
22 1
    $this->literal = $literal;
23 1
  }
24
//
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...
25
//  public static function __callStatic($name, $arguments) {
26
//    // method_exists(get_called_class(), $name)
27
//    return call_user_func_array(array(static::build(), '_' .$name), $arguments);
28
//  }
29
30
  /**
31
   * Renders single argument function
32
   * - supports autobuild alias
33
   * - supports direct alias
34
   * - supports dotted field names like 'table.field'
35
   * - supports all fields with '*' and 'table.*'
36
   *
37
   * @param string      $functionName
38
   * @param string      $field
39
   *  - '*' - all fields
40
   *  - string - quoted as field name
41
   * @param null|string $alias
42
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias
43
   * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field
44
   * - other value: using supplied alias
45
   *
46
   * @return $this
47
   */
48 12 View Code Duplication
  public function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49 12
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
50 4
      $alias = $this->makeAliasFromField($functionName, $field);
51 4
    }
52
53 12
    $this->literal = strtoupper($functionName) . '(' . $this->makeFieldFromString($field) . ')';
54
55 12
    if (!empty($alias)) {
56 8
      $this->literal .= ' AS `' . $alias . '`';
57 8
    }
58
59 12
    return $this;
60
  }
61
62
  /**
63
   * @param string      $field
64
   * @param null|string $alias
65
   *
66
   * @return static
67
   *
68
   * @see buildSingleArgument
69
   */
70 12
  public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
71 12
    return $this->buildSingleArgument('max', $field, $alias);
72
  }
73
74
  /**
75
   * @param string      $field
76
   * @param null|string $alias
77
   *
78
   * @return $this
79
   *
80
   * @see buildSingleArgument
81
   */
82
  public function count($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
83
    return $this->buildSingleArgument('count', $field, $alias);
84
  }
85
86
  /**
87
   * @param string      $field
88
   * @param null|string $alias
89
   *
90
   * @return $this
91
   *
92
   * @see buildSingleArgument
93
   */
94
  public function sum($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
95
    return $this->buildSingleArgument('sum', $field, $alias);
96
  }
97
98
  /**
99
   * @param string      $field
100
   * @param null|string $alias
101
   *
102
   * @return $this
103
   *
104
   * @see buildSingleArgument
105
   */
106 View Code Duplication
  public function isNull($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    $functionName = 'isNull';
108
109
    if ($alias === self::SQL_LITERAL_ALIAS_AUTO) {
110
      $alias = $this->makeAliasFromField($functionName, $field);
111
    }
112
113
    $this->literal = $this->makeFieldFromString($field) . ' IS NULL';
114
115
    if (!empty($alias)) {
116
      $this->literal .= ' AS `' . $alias . '`';
117
    }
118
119
    return $this;
120
  }
121
122
123
  /**
124
   * @return string
125
   */
126 12
  public function __toString() {
127 12
    return $this->literal;
128
  }
129
130
}
131