| Conditions | 1 |
| Paths | 1 |
| Total Lines | 51 |
| Code Lines | 10 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 121 | protected function registerScript() |
||
| 122 | { |
||
| 123 | $JS = <<<JS |
||
| 124 | $("#%1\$s, #%2\$s").change(function (){ |
||
| 125 | var \$this = $(this), |
||
| 126 | buttonSelector = \$this.attr('id'), |
||
| 127 | \$button = $('button[data-mc-action="' + buttonSelector + '"]'), |
||
| 128 | \$selected = $('option:selected', \$this), |
||
| 129 | categoryId = parseInt(\$selected.val()), |
||
| 130 | categoryName = \$selected.text(), |
||
| 131 | items = $("{$this->gridSelector}").yiiGridView('getSelectedRows'); |
||
| 132 | if (items.length) { |
||
| 133 | if (false === isNaN(categoryId)) { |
||
| 134 | \$button.attr("disabled", false); |
||
| 135 | \$button.data('cat-id', categoryId) |
||
| 136 | .data('cat-name', categoryName) |
||
| 137 | .data('items', items); |
||
| 138 | } else { |
||
| 139 | \$button.attr("disabled", true); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | }); |
||
| 143 | $('button[data-mc-action]').click(function () { |
||
| 144 | var \$this = $(this), |
||
| 145 | \$modal = $("{$this->modalSelector}"), |
||
| 146 | action = \$this.data('mc-action'), |
||
| 147 | \$textContainer = $('#mass-categories-action-modal-text', \$modal), |
||
| 148 | categoryId = parseInt(\$this.data('cat-id')), |
||
| 149 | categoryName = \$this.data('cat-name'), |
||
| 150 | items = \$this.data('items'); |
||
| 151 | \$textContainer.closest('.alert').removeClass('alert-danger').addClass('alert-info'); |
||
| 152 | if (false === isNaN(categoryId)) { |
||
| 153 | switch (action) { |
||
| 154 | case '%1\$s' : |
||
| 155 | \$textContainer.text('{$this->addText} ' + categoryName + '?'); |
||
| 156 | break; |
||
| 157 | case '%2\$s' : |
||
| 158 | \$textContainer.text('{$this->moveText} ' + categoryName + '?'); |
||
| 159 | break; |
||
| 160 | } |
||
| 161 | \$modal.data('url', "{$this->url}") |
||
| 162 | .data('items', items) |
||
| 163 | .data('cat-id', categoryId) |
||
| 164 | .data('mc-action', action) |
||
| 165 | .modal('show'); |
||
| 166 | } |
||
| 167 | return false; |
||
| 168 | }); |
||
| 169 | JS; |
||
| 170 | $this->view->registerJs(sprintf($JS, self::ADD_ACTION, self::MOVE_ACTION)); |
||
| 171 | } |
||
| 172 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: