Complex classes like SummaryForm 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 SummaryForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class SummaryForm extends AbstractHelper |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Invoke as function. |
||
| 33 | * |
||
| 34 | * @param null|SummaryFormInterface $form |
||
| 35 | * @param string $layout |
||
| 36 | * @param array $parameter |
||
| 37 | * @return \Core\Form\View\Helper\SummaryForm|string |
||
|
|
|||
| 38 | */ |
||
| 39 | public function __invoke(SummaryFormInterface $form = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Renders a summary form container. |
||
| 58 | * |
||
| 59 | * @param SummaryFormInterface $form |
||
| 60 | * @param string $layout |
||
| 61 | * @param array $parameter |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function render(SummaryFormInterface $form, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Only renders the form representation of a summary form. |
||
| 125 | * |
||
| 126 | * @param SummaryFormInterface $form |
||
| 127 | * @param string $layout |
||
| 128 | * @param array $parameter |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function renderForm(SummaryFormInterface $form, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Only renders the summary representation of a summary form |
||
| 160 | * |
||
| 161 | * @param SummaryFormInterface $form |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function renderSummary(SummaryFormInterface $form) |
||
| 165 | { |
||
| 166 | $form->prepare(); |
||
| 167 | $baseFieldset = $form->getBaseFieldset(); |
||
| 168 | if (!isset($baseFieldset)) { |
||
| 169 | throw new \InvalidArgumentException('For the Form ' . get_class($form) . ' there is no Basefieldset'); |
||
| 170 | } |
||
| 171 | |||
| 172 | $dataAttributesMarkup = ''; |
||
| 173 | |||
| 174 | foreach ($form->getAttributes() as $dataKey => $dataValue) |
||
| 175 | { |
||
| 176 | if (preg_match('/^data-/', $dataKey)) |
||
| 177 | { |
||
| 178 | $dataAttributesMarkup .= sprintf(' %s="%s"', $dataKey, $dataValue); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $markup = '<div class="panel panel-default" style="min-height: 100px;"' . $dataAttributesMarkup . '> |
||
| 183 | <div class="panel-body"><div class="sf-controls">%s</div>%s</div></div>'; |
||
| 184 | |||
| 185 | $view = $this->getView(); |
||
| 186 | $buttonMarkup = false === $form->getOption('editable') |
||
| 187 | ? '' |
||
| 188 | : '<button type="button" class="btn btn-default btn-xs sf-edit">' |
||
| 189 | . '<span class="yk-icon yk-icon-edit"></span> ' |
||
| 190 | . $view->translate('Edit') |
||
| 191 | . '</button>'; |
||
| 192 | |||
| 193 | if (($controlButtons = $form->getOption('control_buttons')) !== null) |
||
| 194 | { |
||
| 195 | $buttonMarkup .= PHP_EOL . implode(PHP_EOL, array_map(function (array $buttonSpec) use ($view) { |
||
| 196 | return '<button type="button" class="btn btn-default btn-xs' . (isset($buttonSpec['class']) ? ' ' . $buttonSpec['class'] : '') . '">' |
||
| 197 | . (isset($buttonSpec['icon']) ? '<span class="yk-icon yk-icon-' . $buttonSpec['icon'] . '"></span> ' : '') |
||
| 198 | . $view->translate($buttonSpec['label']) |
||
| 199 | . '</button>'; |
||
| 200 | }, $controlButtons)); |
||
| 201 | } |
||
| 202 | |||
| 203 | $elementMarkup = $this->renderSummaryElement($baseFieldset); |
||
| 204 | |||
| 205 | |||
| 206 | return sprintf($markup, $buttonMarkup, $elementMarkup); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Helper function to recurse into form elements when rendering summary. |
||
| 211 | * |
||
| 212 | * @param ElementInterface $element |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function renderSummaryElement(ElementInterface $element) |
||
| 315 | } |
||
| 316 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.