Complex classes like YmlCatalog 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 YmlCatalog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class YmlCatalog |
||
26 | { |
||
27 | /** |
||
28 | * @var BaseFileStream |
||
29 | */ |
||
30 | protected $handle; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $shopClass; |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $currencyClass; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $categoryClass; |
||
43 | /** |
||
44 | * @var null|string |
||
45 | */ |
||
46 | protected $localDeliveryCostClass; |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $offerClass; |
||
51 | /** |
||
52 | * @var null|string |
||
53 | */ |
||
54 | protected $date; |
||
55 | |||
56 | /** |
||
57 | * @var null|callable |
||
58 | */ |
||
59 | protected $onValidationError; |
||
60 | |||
61 | /** |
||
62 | * @var null|string |
||
63 | */ |
||
64 | protected $customOfferClass; |
||
65 | |||
66 | /** |
||
67 | * @var null|string |
||
68 | */ |
||
69 | protected $deliveryOptionClass; |
||
70 | |||
71 | 9 | /** |
|
72 | * @var ActiveQuery |
||
73 | */ |
||
74 | 2 | private $query = null; |
|
75 | |||
76 | /** |
||
77 | * @var BatchQueryResult |
||
78 | */ |
||
79 | private $queryIterator = null; |
||
80 | |||
81 | /** |
||
82 | 9 | * @var ActiveDataProvider |
|
83 | 9 | */ |
|
84 | 9 | private $dataProvider = null; |
|
85 | 9 | ||
86 | 9 | /** |
|
87 | 9 | * @var Pagination |
|
88 | 9 | */ |
|
89 | 9 | private $pagination = null; |
|
90 | 9 | ||
91 | 9 | /** |
|
92 | * @var int |
||
93 | */ |
||
94 | private $paginationPage = 0; |
||
95 | |||
96 | 9 | /** |
|
97 | * @param BaseFileStream $handle |
||
98 | 9 | * @param string $shopClass class name |
|
99 | * @param string $currencyClass class name |
||
100 | 9 | * @param string $categoryClass class name |
|
101 | 9 | * @param string $localDeliveryCostClass class name |
|
102 | 9 | * @param array $offerClasses |
|
103 | 9 | * @param null|string $date |
|
104 | 6 | * @param null|callable $onValidationError |
|
105 | * @param null|string $customOfferClass |
||
106 | 9 | */ |
|
107 | 9 | public function __construct( |
|
130 | 9 | ||
131 | /** |
||
132 | 9 | * @throws Exception |
|
133 | 3 | */ |
|
134 | 2 | public function generate() |
|
169 | 6 | ||
170 | /** |
||
171 | 9 | * @return null|string |
|
172 | 9 | */ |
|
173 | 6 | protected function getDate() |
|
183 | 3 | ||
184 | /** |
||
185 | 6 | * @param string $string |
|
186 | 9 | * @throws \Exception |
|
187 | */ |
||
188 | 9 | protected function write($string) |
|
192 | |||
193 | /** |
||
194 | * @param string $string tag name |
||
195 | 9 | */ |
|
196 | protected function writeTag($string) |
||
200 | 9 | ||
201 | /** |
||
202 | * @param BaseModel $model |
||
203 | 9 | * @param $valuesModel |
|
204 | * @throws Exception |
||
205 | 9 | */ |
|
206 | 9 | protected function writeModel(BaseModel $model, $valuesModel) |
|
223 | 6 | ||
224 | 9 | /** |
|
225 | 9 | * @param string|array $modelClass class name or yii configuration array. You can also set params: |
|
226 | 3 | * `findParams`: array of additional find params; |
|
227 | 3 | * `query`: ActiveQuery object to generate yml use already created object; |
|
228 | 6 | * `dataProvider`: ActiveDataProvider or true to generate yml with pagination; |
|
229 | 6 | */ |
|
230 | 4 | protected function writeEachModel($modelClass) |
|
303 | |||
304 | /** |
||
305 | * @return Model[]|false |
||
306 | */ |
||
307 | protected function getModels() |
||
329 | |||
330 | /** |
||
331 | * @param $modelClass |
||
332 | * @return Category|Currency|SimpleOffer |
||
333 | * @throws Exception |
||
334 | */ |
||
335 | protected function getNewModel($modelClass) |
||
353 | |||
354 | /** |
||
355 | * Performs PHP memory garbage collection. |
||
356 | */ |
||
357 | protected function gc() |
||
364 | } |
||
365 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.