| Conditions | 1 |
| Paths | 1 |
| Total Lines | 98 |
| 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 |
||
| 90 | private function registerScriptTreeViewActions($jdata) |
||
| 91 | {
|
||
| 92 | $modelId = $this->model->id; |
||
| 93 | $modelClass = get_class($this->model); |
||
| 94 | $indexUrl = Yii::app()->controller->createUrl('index');
|
||
| 95 | $ajaxUrl = Yii::app()->controller->createUrl('toggle', array('attribute' => '_attr_', 'id' => '_id_'));
|
||
| 96 | $deleteUrl = Yii::app()->controller->createUrl('delete', array('id' => '_id_'));
|
||
| 97 | |||
| 98 | Yii::app()->clientScript->registerScript(__CLASS__.'_InitPlugin', " |
||
| 99 | var treeActions = {$jdata};
|
||
| 100 | var modelClass = '{$modelClass}';
|
||
| 101 | var modelId = '{$modelId}';
|
||
| 102 | var indexUrl = '{$indexUrl}';
|
||
| 103 | var ajaxUrl = '{$ajaxUrl}';
|
||
| 104 | var deleteUrl = '{$deleteUrl}';
|
||
| 105 | |||
| 106 | function initTreeActions() |
||
| 107 | {
|
||
| 108 | |||
| 109 | $('.filetree li:not(#node_1) a').unifloat({
|
||
| 110 | rel: '#treeview-actions', |
||
| 111 | posTop: { value: 'top - 2', auto: false },
|
||
| 112 | posLeft: { value: 'after', auto: false },
|
||
| 113 | |||
| 114 | onShow: function(source, target) |
||
| 115 | {
|
||
| 116 | if( $(source).parent().attr('id') === undefined )
|
||
| 117 | return false; |
||
| 118 | var id = $(source).parent().attr('id').match(/node_(\d+)/)[1];
|
||
| 119 | |||
| 120 | for(var i in treeActions[id]) |
||
| 121 | {
|
||
| 122 | var button = $(target).find('[data-action='+i+']');
|
||
| 123 | button.toggleClass('disabled', treeActions[id][i].disabled);
|
||
| 124 | button.attr('href', treeActions[id][i].url);
|
||
| 125 | button.data('toggle', treeActions[id][i].toggle);
|
||
| 126 | button.data('id', id);
|
||
| 127 | } |
||
| 128 | } |
||
| 129 | }); |
||
| 130 | } |
||
| 131 | ", CClientScript::POS_READY); |
||
| 132 | |||
| 133 | Yii::app()->clientScript->registerScript(__CLASS__, " |
||
| 134 | initTreeActions(); |
||
| 135 | |||
| 136 | $('#treeview-actions a').on('click', function(e)
|
||
| 137 | {
|
||
| 138 | var self = this; |
||
| 139 | var action = $(this).data('action');
|
||
| 140 | var id = $(this).data('id');
|
||
| 141 | |||
| 142 | if( action === 'delete' ) |
||
| 143 | {
|
||
| 144 | e.preventDefault(); |
||
| 145 | |||
| 146 | if( !confirm('Вы действительно хотите удалить данный элемент?') )
|
||
| 147 | return; |
||
| 148 | |||
| 149 | return function(id) |
||
| 150 | {
|
||
| 151 | var callback = function(resp) |
||
| 152 | {
|
||
| 153 | document.location.href = indexUrl; |
||
| 154 | }; |
||
| 155 | |||
| 156 | $.post(deleteUrl.replace('_id_', id), {}, callback);
|
||
| 157 | }(id); |
||
| 158 | } |
||
| 159 | |||
| 160 | if( !$(this).data('toggle') )
|
||
| 161 | return; |
||
| 162 | else |
||
| 163 | e.preventDefault(); |
||
| 164 | |||
| 165 | var callback = function(resp) |
||
| 166 | {
|
||
| 167 | $(self).toggleClass('disabled');
|
||
| 168 | treeActions[id][action].disabled = !treeActions[id][action].disabled; |
||
| 169 | |||
| 170 | if( treeActions[id].visible ) |
||
| 171 | $('#tree_'+modelClass+' li#node_'+id).toggleClass('disabled', treeActions[id].visible.disabled);
|
||
| 172 | |||
| 173 | if( $('#'+modelClass+'_'+action).length && modelId == id )
|
||
| 174 | $('#'+modelClass+'_'+action).attr('checked', treeActions[id][action].disabled ? false : true);
|
||
| 175 | }; |
||
| 176 | |||
| 177 | $.post(ajaxUrl.replace('_attr_', action).replace('_id_', id), {}, callback);
|
||
| 178 | }); |
||
| 179 | |||
| 180 | $('#tree_'+modelClass+' a').each(function()
|
||
| 181 | {
|
||
| 182 | var id = $(this).parent().attr('id').match(/node_(\d+)/)[1];
|
||
| 183 | if( treeActions[id].visible && treeActions[id].visible.disabled === true ) |
||
| 184 | $(this).parent().addClass('disabled');
|
||
| 185 | }); |
||
| 186 | ", CClientScript::POS_READY); |
||
| 187 | } |
||
| 188 | |||
| 304 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.