| Conditions | 13 |
| Paths | 98 |
| Total Lines | 64 |
| Code Lines | 39 |
| 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 |
||
| 65 | public function listView($fields = array()) |
||
| 66 | { |
||
| 67 | $config = ConfigProvider::getInstance(); |
||
| 68 | $sessionProvider = $config->get('session.provider.name'); |
||
| 69 | $session = SessionProviderFactory::getInstance($sessionProvider); |
||
| 70 | |||
| 71 | $reflection = new \ReflectionClass(get_class($this->record)); |
||
| 72 | $properties = $reflection->getProperties(); |
||
| 73 | $labels = $this->record->getDataLabels(); |
||
| 74 | $colCount = 1; |
||
| 75 | |||
| 76 | $html = '<form action="'.$fields['URI'].'" method="POST">'; |
||
| 77 | $html .= '<table class="table">'; |
||
| 78 | // first render all of the table headers |
||
| 79 | $html .= '<tr>'; |
||
| 80 | foreach ($properties as $propObj) { |
||
| 81 | $prop = $propObj->name; |
||
| 82 | if (!in_array($prop, $this->record->getDefaultAttributes()) && !in_array($prop, $this->record->getTransientAttributes())) { |
||
| 83 | if (get_class($this->record->getPropObject($prop)) != 'Alpha\Model\Type\Text') { |
||
| 84 | ++$colCount; |
||
| 85 | $html .= ' <th>'.$labels[$prop].'</th>'; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | if ($prop == 'ID') { |
||
| 89 | $html .= ' <th>'.$labels[$prop].'</th>'; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | // render the count |
||
| 93 | $html .= ' <th>Item count</th>'; |
||
| 94 | |||
| 95 | $html .= '</tr><tr>'; |
||
| 96 | |||
| 97 | // and now the values |
||
| 98 | foreach ($properties as $propObj) { |
||
| 99 | $prop = $propObj->name; |
||
| 100 | if (!in_array($prop, $this->record->getDefaultAttributes()) && !in_array($prop, $this->record->getTransientAttributes())) { |
||
| 101 | if (get_class($this->record->getPropObject($prop)) != 'Alpha\Model\Type\Text') { |
||
| 102 | $html .= ' <td> '.$this->record->get($prop).'</td>'; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | if ($prop == 'ID') { |
||
| 106 | $html .= ' <td> '.$this->record->getID().'</td>'; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | // render the count |
||
| 110 | $html .= ' <td> '.$this->record->getItemCount().'</td>'; |
||
|
|
|||
| 111 | |||
| 112 | $html .= '</tr>'; |
||
| 113 | |||
| 114 | $html .= '<tr><td colspan="'.($colCount+1).'" style="text-align:center;">'; |
||
| 115 | // render edit buttons for admins only |
||
| 116 | if ($session->get('currentUser') != null && $session->get('currentUser')->inGroup('Admin')) { |
||
| 117 | $html .= ' '; |
||
| 118 | $button = new Button("document.location = '".FrontController::generateSecureURL('act=Alpha\Controller\DEnumController&denumID='.$this->record->getID())."'", 'Edit', 'edit'.$this->record->getID().'But'); |
||
| 119 | $html .= $button->render(); |
||
| 120 | } |
||
| 121 | $html .= '</td></tr>'; |
||
| 122 | |||
| 123 | $html .= '</table>'; |
||
| 124 | |||
| 125 | $html .= '</form>'; |
||
| 126 | |||
| 127 | return $html; |
||
| 128 | } |
||
| 129 | |||
| 184 |
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: