Complex classes like DataContext 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 DataContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class DataContext extends BehatContext implements KernelAwareInterface |
||
36 | { |
||
37 | /** |
||
38 | * |
||
39 | * Kernel. |
||
40 | * |
||
41 | * @var KernelInterface |
||
42 | */ |
||
43 | protected $kernel; |
||
44 | |||
45 | public function __construct() |
||
49 | |||
50 | /** |
||
51 | * |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function setKernel(KernelInterface $kernel) |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | * Get entity manager. |
||
62 | * |
||
63 | * @return EntityManager |
||
64 | */ |
||
65 | public function getEntityManager() |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * Returns Container instance. |
||
73 | * |
||
74 | * @return ContainerInterface |
||
|
|||
75 | */ |
||
76 | protected function getContainer() |
||
80 | |||
81 | /** |
||
82 | * |
||
83 | * Get service by id. |
||
84 | * |
||
85 | * @param string $serviceName |
||
86 | * |
||
87 | * @return object |
||
88 | */ |
||
89 | protected function getService($serviceName) |
||
93 | |||
94 | protected $users = array(); |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | * @Given /^there are following users defined:$/ |
||
99 | */ |
||
100 | public function thereAreFollowingUsersDefined(TableNode $table) |
||
114 | |||
115 | public function thereIsUser($username, $email, $password, $role = 'ROLE_USER', $enabled = true) |
||
132 | |||
133 | protected $forums = array(); |
||
134 | |||
135 | /** |
||
136 | * |
||
137 | * @Given /^there are following forums defined:$/ |
||
138 | */ |
||
139 | public function thereAreFollowingForumsDefined(TableNode $table) |
||
149 | |||
150 | public function thereIsForum($name) |
||
160 | |||
161 | protected $categories = array(); |
||
162 | |||
163 | /** |
||
164 | * |
||
165 | * @Given /^there are following categories defined:$/ |
||
166 | */ |
||
167 | public function thereAreFollowingCategoriesDefined(TableNode $table) |
||
179 | |||
180 | public function thereIsCategory($name, $order, $forumName = null) |
||
197 | |||
198 | protected $boards = array(); |
||
199 | |||
200 | /** |
||
201 | * |
||
202 | * @Given /^there are following boards defined:$/ |
||
203 | */ |
||
204 | public function thereAreFollowingBoardsDefined(TableNode $table) |
||
217 | |||
218 | public function thereIsBoard($name, $description, $order, $categoryName = null) |
||
236 | |||
237 | protected $topics = array(); |
||
238 | |||
239 | /** |
||
240 | * |
||
241 | * @Given /^there are following topics defined:$/ |
||
242 | */ |
||
243 | public function thereAreFollowingTopicsDefined(TableNode $table) |
||
257 | |||
258 | public function thereIsTopic($title, $body, $boardName, $userEmail, $subscribed = false) |
||
304 | |||
305 | /** |
||
306 | * |
||
307 | * @var \CCDNForum\ForumBundle\Model\FrontModel\ForumModel $forumModel |
||
308 | */ |
||
309 | private $forumModel; |
||
310 | |||
311 | /** |
||
312 | * |
||
313 | * @var \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel $categoryModel |
||
314 | */ |
||
315 | private $categoryModel; |
||
316 | |||
317 | /** |
||
318 | * |
||
319 | * @var \CCDNForum\ForumBundle\Model\FrontModel\BoardModel $boardModel |
||
320 | */ |
||
321 | private $boardModel; |
||
322 | |||
323 | /** |
||
324 | * |
||
325 | * @var \CCDNForum\ForumBundle\Model\FrontModel\TopicModel $topicModel |
||
326 | */ |
||
327 | private $topicModel; |
||
328 | |||
329 | /** |
||
330 | * |
||
331 | * @var \CCDNForum\ForumBundle\Model\FrontModel\PostModel $postModel |
||
332 | */ |
||
333 | private $postModel; |
||
334 | |||
335 | /** |
||
336 | * |
||
337 | * @var \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel $registryModel |
||
338 | */ |
||
339 | private $registryModel; |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | * @var \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel $subscriptionModel |
||
344 | */ |
||
345 | private $subscriptionModel; |
||
346 | |||
347 | /** |
||
348 | * |
||
349 | * @access protected |
||
350 | * @return \CCDNForum\ForumBundle\Model\FrontModel\ForumModel |
||
351 | */ |
||
352 | protected function getForumModel() |
||
360 | |||
361 | /** |
||
362 | * |
||
363 | * @access protected |
||
364 | * @return \CCDNForum\ForumBundle\Model\FrontModel\CategoryModel |
||
365 | */ |
||
366 | protected function getCategoryModel() |
||
374 | |||
375 | /** |
||
376 | * |
||
377 | * @access protected |
||
378 | * @return \CCDNForum\ForumBundle\Model\FrontModel\BoardModel |
||
379 | */ |
||
380 | protected function getBoardModel() |
||
388 | |||
389 | /** |
||
390 | * |
||
391 | * @access protected |
||
392 | * @return \CCDNForum\ForumBundle\Model\FrontModel\TopicModel |
||
393 | */ |
||
394 | protected function getTopicModel() |
||
402 | |||
403 | /** |
||
404 | * |
||
405 | * @access protected |
||
406 | * @return \CCDNForum\ForumBundle\Model\FrontModel\PostModel |
||
407 | */ |
||
408 | protected function getPostModel() |
||
416 | |||
417 | /** |
||
418 | * |
||
419 | * @access protected |
||
420 | * @return \CCDNForum\ForumBundle\Model\FrontModel\RegistryModel |
||
421 | */ |
||
422 | protected function getRegistryModel() |
||
430 | |||
431 | /** |
||
432 | * |
||
433 | * @access protected |
||
434 | * @return \CCDNForum\ForumBundle\Model\FrontModel\SubscriptionModel |
||
435 | */ |
||
436 | protected function getSubscriptionModel() |
||
444 | } |
||
445 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.