| 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 |  |  | // | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.