Complex classes like Region 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 Region, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Region |
||
| 29 | { |
||
| 30 | |||
| 31 | use StaticCacheTrait; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * machine name of this region. e.g. 'left-sidebar' |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $_machineName = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Collection of blocks for this region. |
||
| 42 | * |
||
| 43 | * @var \Cake\Collection\Collection |
||
| 44 | */ |
||
| 45 | protected $_blocks = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Maximum number of blocks this region can holds. |
||
| 49 | * |
||
| 50 | * @var null|int |
||
| 51 | */ |
||
| 52 | protected $_blockLimit = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Information about the theme this region belongs to. |
||
| 56 | * |
||
| 57 | * @var \CMS\Core\Package\PluginPackage |
||
| 58 | */ |
||
| 59 | protected $_theme; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * View instance. |
||
| 63 | * |
||
| 64 | * @var \CMS\View\View |
||
| 65 | */ |
||
| 66 | protected $_View = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor. |
||
| 70 | * |
||
| 71 | * ### Valid options are: |
||
| 72 | * |
||
| 73 | * - `fixMissing`: When creating a region that is not defined by the theme, it |
||
| 74 | * will try to fix it by adding it to theme's regions if this option is set |
||
| 75 | * to TRUE. Defaults to NULL which automatically enables when `debug` is |
||
| 76 | * enabled. This option will not work when using QuickAppsCMS's core themes. |
||
| 77 | * (NOTE: This option will alter theme's `composer.json` file) |
||
| 78 | * |
||
| 79 | * - `theme`: Name of the theme this regions belongs to. Defaults to auto-detect. |
||
| 80 | * |
||
| 81 | * @param \CMS\View\View $view Instance of View class to use |
||
| 82 | * @param string $name Machine name of the region. e.g.: `left-sidebar` |
||
| 83 | * @param array $options Options given as an array |
||
| 84 | */ |
||
| 85 | public function __construct(View $view, $name, array $options = []) |
||
| 86 | { |
||
| 87 | $options += [ |
||
| 88 | 'fixMissing' => null, |
||
| 89 | 'theme' => $view->theme(), |
||
| 90 | ]; |
||
| 91 | $this->_machineName = Inflector::slug($name, '-'); |
||
|
|
|||
| 92 | $this->_View = $view; |
||
| 93 | $this->_theme = plugin($options['theme']); |
||
| 94 | |||
| 95 | if (isset($this->_theme->composer['extra']['regions'])) { |
||
| 96 | $validRegions = array_keys($this->_theme->composer['extra']['regions']); |
||
| 97 | $jsonPath = "{$this->_theme->path}/composer.json"; |
||
| 98 | $options['fixMissing'] = $options['fixMissing'] == null ? Configure::read('debug') : $options['fixMissing']; |
||
| 99 | if (!in_array($this->_machineName, $validRegions) && |
||
| 100 | $options['fixMissing'] && |
||
| 101 | is_writable($jsonPath) |
||
| 102 | ) { |
||
| 103 | $jsonArray = json_decode(file_get_contents($jsonPath), true); |
||
| 104 | if (is_array($jsonArray)) { |
||
| 105 | $humanName = Inflector::humanize(str_replace('-', '_', $this->_machineName)); |
||
| 106 | $jsonArray['extra']['regions'][$this->_machineName] = $humanName; |
||
| 107 | $encode = json_encode($jsonArray, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); |
||
| 108 | if ($encode) { |
||
| 109 | file_put_contents($jsonPath, $encode); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the name of this region. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function name() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns information of the theme this regions belongs to. |
||
| 128 | * |
||
| 129 | * ### Usage: |
||
| 130 | * |
||
| 131 | * ```php |
||
| 132 | * $theme = $this->region('left-sidebar')->theme(); |
||
| 133 | * ``` |
||
| 134 | * |
||
| 135 | * @return CMS\Core\Package\PluginPackage |
||
| 136 | */ |
||
| 137 | public function theme() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Gets or sets the block collection of this region. |
||
| 144 | * |
||
| 145 | * When passing a collection of blocks as first argument, all blocks in the |
||
| 146 | * collection will be homogenized, see homogenize() for details. |
||
| 147 | * |
||
| 148 | * @param \Cake\Collection\Collection $blocks Blocks collection if you want to |
||
| 149 | * overwrite current collection, leave empty to return current collection |
||
| 150 | * @return \Cake\Collection\Collection |
||
| 151 | * @see \Block\View\Region::homogenize() |
||
| 152 | */ |
||
| 153 | public function blocks(Collection $blocks = null) |
||
| 154 | { |
||
| 155 | if ($blocks) { |
||
| 156 | $this->_blocks = $blocks; |
||
| 157 | $this->homogenize(); |
||
| 158 | } elseif ($this->_blocks === null) { |
||
| 159 | $this->_prepareBlocks(); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this->_blocks; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Counts the number of blocks within this region. |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function count() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Limits the number of blocks in this region. |
||
| 177 | * |
||
| 178 | * Null means unlimited number. |
||
| 179 | * |
||
| 180 | * @param null|int $number Defaults to null |
||
| 181 | * @return \Block\View\Region |
||
| 182 | */ |
||
| 183 | public function blockLimit($number = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Merge blocks from another region. |
||
| 193 | * |
||
| 194 | * You can not merge regions with the same machine-name, new blocks are appended |
||
| 195 | * to this region. |
||
| 196 | * |
||
| 197 | * @param \Block\View\Region $region Region to merge with |
||
| 198 | * @param bool $homogenize Set to true to make sure all blocks in the |
||
| 199 | * collection are marked as they belongs to this region |
||
| 200 | * @return \Block\View\Region This region with $region's blocks appended |
||
| 201 | */ |
||
| 202 | public function merge(Region $region, $homogenize = true) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Makes sure that every block in this region is actually marked as it belongs |
||
| 220 | * to this region. |
||
| 221 | * |
||
| 222 | * Used when merging blocks from another region. |
||
| 223 | * |
||
| 224 | * @return \Block\View\Region This region with homogenized blocks |
||
| 225 | */ |
||
| 226 | public function homogenize() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Render all the blocks within this region. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function render() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Fetches all block entities that could be rendered within this region. |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | protected function _prepareBlocks() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Checks if the given block can be rendered. |
||
| 292 | * |
||
| 293 | * @param \Block\Model\Entity\Block $block Block entity |
||
| 294 | * @return bool True if can be rendered |
||
| 295 | */ |
||
| 296 | protected function _filterBlock(Block $block) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if a current URL matches any pattern in a set of patterns. |
||
| 343 | * |
||
| 344 | * @param string $patterns String containing a set of patterns separated by |
||
| 345 | * \n, \r or \r\n |
||
| 346 | * @return bool TRUE if the path matches a pattern, FALSE otherwise |
||
| 347 | */ |
||
| 348 | protected function _urlMatch($patterns) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Magic method for rendering this region. |
||
| 394 | * |
||
| 395 | * ```php |
||
| 396 | * echo $this->region('left-sidebar'); |
||
| 397 | * ``` |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function __toString() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns an array that can be used to describe the internal state of |
||
| 408 | * this object. |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | public function __debugInfo() |
||
| 422 | } |
||
| 423 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.