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:
Complex classes like ContentBlockHelper 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ContentBlockHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ContentBlockHelper |
||
| 23 | { |
||
| 24 | private static $chunksByKey = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Compiles content string by injecting chunks into content |
||
| 28 | * Preloads chunks which have preload = 1 |
||
| 29 | * |
||
| 30 | * @param string $content Original content with chunk calls |
||
| 31 | * @param string $content_key Key for caching compiled content version |
||
| 32 | * @param yii\caching\Dependency $dependency Cache dependency |
||
| 33 | * @return string Compiled content with injected chunks |
||
| 34 | */ |
||
| 35 | public static function compileContentString($content, $content_key, $dependency) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Finding chunk calls with regexp |
||
| 45 | * Iterate matches |
||
| 46 | * While iterating: |
||
| 47 | * Extracts single chunk data with sanitizeChunk() method |
||
| 48 | * Fetches chunk by key using fetchChunkByKey(), who returns chunk value by key from static array if exists, otherwise from db |
||
|
|
|||
| 49 | * Compiles single chunk using compileChunk() method |
||
| 50 | * Replaces single chunk call with compiled chunk data in the model content |
||
| 51 | * |
||
| 52 | * @param string $content_key Key for caching compiled content version |
||
| 53 | * @param yii\caching\Dependency $dependency Cache dependency |
||
| 54 | * @param bool $preprocess flag to separate rendering non cacheable chunks such as Form |
||
| 55 | * @param string $content |
||
| 56 | * @param string $chunk_key ContentBlock key string to prevent endless recursion |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | private static function processData($content, $content_key, $dependency, $chunk_key = '', $preprocess = true) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $chunkData |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | private static function replaceForms($chunkData) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * renders url according to given data |
||
| 142 | * @param $chunkData |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | private static function renderUrl($chunkData) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Extracts chunk data from chunk call |
||
| 175 | * uses regexp to extract param data from placeholder |
||
| 176 | * [[$chunk <paramName>='<escapedValue>'|'<escapedDefault>' <paramName>=<unescapedValue>|<unescapedDefault>]] |
||
| 177 | * iterate matches. |
||
| 178 | * While iterating converts escapedValue and escapedDefault into string, unescapedValue and unescapedDefault - into float |
||
| 179 | * Returns chunk data array like: |
||
| 180 | * [ |
||
| 181 | * 'key' => 'chunkKey', |
||
| 182 | * 'firstParam'=> 'string value', |
||
| 183 | * 'firstParam-default'=> 'default string value', |
||
| 184 | * 'secondParam'=> float value, |
||
| 185 | * 'secondParam-default'=> default float value, |
||
| 186 | * ] |
||
| 187 | * |
||
| 188 | * @param string $rawChunk |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | private static function sanitizeChunk($rawChunk) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Compiles single chunk |
||
| 218 | * uses regexp to find placeholders and extract it's data from chunk value field |
||
| 219 | * [[<token><paramName>:<format><params>]] |
||
| 220 | * token switch is for future functionality increase |
||
| 221 | * now method only recognizes + token and replaces following param with according $arguments array data |
||
| 222 | * applies formatter according previously defined param values type if needed |
||
| 223 | * if param name from placeholder was not found in arguments array, placeholder in the compiled chunk will be replaced with empty string |
||
| 224 | * returns compiled chunk |
||
| 225 | * |
||
| 226 | * @param string $content_key Key for caching compiled content version |
||
| 227 | * @param yii\caching\Dependency $dependency Cache dependency |
||
| 228 | * @param string $chunk ContentBlock instance |
||
| 229 | * @param array $arguments Arguments for this chunk from original content |
||
| 230 | * @param string $key ContentBlock key string to prevent endless recursion |
||
| 231 | * @return string Result string ready for replacing |
||
| 232 | */ |
||
| 233 | private static function compileChunk($chunk, $arguments, $key, $content_key, $dependency) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Find formatter declarations in chunk placeholders. if find trying to apply |
||
| 264 | * yii\i18n\Formatter formats see yii\i18n\Formatter for details |
||
| 265 | * |
||
| 266 | * @param string $value single placeholder declaration from chunk |
||
| 267 | * @param string $format |
||
| 268 | * @param array $params |
||
| 269 | * @return string|array |
||
| 270 | */ |
||
| 271 | private static function applyFormatter($value, $format, $params) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Fetches single chunk by key from static var |
||
| 287 | * if is no there - get it from db and push to static array |
||
| 288 | * |
||
| 289 | * @param $key string Chunk key field |
||
| 290 | * @return string Chunk value field |
||
| 291 | */ |
||
| 292 | private static function fetchChunkByKey($key) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * preloads chunks with option preload = 1 |
||
| 313 | * and push it to static array |
||
| 314 | * |
||
| 315 | * @return array|void |
||
| 316 | */ |
||
| 317 | private static function preloadChunks() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * renders chunks in template files |
||
| 338 | * @param string $key ContentBlock key |
||
| 339 | * @param array $params . Array of params to be replaced while render |
||
| 340 | * @param yii\base\Model $model . Caller model instance to use in caching |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | public static function getChunk($key, $params = [], yii\base\Model $model = null) |
||
| 362 | } |
||
| 363 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.