Complex classes like CacheController 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 CacheController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class CacheController extends Controller |
||
47 | { |
||
48 | /** |
||
49 | * Lists the caches that can be flushed. |
||
50 | */ |
||
51 | public function actionIndex() |
||
52 | { |
||
53 | $caches = $this->findCaches(); |
||
54 | |||
55 | if (!empty($caches)) { |
||
56 | $this->notifyCachesCanBeFlushed($caches); |
||
57 | } else { |
||
58 | $this->notifyNoCachesFound(); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Flushes given cache components. |
||
64 | * For example, |
||
65 | * |
||
66 | * ``` |
||
67 | * # flushes caches specified by their id: "first", "second", "third" |
||
68 | * yii cache/flush first second third |
||
69 | * ``` |
||
70 | */ |
||
71 | public function actionFlush() |
||
72 | { |
||
73 | $cachesInput = func_get_args(); |
||
74 | |||
75 | if (empty($cachesInput)) { |
||
76 | throw new Exception('You should specify cache components names'); |
||
77 | } |
||
78 | |||
79 | $caches = $this->findCaches($cachesInput); |
||
80 | $cachesInfo = []; |
||
81 | |||
82 | $foundCaches = array_keys($caches); |
||
83 | $notFoundCaches = array_diff($cachesInput, array_keys($caches)); |
||
84 | |||
85 | if ($notFoundCaches) { |
||
|
|||
86 | $this->notifyNotFoundCaches($notFoundCaches); |
||
87 | } |
||
88 | |||
89 | if (!$foundCaches) { |
||
90 | $this->notifyNoCachesFound(); |
||
91 | return static::EXIT_CODE_NORMAL; |
||
92 | } |
||
93 | |||
94 | if (!$this->confirmFlush($foundCaches)) { |
||
95 | return static::EXIT_CODE_NORMAL; |
||
96 | } |
||
97 | |||
98 | foreach ($caches as $name => $class) { |
||
99 | $cachesInfo[] = [ |
||
100 | 'name' => $name, |
||
101 | 'class' => $class, |
||
102 | 'is_flushed' => $this->canBeFlushed($class) ? Yii::$app->get($name)->flush() : false, |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | $this->notifyFlushed($cachesInfo); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Flushes all caches registered in the system. |
||
111 | */ |
||
112 | public function actionFlushAll() |
||
113 | { |
||
114 | $caches = $this->findCaches(); |
||
115 | $cachesInfo = []; |
||
116 | |||
117 | if (empty($caches)) { |
||
118 | $this->notifyNoCachesFound(); |
||
119 | return static::EXIT_CODE_NORMAL; |
||
120 | } |
||
121 | |||
122 | foreach ($caches as $name => $class) { |
||
123 | $cachesInfo[] = [ |
||
124 | 'name' => $name, |
||
125 | 'class' => $class, |
||
126 | 'is_flushed' => $this->canBeFlushed($class) ? Yii::$app->get($name)->flush() : false, |
||
127 | ]; |
||
128 | } |
||
129 | |||
130 | $this->notifyFlushed($cachesInfo); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Clears DB schema cache for a given connection component. |
||
135 | * |
||
136 | * ``` |
||
137 | * # clears cache schema specified by component id: "db" |
||
138 | * yii cache/flush-schema db |
||
139 | * ``` |
||
140 | * |
||
141 | * @param string $db id connection component |
||
142 | * @return int exit code |
||
143 | * @throws Exception |
||
144 | * @throws \yii\base\InvalidConfigException |
||
145 | * |
||
146 | * @since 2.0.1 |
||
147 | */ |
||
148 | public function actionFlushSchema($db = 'db') |
||
171 | |||
172 | /** |
||
173 | * Notifies user that given caches are found and can be flushed. |
||
174 | * @param array $caches array of cache component classes |
||
175 | */ |
||
176 | private function notifyCachesCanBeFlushed($caches) |
||
190 | |||
191 | /** |
||
192 | * Notifies user that there was not found any cache in the system. |
||
193 | */ |
||
194 | private function notifyNoCachesFound() |
||
198 | |||
199 | /** |
||
200 | * Notifies user that given cache components were not found in the system. |
||
201 | * @param array $cachesNames |
||
202 | */ |
||
203 | private function notifyNotFoundCaches($cachesNames) |
||
213 | |||
214 | /** |
||
215 | * @param array $caches |
||
216 | */ |
||
217 | private function notifyFlushed($caches) |
||
233 | |||
234 | /** |
||
235 | * Prompts user with confirmation if caches should be flushed. |
||
236 | * @param array $cachesNames |
||
237 | * @return bool |
||
238 | */ |
||
239 | private function confirmFlush($cachesNames) |
||
249 | |||
250 | /** |
||
251 | * Returns array of caches in the system, keys are cache components names, values are class names. |
||
252 | * @param array $cachesNames caches to be found |
||
253 | * @return array |
||
254 | */ |
||
255 | private function findCaches(array $cachesNames = []) |
||
277 | |||
278 | /** |
||
279 | * Checks if given class is a Cache class. |
||
280 | * @param string $className class name. |
||
281 | * @return bool |
||
282 | */ |
||
283 | private function isCacheClass($className) |
||
287 | |||
288 | /** |
||
289 | * Checks if cache of a certain class can be flushed |
||
290 | * @param string $className class name. |
||
291 | * @return bool |
||
292 | */ |
||
293 | private function canBeFlushed($className) |
||
297 | } |
||
298 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.