Conditions | 10 |
Paths | 48 |
Total Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function listAction(Request $request = null) |
||
63 | { |
||
64 | $this->admin->checkAccess('list'); |
||
65 | |||
66 | if ($listMode = $request->get('_list_mode', 'mosaic')) { |
||
67 | $this->admin->setListMode($listMode); |
||
68 | } |
||
69 | |||
70 | $datagrid = $this->admin->getDatagrid(); |
||
71 | |||
72 | $filters = $request->get('filter'); |
||
73 | |||
74 | // set the default context |
||
75 | if (!$filters || !\array_key_exists('context', $filters)) { |
||
76 | $context = $this->admin->getPersistentParameter('context', $this->get('sonata.media.pool')->getDefaultContext()); |
||
77 | } else { |
||
78 | $context = $filters['context']['value']; |
||
79 | } |
||
80 | |||
81 | $datagrid->setValue('context', null, $context); |
||
82 | |||
83 | $rootCategory = null; |
||
84 | if ($this->has('sonata.media.manager.category')) { |
||
85 | $rootCategory = $this->get('sonata.media.manager.category')->getRootCategory($context); |
||
86 | } |
||
87 | |||
88 | if (null !== $rootCategory && !$filters) { |
||
89 | $datagrid->setValue('category', null, $rootCategory->getId()); |
||
90 | } |
||
91 | if ($this->has('sonata.media.manager.category') && $request->get('category')) { |
||
92 | $category = $this->get('sonata.media.manager.category')->findOneBy([ |
||
93 | 'id' => (int) $request->get('category'), |
||
94 | 'context' => $context, |
||
95 | ]); |
||
96 | |||
97 | if (!empty($category)) { |
||
98 | $datagrid->setValue('category', null, $category->getId()); |
||
99 | } else { |
||
100 | $datagrid->setValue('category', null, $rootCategory->getId()); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | $formView = $datagrid->getForm()->createView(); |
||
105 | |||
106 | $this->setFormTheme($formView, $this->admin->getFilterTheme()); |
||
107 | |||
108 | return $this->render($this->admin->getTemplate('list'), [ |
||
109 | 'action' => 'list', |
||
110 | 'form' => $formView, |
||
111 | 'datagrid' => $datagrid, |
||
112 | 'root_category' => $rootCategory, |
||
113 | 'csrf_token' => $this->getCsrfToken('sonata.batch'), |
||
114 | ]); |
||
115 | } |
||
116 | |||
127 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: