Complex classes like PageCache 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 PageCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class PageCache extends ActionFilter |
||
53 | { |
||
54 | /** |
||
55 | * @var bool whether the content being cached should be differentiated according to the route. |
||
56 | * A route consists of the requested controller ID and action ID. Defaults to `true`. |
||
57 | */ |
||
58 | public $varyByRoute = true; |
||
59 | /** |
||
60 | * @var Cache|array|string the cache object or the application component ID of the cache object. |
||
61 | * After the PageCache object is created, if you want to change this property, |
||
62 | * you should only assign it with a cache object. |
||
63 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
64 | */ |
||
65 | public $cache = 'cache'; |
||
66 | /** |
||
67 | * @var int number of seconds that the data can remain valid in cache. |
||
68 | * Use `0` to indicate that the cached data will never expire. |
||
69 | */ |
||
70 | public $duration = 60; |
||
71 | /** |
||
72 | * @var array|Dependency the dependency that the cached content depends on. |
||
73 | * This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||
74 | * For example, |
||
75 | * |
||
76 | * ```php |
||
77 | * [ |
||
78 | * 'class' => 'yii\caching\DbDependency', |
||
79 | * 'sql' => 'SELECT MAX(updated_at) FROM post', |
||
80 | * ] |
||
81 | * ``` |
||
82 | * |
||
83 | * would make the output cache depend on the last modified time of all posts. |
||
84 | * If any post has its modification time changed, the cached content would be invalidated. |
||
85 | * |
||
86 | * If [[cacheCookies]] or [[cacheHeaders]] is enabled, then [[\yii\caching\Dependency::reusable]] should be enabled as well to save performance. |
||
87 | * This is because the cookies and headers are currently stored separately from the actual page content, causing the dependency to be evaluated twice. |
||
88 | */ |
||
89 | public $dependency; |
||
90 | /** |
||
91 | * @var array list of factors that would cause the variation of the content being cached. |
||
92 | * Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||
93 | * The following variation setting will cause the content to be cached in different versions |
||
94 | * according to the current application language: |
||
95 | * |
||
96 | * ```php |
||
97 | * [ |
||
98 | * Yii::$app->language, |
||
99 | * ] |
||
100 | * ``` |
||
101 | */ |
||
102 | public $variations; |
||
103 | /** |
||
104 | * @var bool whether to enable the page cache. You may use this property to turn on and off |
||
105 | * the page cache according to specific setting (e.g. enable page cache only for GET requests). |
||
106 | */ |
||
107 | public $enabled = true; |
||
108 | /** |
||
109 | * @var \yii\base\View the view component to use for caching. If not set, the default application view component |
||
110 | * [[\yii\web\Application::view]] will be used. |
||
111 | */ |
||
112 | public $view; |
||
113 | /** |
||
114 | * @var bool|array a boolean value indicating whether to cache all cookies, or an array of |
||
115 | * cookie names indicating which cookies can be cached. Be very careful with caching cookies, because |
||
116 | * it may leak sensitive or private data stored in cookies to unwanted users. |
||
117 | * @since 2.0.4 |
||
118 | */ |
||
119 | public $cacheCookies = false; |
||
120 | /** |
||
121 | * @var bool|array a boolean value indicating whether to cache all HTTP headers, or an array of |
||
122 | * HTTP header names (case-insensitive) indicating which HTTP headers can be cached. |
||
123 | * Note if your HTTP headers contain sensitive information, you should white-list which headers can be cached. |
||
124 | * @since 2.0.4 |
||
125 | */ |
||
126 | public $cacheHeaders = true; |
||
127 | /** |
||
128 | * @var array a list of placeholders for embedding dynamic contents. This property |
||
129 | * is used internally to implement the content caching feature. Do not modify it. |
||
130 | * @internal |
||
131 | * @since 2.0.11 |
||
132 | */ |
||
133 | public $dynamicPlaceholders; |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 14 | public function init() |
|
146 | |||
147 | /** |
||
148 | * This method is invoked right before an action is to be executed (after all possible filters.) |
||
149 | * You may override this method to do last-minute preparation for the action. |
||
150 | * @param Action $action the action to be executed. |
||
151 | * @return bool whether the action should continue to be executed. |
||
152 | */ |
||
153 | 13 | public function beforeAction($action) |
|
180 | |||
181 | /** |
||
182 | * This method is invoked right before the response caching is to be started. |
||
183 | * You may override this method to cancel caching by returning `false` or store an additional data |
||
184 | * in a cache entry by returning an array instead of `true`. |
||
185 | * @return bool|array whether to cache or not, return an array instead of `true` to store an additional data. |
||
186 | * @since 2.0.11 |
||
187 | */ |
||
188 | 12 | public function beforeCacheResponse() |
|
192 | |||
193 | /** |
||
194 | * This method is invoked right after the response restoring is finished (but before the response is sent). |
||
195 | * You may override this method to do last-minute preparation before the response is sent. |
||
196 | * @param array|null $data an array of an additional data stored in a cache entry or `null`. |
||
197 | * @since 2.0.11 |
||
198 | */ |
||
199 | 11 | public function afterRestoreResponse($data) |
|
202 | |||
203 | /** |
||
204 | * Restores response properties from the given data. |
||
205 | * @param Response $response the response to be restored. |
||
206 | * @param array $data the response property data. |
||
207 | * @since 2.0.3 |
||
208 | */ |
||
209 | 11 | protected function restoreResponse($response, $data) |
|
230 | |||
231 | /** |
||
232 | * Caches response properties. |
||
233 | * @since 2.0.3 |
||
234 | */ |
||
235 | 12 | public function cacheResponse() |
|
270 | |||
271 | /** |
||
272 | * Inserts (or filters/ignores according to config) response headers/cookies into a cache data array. |
||
273 | * @param Response $response the response. |
||
274 | * @param string $collectionName currently it's `headers` or `cookies`. |
||
275 | * @param array $data the cache data. |
||
276 | */ |
||
277 | 12 | private function insertResponseCollectionIntoData(Response $response, $collectionName, array &$data) |
|
299 | |||
300 | /** |
||
301 | * Replaces placeholders in content by results of evaluated dynamic statements. |
||
302 | * @param string $content content to be parsed. |
||
303 | * @param array $placeholders placeholders and their values. |
||
304 | * @return string final content. |
||
305 | * @since 2.0.11 |
||
306 | */ |
||
307 | 12 | protected function updateDynamicContent($content, $placeholders) |
|
315 | |||
316 | /** |
||
317 | * @return array the key used to cache response properties. |
||
318 | * @since 2.0.3 |
||
319 | */ |
||
320 | 12 | protected function calculateCacheKey() |
|
333 | } |
||
334 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.