Total Complexity | 41 |
Total Lines | 235 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Traits 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 Traits, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait Traits |
||
21 | { |
||
22 | private array $exprPlugins = []; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Returns the left side of the compare expression. |
||
27 | * |
||
28 | * @return string Name of variable or column that should be compared |
||
29 | */ |
||
30 | abstract public function getName() : string; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Translates the sort key into the name required by the storage |
||
35 | * |
||
36 | * @param array $translations Associative list of variable or column names that should be translated |
||
37 | * @param array $funcs Associative list of item names and functions modifying the conditions |
||
38 | * @return string Translated name (with replaced parameters if the name is an expression function) |
||
39 | */ |
||
40 | public function translate( array $translations, array $funcs = [] ) : ?string |
||
41 | { |
||
42 | $name = $this->getName(); |
||
43 | return $this->translateName( $name, $translations, $funcs ); |
||
44 | } |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Checks if the given string is an expression function and returns the parameters. |
||
49 | * The parameters will be cut off the function name and will be added to |
||
50 | * the given parameter array |
||
51 | * |
||
52 | * @param string $name Function string to check, will be cut to <function>() (without parameter) |
||
53 | * @param array $params Array that will contain the list of parameters afterwards |
||
54 | * @return bool True if string is an expression function, false if not |
||
55 | */ |
||
56 | protected function isFunction( string &$name, array &$params ) : bool |
||
57 | { |
||
58 | $len = strlen( $name ); |
||
59 | if( $len === 0 || $name[$len - 1] !== ')' ) { |
||
60 | return false; |
||
61 | } |
||
62 | |||
63 | if( ( $pos = strpos( $name, '(' ) ) === false ) { |
||
64 | throw new \Aimeos\Base\Exception( 'Missing opening bracket for function syntax' ); |
||
65 | } |
||
66 | |||
67 | if( ( $paramstr = substr( $name, $pos, $len - $pos ) ) === false ) { |
||
68 | throw new \Aimeos\Base\Exception( 'Unable to extract function parameter' ); |
||
69 | } |
||
70 | |||
71 | if( ( $namestr = substr( $name, 0, $pos ) ) === false ) { |
||
72 | throw new \Aimeos\Base\Exception( 'Unable to extract function name' ); |
||
73 | } |
||
74 | |||
75 | $params = json_decode( str_replace( ['(', ')'], ['[', ']'], $paramstr ), true ); |
||
76 | $name = $namestr . '()'; |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Replaces the parameters in nested arrays |
||
84 | * |
||
85 | * @param array $list Multi-dimensional associative array of values including positional parameter, e.g. "$1" |
||
86 | * @param string[] $find List of strings to search for, e.g. ['$1', '$2'] |
||
87 | * @param string[] $replace List of strings to replace by, e.g. ['val1', 'val2'] |
||
88 | * @return array Multi-dimensional associative array with parameters replaced |
||
89 | */ |
||
90 | protected function replaceParameter( array $list, array $find, array $replace ) : array |
||
91 | { |
||
92 | foreach( $list as $key => $value ) |
||
93 | { |
||
94 | if( is_array( $value ) ) { |
||
95 | $list[$key] = $this->replaceParameter( $value, $find, $replace ); |
||
96 | } else { |
||
97 | $list[$key] = str_replace( $find, $replace, $value ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return $list; |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Translates an expression string and replaces the parameter if it's an expression function. |
||
107 | * |
||
108 | * @param string $name Expresion string or function |
||
109 | * @param array $translations Associative list of names and their translations (may include parameter if a name is an expression function) |
||
110 | * @param array $funcs Associative list of item names and functions modifying the conditions |
||
111 | * @return mixed Translated name (with replaced parameters if the name is an expression function) |
||
112 | */ |
||
113 | protected function translateName( string &$name, array $translations = [], array $funcs = [] ) |
||
161 | } |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Translates a value to another one by a plugin if available. |
||
166 | * |
||
167 | * @param string $name Name of variable or column that should be translated |
||
168 | * @param mixed $value Original value |
||
169 | * @param mixed $type Value type |
||
170 | * @return mixed Translated value |
||
171 | */ |
||
172 | protected function translateValue( string $name, $value, $type ) |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Sets the new plugins for translating values. |
||
181 | * |
||
182 | * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of names as keys and plugin items as values |
||
183 | */ |
||
184 | protected function setPlugins( array $plugins ) |
||
185 | { |
||
186 | $this->exprPlugins = \Aimeos\Base\Criteria\Base::implements( \Aimeos\Base\Criteria\Plugin\Iface::class, $plugins ); |
||
187 | } |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Returns the plugin for translating values. |
||
192 | * |
||
193 | * @param string $name Column name |
||
194 | * @return \Aimeos\Base\Criteria\Plugin\Iface|null Plugin item or NULL if not available |
||
195 | */ |
||
196 | protected function getPlugin( string $name ) : ?\Aimeos\Base\Criteria\Plugin\Iface |
||
199 | } |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Escapes the value so it can be inserted into a SQL statement |
||
204 | * |
||
205 | * @param string $operator Operator used for the expression |
||
206 | * @param string $type Type constant |
||
207 | * @param mixed $value Value that the variable or column should be compared to |
||
208 | * @return double|string|int Escaped value |
||
209 | */ |
||
210 | protected function escape( string $operator, string $type, $value ) |
||
244 | } |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @param string &$item Reference to parameter value (will be updated if necessary) |
||
249 | * |
||
250 | * @param mixed $item Parameter value |
||
251 | * @return string Internal parameter type |
||
252 | * @throws \Aimeos\Base\Exception If an error occurs |
||
253 | */ |
||
254 | abstract protected function getParamType( &$item ) : string; |
||
255 | } |
||
256 |