| Total Complexity | 43 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class Base implements \Aimeos\Base\Criteria\Iface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Tests if all list entries implement the passed interface name |
||
| 25 | * |
||
| 26 | * @param string $interface Interface name |
||
| 27 | * @param iterable $list List of items |
||
| 28 | * @return iterable List of tested items |
||
| 29 | * @throws \Aimeos\Base\Exception If at least one items doesn't implement the interface |
||
| 30 | */ |
||
| 31 | public static function implements( string $interface, iterable $list ) : iterable |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the database type constant for the given type value |
||
| 46 | * |
||
| 47 | * @param string $value Type value |
||
| 48 | * @return int Database type constant |
||
| 49 | */ |
||
| 50 | public static function type( string $value ) : int |
||
| 51 | { |
||
| 52 | switch( $value ) |
||
| 53 | { |
||
| 54 | case 'null': |
||
| 55 | return \Aimeos\Base\DB\Statement\Base::PARAM_NULL; |
||
| 56 | case 'bool': |
||
| 57 | case 'boolean': |
||
| 58 | return \Aimeos\Base\DB\Statement\Base::PARAM_BOOL; |
||
| 59 | case 'int': |
||
| 60 | case 'integer': |
||
| 61 | return \Aimeos\Base\DB\Statement\Base::PARAM_INT; |
||
| 62 | case 'float': |
||
| 63 | return \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT; |
||
| 64 | } |
||
| 65 | |||
| 66 | return \Aimeos\Base\DB\Statement\Base::PARAM_STR; |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns an array representation of the expression that can be parsed again |
||
| 72 | * |
||
| 73 | * @return array Multi-dimensional expression structure |
||
| 74 | */ |
||
| 75 | public function __toArray() : array |
||
| 76 | { |
||
| 77 | $cond = $this->getConditions(); |
||
| 78 | return $cond ? $cond->__toArray() : []; |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Adds a new expression to the existing list combined by the AND operator. |
||
| 84 | * |
||
| 85 | * You can add expression is three ways: |
||
| 86 | * |
||
| 87 | * - Name, operator and value: |
||
| 88 | * $f->add( 'product.code', '==', 'abc' ); |
||
| 89 | * |
||
| 90 | * - Name/value pairs and optional operator ("==" by default): |
||
| 91 | * $f->add( ['product.type' => 'voucher', 'product.status' => 1], '!=' ); |
||
| 92 | * $f->add( ['product.type' => 'default', 'product.status' => 1] ); |
||
| 93 | * |
||
| 94 | * - Single expression: |
||
| 95 | * $f->add( $f->is( 'product.code', '==', 'abc' ) ); |
||
| 96 | * $f->add( $f->and( [$f->is( 'product.code', '==', 'abc' ), $f->is( 'product.status', '>', 0 )] ); |
||
| 97 | * $f->add( $f->or( [$f->is( 'product.code', '==', 'abc' ), $f->is( 'product.label', '=~', 'abc' )] ); |
||
| 98 | * $f->add( $f->not( $f->is( 'product.code', '=~', 'abc' ) ); |
||
| 99 | * |
||
| 100 | * @param \Aimeos\Base\Criteria\Expression\Combine\Iface|\Aimeos\Base\Criteria\Expression\Compare\Iface|array|string|null Expression, list of name/value pairs or name |
||
| 101 | * @param string $operator Operator to compare name and value with |
||
| 102 | * @param mixed $value Value to compare the name with |
||
| 103 | * @return \Aimeos\Base\Criteria\Iface Same object for fluent interface |
||
| 104 | */ |
||
| 105 | public function add( $expr, string $operator = '==', $value = null ) : \Aimeos\Base\Criteria\Iface |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Combines the expression with an AND operator |
||
| 147 | * |
||
| 148 | * @param \Aimeos\Base\Criteria\Expression\Compare\Iface[] $list List of expression objects |
||
| 149 | * @return \Aimeos\Base\Criteria\Expression\Combine\Iface Combine expression object |
||
| 150 | */ |
||
| 151 | public function and( array $list ) : \Aimeos\Base\Criteria\Expression\Combine\Iface |
||
| 152 | { |
||
| 153 | return $this->combine( '&&', $list ); |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Creates a new compare expression. |
||
| 159 | * |
||
| 160 | * Available comparision operators are: |
||
| 161 | * "==": item EQUAL value |
||
| 162 | * "!=": item NOT EQUAL value |
||
| 163 | * "~=": item LIKE value |
||
| 164 | * ">=": item GREATER OR EQUAL value |
||
| 165 | * "<=": item SMALLER OR EQUAL value |
||
| 166 | * ">": item GREATER value |
||
| 167 | * "<": item SMALLER value |
||
| 168 | * |
||
| 169 | * @param string $name Name of the column or property that should be used for comparison |
||
| 170 | * @param string $operator One of the known operators |
||
| 171 | * @param mixed $value Value the column or property should be compared to |
||
| 172 | * @return \Aimeos\Base\Criteria\Expression\Compare\Iface Compare expression object |
||
| 173 | */ |
||
| 174 | public function is( string $name, string $operator, $value ) : \Aimeos\Base\Criteria\Expression\Compare\Iface |
||
| 175 | { |
||
| 176 | return $this->compare( $operator, $name, $value ); |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Creates a function signature for expressions used in is() and add(). |
||
| 182 | * |
||
| 183 | * @param string $name Function name without parentheses |
||
| 184 | * @param array $params Single- or multi-dimensional list of parameters of type boolean, integer, float and string |
||
| 185 | * @return string Function signature |
||
| 186 | */ |
||
| 187 | public function make( string $name, array $params ) : string |
||
| 188 | { |
||
| 189 | return $name . '(' . substr( json_encode( $params ), 1, -1 ) . ')'; |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Negates the whole expression. |
||
| 195 | * |
||
| 196 | * @param \Aimeos\Base\Criteria\Expression\Iface $expr Expression object |
||
| 197 | * @return \Aimeos\Base\Criteria\Expression\Combine\Iface Combine expression object |
||
| 198 | */ |
||
| 199 | public function not( \Aimeos\Base\Criteria\Expression\Iface $expr ) : \Aimeos\Base\Criteria\Expression\Combine\Iface |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Combines the expression with an OR operator |
||
| 207 | * |
||
| 208 | * @param \Aimeos\Base\Criteria\Expression\Compare\Iface[] $list List of expression objects |
||
| 209 | * @return \Aimeos\Base\Criteria\Expression\Combine\Iface Combine expression object |
||
| 210 | */ |
||
| 211 | public function or( array $list ) : \Aimeos\Base\Criteria\Expression\Combine\Iface |
||
| 212 | { |
||
| 213 | return $this->combine( '||', $list ); |
||
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Sets the keys the data should be ordered by. |
||
| 219 | * |
||
| 220 | * |
||
| 221 | * Available sorting operators are: |
||
| 222 | * "product.label": sort ascending |
||
| 223 | * "-product.label": sort descending |
||
| 224 | * |
||
| 225 | * @param array|string $keys Name of the column or property that should be used for sorting |
||
| 226 | * @return \Aimeos\Base\Criteria\Iface Object instance for fluent interface |
||
| 227 | */ |
||
| 228 | public function order( $names ) : \Aimeos\Base\Criteria\Iface |
||
| 229 | { |
||
| 230 | $sort = []; |
||
| 231 | |||
| 232 | foreach( (array) $names as $name ) |
||
| 233 | { |
||
| 234 | $op = '+'; |
||
| 235 | $name = (string) $name; |
||
| 236 | |||
| 237 | if( strlen( $name ) && $name[0] === '-' ) { |
||
| 238 | $op = '-'; $name = substr( $name, 1 ); |
||
| 239 | } |
||
| 240 | |||
| 241 | $sort[] = $this->sort( $op, $name ); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $this->setSortations( $sort ); |
||
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Creates condition expressions from a multi-dimensional associative array. |
||
| 250 | * |
||
| 251 | * The simplest form of a valid associative array is a single comparison: |
||
| 252 | * $array = [ |
||
| 253 | * '==' => ['name' => 'value'], |
||
| 254 | * ]; |
||
| 255 | * |
||
| 256 | * Combining several conditions can look like: |
||
| 257 | * $array = [ |
||
| 258 | * '&&' => [ |
||
| 259 | * ['==' => ['name' => 'value']], |
||
| 260 | * ['==' => ['name2' => 'value2']], |
||
| 261 | * ], |
||
| 262 | * ]; |
||
| 263 | * |
||
| 264 | * Nested combine operators are also possible. |
||
| 265 | * |
||
| 266 | * @param array $array Multi-dimensional associative array containing the expression arrays |
||
| 267 | * @return \Aimeos\Base\Criteria\Expression\Iface|null Condition expressions (maybe nested) or null for none |
||
| 268 | * @throws \Aimeos\Base\Exception If given array is invalid |
||
| 269 | */ |
||
| 270 | public function parse( array $array ) : ?\Aimeos\Base\Criteria\Expression\Iface |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the list of translated colums |
||
| 292 | * |
||
| 293 | * @param array $columns List of objects implementing getName() method |
||
| 294 | * @param array $translations Associative list of item names that should be translated |
||
| 295 | * @param array $funcs Associative list of item names and functions modifying the conditions |
||
| 296 | * @return array List of translated columns |
||
| 297 | */ |
||
| 298 | public function translate( array $columns, array $translations = [], array $funcs = [] ) : array |
||
| 299 | { |
||
| 300 | $list = []; |
||
| 301 | |||
| 302 | foreach( $columns as $item ) |
||
| 303 | { |
||
| 304 | if( ( $value = $item->translate( $translations, $funcs ) ) !== null ) { |
||
| 305 | $list[] = $value; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | return $list; |
||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Creates a "combine" expression. |
||
| 315 | * |
||
| 316 | * @param string $operator One of the "combine" operators |
||
| 317 | * @param array $list List of arrays with "combine" or "compare" representations |
||
| 318 | * @return \Aimeos\Base\Criteria\Expression\Combine\Iface Combine expression object |
||
| 319 | * @throws \Aimeos\Base\Exception If operator is invalid |
||
| 320 | */ |
||
| 321 | protected function createCombineExpression( string $operator, array $list ) |
||
| 346 | } |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * Creates a "compare" expression. |
||
| 351 | * |
||
| 352 | * @param string $op One of the "compare" operators |
||
| 353 | * @param array $pair Associative list containing one name/value pair |
||
| 354 | * @return \Aimeos\Base\Criteria\Expression\Compare\Iface Compare expression object |
||
| 355 | * @throws \Aimeos\Base\Exception If no name/value pair is available |
||
| 356 | */ |
||
| 357 | protected function createCompareExpression( $op, array $pair ) |
||
| 364 | } |
||
| 365 | } |
||
| 366 |