Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 41 | class Query extends Component implements QueryInterface |
||
| 42 | { |
||
| 43 | use QueryTrait; |
||
| 44 | |||
| 45 | const SEARCH_SCOPE_SUB = 'ldap_search'; |
||
| 46 | const SEARCH_SCOPE_ONE = 'ldap_list'; |
||
| 47 | const SEARCH_SCOPE_BASE = 'ldap_read'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string the scope of search |
||
| 51 | * The search scope: |
||
| 52 | * Query::SEARCH_SCOPE_SUB searches the complete subtree including the $baseDn node. This is the default value. |
||
| 53 | * Query::SEARCH_SCOPE_ONE restricts search to one level below $baseDn. |
||
| 54 | * Query::SEARCH_SCOPE_BASE restricts search to the $baseDn itself; this can be used to efficiently retrieve a single entry by its DN. |
||
| 55 | */ |
||
| 56 | public $scope = self::SEARCH_SCOPE_SUB; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array the columns being selected. For example, `['id', 'name']`. |
||
| 60 | * This is used to construct the SEARCH function in a LDAP statement. If not set, it means selecting all columns. |
||
| 61 | * @see select() |
||
| 62 | */ |
||
| 63 | public $select; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string The search filter. Format is described in the LDAP documentation. |
||
| 67 | * @see http://www.faqs.org/rfcs/rfc4515.html |
||
| 68 | */ |
||
| 69 | public $filter; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Creates a LDAP command that can be used to execute this query. |
||
| 73 | * @param Connection $db the database connection. |
||
| 74 | * If this parameter is not given, the `db` application component will be used. |
||
| 75 | * @return DataReader |
||
| 76 | */ |
||
| 77 | protected function execute($db = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Executes the query and returns all results as an array. |
||
| 100 | * @param Connection $db the database connection. |
||
| 101 | * If this parameter is not given, the `db` application component will be used. |
||
| 102 | * @return array the query results. If the query results in nothing, an empty array will be returned. |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function all($db = null) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Converts the raw query results into the format as specified by this query. |
||
| 117 | * This method is internally used to convert the data fetched from database |
||
| 118 | * into the format as required by this query. |
||
| 119 | * @param array $rows the raw query result from database |
||
| 120 | * @return array the converted query result |
||
| 121 | */ |
||
| 122 | public function populate($rows) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Executes the query and returns a single row of result. |
||
| 141 | * @param Connection $db the database connection. |
||
| 142 | * If this parameter is not given, the `db` application component will be used. |
||
| 143 | * @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query |
||
| 144 | * results in nothing. |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function one($db = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the number of entries in a search. |
||
| 159 | * @param Connection $db the database connection |
||
| 160 | * If this parameter is not given (or null), the `db` application component will be used. |
||
| 161 | * @return integer number of entries. |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function count($db = null) |
|
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns a value indicating whether the query result contains any row of data. |
||
| 176 | * @param Connection $db the database connection. |
||
| 177 | * If this parameter is not given, the `db` application component will be used. |
||
| 178 | * @return boolean whether the query result contains any row of entries. |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function exists($db = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the SELECT part of the query. |
||
| 192 | * @param string|array $columns the columns to be selected. |
||
| 193 | * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
||
| 194 | * |
||
| 195 | * ```php |
||
| 196 | * $query->addSelect(['cn, mail'])->one(); |
||
| 197 | * ``` |
||
| 198 | * |
||
| 199 | * @return $this the query object itself |
||
| 200 | */ |
||
| 201 | public function select($columns) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Add more columns to the select part of the query. |
||
| 212 | * |
||
| 213 | * ```php |
||
| 214 | * $query->addSelect(['cn, mail'])->one(); |
||
| 215 | * ``` |
||
| 216 | * |
||
| 217 | * @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more |
||
| 218 | * details about the format of this parameter. |
||
| 219 | * @return $this the query object itself |
||
| 220 | * @see select() |
||
| 221 | */ |
||
| 222 | public function addSelect($columns) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Adds a filtering condition for a specific column and allow the user to choose a filter operator. |
||
| 237 | * |
||
| 238 | * It adds an additional WHERE condition for the given field and determines the comparison operator |
||
| 239 | * based on the first few characters of the given value. |
||
| 240 | * The condition is added in the same way as in [[andFilterWhere]] so [[isEmpty()|empty values]] are ignored. |
||
| 241 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
| 242 | * |
||
| 243 | * The comparison operator is intelligently determined based on the first few characters in the given value. |
||
| 244 | * In particular, it recognizes the following operators if they appear as the leading characters in the given value: |
||
| 245 | * |
||
| 246 | * - `<`: the column must be less than the given value. |
||
| 247 | * - `>`: the column must be greater than the given value. |
||
| 248 | * - `<=`: the column must be less than or equal to the given value. |
||
| 249 | * - `>=`: the column must be greater than or equal to the given value. |
||
| 250 | * - `~=`: the column must approximate the given value. |
||
| 251 | * - `=`: the column must be equal to the given value. |
||
| 252 | * - If none of the above operators is detected, the `$defaultOperator` will be used. |
||
| 253 | * |
||
| 254 | * @param string $name the column name. |
||
| 255 | * @param string $value the column value optionally prepended with the comparison operator. |
||
| 256 | * @param string $defaultOperator The operator to use, when no operator is given in `$value`. |
||
| 257 | * Defaults to `=`, performing an exact match. |
||
| 258 | * @return $this The query object itself |
||
| 259 | */ |
||
| 260 | public function andFilterCompare($name, $value, $defaultOperator = '=') |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Creates a new Query object and copies its property values from an existing one. |
||
| 273 | * The properties being copies are the ones to be used by query builders. |
||
| 274 | * @param Query $from the source query object |
||
| 275 | * @return Query the new Query object |
||
| 276 | */ |
||
| 277 | public static function create(Query $from) |
||
| 288 | } |
||
| 289 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.