Conditions | 22 |
Paths | 494 |
Total Lines | 109 |
Code Lines | 64 |
Lines | 13 |
Ratio | 11.93 % |
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 |
||
92 | public function __invoke($row, array $cols, AbstractAction $rowClickAction = null, array $rowStyles = [], $hasMassActions = false) |
||
93 | { |
||
94 | $return = $this->getTr($row); |
||
95 | |||
96 | if (true === $hasMassActions) { |
||
97 | $return .= '<td><input type="checkbox" name="massActionSelected[]" value="'.$row['idConcated'].'" /></td>'; |
||
98 | } |
||
99 | |||
100 | foreach ($cols as $col) { |
||
101 | /* @var $col Column\AbstractColumn */ |
||
102 | |||
103 | $value = $row[$col->getUniqueId()]; |
||
104 | |||
105 | $cssStyles = []; |
||
106 | $classes = []; |
||
107 | |||
108 | if ($col->isHidden() === true) { |
||
109 | $classes[] = 'hidden'; |
||
110 | } |
||
111 | |||
112 | switch (get_class($col->getType())) { |
||
113 | |||
114 | case Column\Type\Number::class: |
||
115 | $cssStyles[] = 'text-align: right'; |
||
116 | break; |
||
117 | |||
118 | case Column\Type\PhpArray::class: |
||
119 | $value = '<pre>'.print_r($value, true).'</pre>'; |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | $styles = array_merge($rowStyles, $col->getStyles()); |
||
124 | foreach ($styles as $style) { |
||
125 | /* @var $style Column\Style\AbstractStyle */ |
||
126 | if ($style->isApply($row) === true) { |
||
127 | switch (get_class($style)) { |
||
128 | |||
129 | case Column\Style\Bold::class: |
||
130 | $cssStyles[] = 'font-weight: bold'; |
||
131 | break; |
||
132 | |||
133 | case Column\Style\Italic::class: |
||
134 | $cssStyles[] = 'font-style: italic'; |
||
135 | break; |
||
136 | |||
137 | case Column\Style\Color::class: |
||
138 | $cssStyles[] = 'color: #'.$style->getRgbHexString(); |
||
|
|||
139 | break; |
||
140 | |||
141 | case Column\Style\BackgroundColor::class: |
||
142 | $cssStyles[] = 'background-color: #'.$style->getRgbHexString(); |
||
143 | break; |
||
144 | |||
145 | case Column\Style\Align::class: |
||
146 | $cssStyles[] = 'text-align: '.$style->getAlignment(); |
||
147 | break; |
||
148 | |||
149 | case Column\Style\Strikethrough::class: |
||
150 | $value = '<s>'.$value.'</s>'; |
||
151 | break; |
||
152 | |||
153 | case Column\Style\CSSClass::class: |
||
154 | $classes[] = $style->getClass(); |
||
155 | break; |
||
156 | |||
157 | case Column\Style\Html::class: |
||
158 | // do NOTHING! just pass the HTML! |
||
159 | break; |
||
160 | |||
161 | default: |
||
162 | throw new \InvalidArgumentException('Not defined style: "'.get_class($style).'"'); |
||
163 | break; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | View Code Duplication | if ($col instanceof Column\Action) { |
|
169 | /* @var $col Column\Action */ |
||
170 | $actions = []; |
||
171 | foreach ($col->getActions() as $action) { |
||
172 | /* @var $action Column\Action\AbstractAction */ |
||
173 | if ($action->isDisplayed($row) === true) { |
||
174 | $action->setTitle($this->translate($action->getTitle())); |
||
175 | $actions[] = $action->toHtml($row); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $value = implode(' ', $actions); |
||
180 | } |
||
181 | |||
182 | // "rowClick" action |
||
183 | if ($col instanceof Column\Select && $rowClickAction instanceof AbstractAction |
||
184 | && $col->isRowClickEnabled()) { |
||
185 | $value = '<a href="'.$rowClickAction->getLinkReplaced($row).'">'.$value.'</a>'; |
||
186 | } |
||
187 | |||
188 | $attributes = [ |
||
189 | 'class' => implode(' ', $classes), |
||
190 | 'style' => implode(';', $cssStyles), |
||
191 | 'data-columnUniqueId' => $col->getUniqueId(), |
||
192 | ]; |
||
193 | |||
194 | $return .= $this->getTd($value, $attributes); |
||
195 | } |
||
196 | |||
197 | $return .= $this->getTr($row, false); |
||
198 | |||
199 | return $return; |
||
200 | } |
||
201 | } |
||
202 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: