Complex classes like ResourceController 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 ResourceController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class ResourceController extends Controller |
||
34 | { |
||
35 | /** |
||
36 | * @var MetadataInterface |
||
37 | */ |
||
38 | protected $metadata; |
||
39 | |||
40 | /** |
||
41 | * @var RequestConfigurationFactoryInterface |
||
42 | */ |
||
43 | protected $requestConfigurationFactory; |
||
|
|||
44 | |||
45 | /** |
||
46 | * @var ViewHandlerInterface |
||
47 | */ |
||
48 | protected $viewHandler; |
||
49 | |||
50 | /** |
||
51 | * @var RepositoryInterface |
||
52 | */ |
||
53 | protected $repository; |
||
54 | |||
55 | /** |
||
56 | * @var FactoryInterface |
||
57 | */ |
||
58 | protected $factory; |
||
59 | |||
60 | /** |
||
61 | * @var NewResourceFactoryInterface |
||
62 | */ |
||
63 | protected $newResourceFactory; |
||
64 | |||
65 | /** |
||
66 | * @var ObjectManager |
||
67 | */ |
||
68 | protected $manager; |
||
69 | |||
70 | /** |
||
71 | * @var SingleResourceProviderInterface |
||
72 | */ |
||
73 | protected $singleResourceProvider; |
||
74 | |||
75 | /** |
||
76 | * @var ResourcesCollectionProviderInterface |
||
77 | */ |
||
78 | protected $resourcesCollectionProvider; |
||
79 | |||
80 | /** |
||
81 | * @var ResourceFormFactoryInterface |
||
82 | */ |
||
83 | protected $resourceFormFactory; |
||
84 | |||
85 | /** |
||
86 | * @var RedirectHandlerInterface |
||
87 | */ |
||
88 | protected $redirectHandler; |
||
89 | |||
90 | /** |
||
91 | * @var FlashHelperInterface |
||
92 | */ |
||
93 | protected $flashHelper; |
||
94 | |||
95 | /** |
||
96 | * @var AuthorizationCheckerInterface |
||
97 | */ |
||
98 | protected $authorizationChecker; |
||
99 | |||
100 | /** |
||
101 | * @var EventDispatcherInterface |
||
102 | */ |
||
103 | protected $eventDispatcher; |
||
104 | |||
105 | /** |
||
106 | * @var StateMachineInterface |
||
107 | */ |
||
108 | protected $stateMachine; |
||
109 | |||
110 | /** |
||
111 | * @param MetadataInterface $metadata |
||
112 | * @param RequestConfigurationFactoryInterface $requestConfigurationFactory |
||
113 | * @param ViewHandlerInterface $viewHandler |
||
114 | * @param RepositoryInterface $repository |
||
115 | * @param FactoryInterface $factory |
||
116 | * @param NewResourceFactoryInterface $newResourceFactory |
||
117 | * @param ObjectManager $manager |
||
118 | * @param SingleResourceProviderInterface $singleResourceProvider |
||
119 | * @param ResourcesCollectionProviderInterface $resourcesFinder |
||
120 | * @param ResourceFormFactoryInterface $resourceFormFactory |
||
121 | * @param RedirectHandlerInterface $redirectHandler |
||
122 | * @param FlashHelperInterface $flashHelper |
||
123 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
124 | * @param EventDispatcherInterface $eventDispatcher |
||
125 | * @param StateMachineInterface $stateMachine |
||
126 | */ |
||
127 | public function __construct( |
||
128 | MetadataInterface $metadata, |
||
129 | RequestConfigurationFactoryInterface $requestConfigurationFactory, |
||
130 | ViewHandlerInterface $viewHandler, |
||
131 | RepositoryInterface $repository, |
||
132 | FactoryInterface $factory, |
||
133 | NewResourceFactoryInterface $newResourceFactory, |
||
134 | ObjectManager $manager, |
||
135 | SingleResourceProviderInterface $singleResourceProvider, |
||
136 | ResourcesCollectionProviderInterface $resourcesFinder, |
||
137 | ResourceFormFactoryInterface $resourceFormFactory, |
||
138 | RedirectHandlerInterface $redirectHandler, |
||
139 | FlashHelperInterface $flashHelper, |
||
140 | AuthorizationCheckerInterface $authorizationChecker, |
||
141 | EventDispatcherInterface $eventDispatcher, |
||
142 | StateMachineInterface $stateMachine |
||
143 | ) { |
||
144 | $this->metadata = $metadata; |
||
145 | $this->requestConfigurationFactory = $requestConfigurationFactory; |
||
146 | $this->viewHandler = $viewHandler; |
||
147 | $this->repository = $repository; |
||
148 | $this->factory = $factory; |
||
149 | $this->newResourceFactory = $newResourceFactory; |
||
150 | $this->manager = $manager; |
||
151 | $this->singleResourceProvider = $singleResourceProvider; |
||
152 | $this->resourcesCollectionProvider = $resourcesFinder; |
||
153 | $this->resourceFormFactory = $resourceFormFactory; |
||
154 | $this->redirectHandler = $redirectHandler; |
||
155 | $this->flashHelper = $flashHelper; |
||
156 | $this->authorizationChecker = $authorizationChecker; |
||
157 | $this->eventDispatcher = $eventDispatcher; |
||
158 | $this->stateMachine = $stateMachine; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param Request $request |
||
163 | * |
||
164 | * @return Response |
||
165 | */ |
||
166 | public function showAction(Request $request) |
||
192 | |||
193 | /** |
||
194 | * @param Request $request |
||
195 | * |
||
196 | * @return \Symfony\Component\HttpFoundation\Response |
||
197 | */ |
||
198 | public function indexAction(Request $request) |
||
222 | |||
223 | /** |
||
224 | * @param Request $request |
||
225 | * |
||
226 | * @return Response |
||
227 | */ |
||
228 | public function createAction(Request $request) |
||
284 | |||
285 | /** |
||
286 | * @param Request $request |
||
287 | * |
||
288 | * @return Response |
||
289 | */ |
||
290 | public function updateAction(Request $request) |
||
346 | |||
347 | /** |
||
348 | * @param Request $request |
||
349 | * |
||
350 | * @return Response |
||
351 | */ |
||
352 | public function deleteAction(Request $request) |
||
385 | |||
386 | /** |
||
387 | * @param Request $request |
||
388 | * |
||
389 | * @return RedirectResponse |
||
390 | */ |
||
391 | public function applyStateMachineTransitionAction(Request $request) |
||
426 | |||
427 | /** |
||
428 | * @param RequestConfiguration $configuration |
||
429 | * @param string $permission |
||
430 | * |
||
431 | * @throws AccessDeniedException |
||
432 | */ |
||
433 | protected function isGrantedOr403(RequestConfiguration $configuration, $permission) |
||
445 | |||
446 | /** |
||
447 | * @param RequestConfiguration $configuration |
||
448 | * |
||
449 | * @return \Sylius\Component\Resource\Model\ResourceInterface |
||
450 | * |
||
451 | * @throws NotFoundHttpException |
||
452 | */ |
||
453 | protected function findOr404(RequestConfiguration $configuration) |
||
461 | } |
||
462 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.