| Conditions | 1 |
| Paths | 1 |
| Total Lines | 204 |
| Code Lines | 9 |
| 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 |
||
| 20 | public function actionUpdate($id) |
||
| 21 | {
|
||
| 22 | Yii::app()->clientScript->registerScript( |
||
| 23 | 'BContactFormScripts', " |
||
| 24 | $(function(){
|
||
| 25 | //----------------------------------------------------------------------------- |
||
| 26 | // Добавить тестовое поле |
||
| 27 | var textblockCounter = 0; |
||
| 28 | $('.action.btn[data-action=\"add-textblock\"]').on('click', function(){
|
||
| 29 | var name = $('<input type=\"text\" style=\"width:250px\" name=\"BContactTextBlock[new][' + textblockCounter + '][name]\" placeholder=\"Заголовок\" />');
|
||
| 30 | var sysname = $('<input type=\"text\" style=\"width:250px\" name=\"BContactTextBlock[new][' + textblockCounter + '][sysname]\" placeholder=\"Системное имя\" />');
|
||
| 31 | |||
| 32 | // создание кнопки \"Удалить\" |
||
| 33 | var button_delete = $('<a href=\"#\" rel=\"tooltip\" title=\"Удалить текстовый блок\" class=\"btn btn-alone delete\" />');
|
||
| 34 | button_delete.on('click', function(e) {
|
||
| 35 | e.preventDefault(); |
||
| 36 | $(this).parent('div').remove();
|
||
| 37 | }); |
||
| 38 | |||
| 39 | var content = $('<div style=\"margin-bottom:5px\" />');
|
||
| 40 | content.append(name); |
||
| 41 | $(content).append(' ');
|
||
| 42 | content.append(sysname); |
||
| 43 | $(content).append(' ');
|
||
| 44 | $(content).append(button_delete); |
||
| 45 | $('#textblock-add-cell').append(content);
|
||
| 46 | |||
| 47 | textblockCounter++; |
||
| 48 | }); |
||
| 49 | |||
| 50 | //----------------------------------------------------------------------------- |
||
| 51 | // Добавление группы полей |
||
| 52 | var group_counter = 1; |
||
| 53 | $('.action.btn[data-action=\"group\"]').on('click', function(){
|
||
| 54 | // создание поля с названием поля |
||
| 55 | var name = $('<input type=\"text\" style=\"width:250px\" name=\"BContactGroup[' + group_counter + '][name]\" placeholder=\"Название\" />');
|
||
| 56 | |||
| 57 | // создание поля с системным именем поля |
||
| 58 | var sysname = $('<input type=\"text\" style=\"width:250px\" name=\"BContactGroup[' + group_counter + '][sysname]\" placeholder=\"Системное имя\" />');
|
||
| 59 | |||
| 60 | // создание кнопки \"Удалить\" |
||
| 61 | var button_delete = $('<a href=\"#\" rel=\"tooltip\" title=\"Удалить поле\" class=\"btn btn-alone delete\" />');
|
||
| 62 | button_delete.on('click', function(e) {
|
||
| 63 | e.preventDefault(); |
||
| 64 | $(this).parent('div').remove();
|
||
| 65 | }); |
||
| 66 | |||
| 67 | var content = $('<div style=\"margin-bottom:5px\" />');
|
||
| 68 | content.append(name); |
||
| 69 | $(content).append(' ');
|
||
| 70 | content.append(sysname); |
||
| 71 | $(content).append(' ');
|
||
| 72 | $(content).append(button_delete); |
||
| 73 | |||
| 74 | $('#group-add-cell').append(content);
|
||
| 75 | group_counter++; |
||
| 76 | }); |
||
| 77 | |||
| 78 | //----------------------------------------------------------------------------- |
||
| 79 | // Добавление поля к группе |
||
| 80 | $('.action.btn[data-action=\"field\"]').on('click', function(){
|
||
| 81 | var group = $(this).attr('data-group'),
|
||
| 82 | list = $('.field-add-cell[data-group=' + group + '] ul');
|
||
| 83 | var last_id = list.find('li').size();
|
||
| 84 | |||
| 85 | // создание иконки |
||
| 86 | var icon = $('<i class=\"icon-move\" style=\"background:none;\"/>');
|
||
| 87 | |||
| 88 | // создание поля со значением поля |
||
| 89 | var name = $('<input type=\"text\" style=\"width:250px\" name=\"BContactField[new][' + last_id + '][' + group + '][value]\" />');
|
||
| 90 | |||
| 91 | // создание поля с описанием поля |
||
| 92 | var description = $('<input type=\"text\" style=\"width:500px\" name=\"BContactField[new][' + last_id + '][' + group + '][description]\" />');
|
||
| 93 | |||
| 94 | // создание кнопки \"Удалить\" |
||
| 95 | var button_delete = $('<a href=\"#\" rel=\"tooltip\" title=\"Удалить поле\" style=\"background:none;content:none;\" class=\"btn btn-alone delete\" />');
|
||
| 96 | button_delete.on('click', function(e) {
|
||
| 97 | e.preventDefault(); |
||
| 98 | $(this).parent('li').remove();
|
||
| 99 | }); |
||
| 100 | |||
| 101 | // добавляем все созданные элементы в список |
||
| 102 | var content = $('<li class=\"not-sortable\" style=\"margin-bottom:5px;cursor:default;\" />');
|
||
| 103 | |||
| 104 | $(content).append(icon); |
||
| 105 | $(content).append(' ');
|
||
| 106 | $(content).append(name); |
||
| 107 | $(content).append(' ');
|
||
| 108 | $(content).append(description); |
||
| 109 | $(content).append(' ');
|
||
| 110 | $(content).append(button_delete); |
||
| 111 | |||
| 112 | list.append(content); |
||
| 113 | }); |
||
| 114 | |||
| 115 | // ----------------------------------------------------------------------------------------- |
||
| 116 | // Сортировка полей в группе |
||
| 117 | $('.sortable').sortable({
|
||
| 118 | items: 'li:not(.not-sortable)', |
||
| 119 | update: function(event, ui) {
|
||
| 120 | sortFields(ui.item); |
||
| 121 | } |
||
| 122 | }); |
||
| 123 | |||
| 124 | function sortFields(item) {
|
||
| 125 | var url = '" . $this->createUrl('sort') . "';
|
||
| 126 | var list = []; |
||
| 127 | var position = 1; |
||
| 128 | var hasError = false; |
||
| 129 | |||
| 130 | // создание массива полей |
||
| 131 | $(item).parents('ul').children('li:not(.not-sortable)').each(function(){
|
||
| 132 | var listItem = {};
|
||
| 133 | listItem['id'] = $(this).attr('data-fid');
|
||
| 134 | listItem['position'] = position; |
||
| 135 | |||
| 136 | list.push(listItem); |
||
| 137 | position++; |
||
| 138 | }); |
||
| 139 | |||
| 140 | if( !hasError ) |
||
| 141 | $.post(url, {'sort' : list, 'type' : 'field'});
|
||
| 142 | } |
||
| 143 | |||
| 144 | // ----------------------------------------------------------------------------------------- |
||
| 145 | // Сортировка текстовых блоков |
||
| 146 | $('.textblock-container').sortable({
|
||
| 147 | update: function(event, ui) {
|
||
| 148 | sortTextBlocks(ui.item); |
||
| 149 | } |
||
| 150 | }); |
||
| 151 | |||
| 152 | function sortTextBlocks(item) |
||
| 153 | {
|
||
| 154 | var url = '" . $this->createUrl('sort') . "';
|
||
| 155 | var list = []; |
||
| 156 | var position = 1; |
||
| 157 | var hasError = false; |
||
| 158 | |||
| 159 | console.log(item); |
||
| 160 | |||
| 161 | // создание массива полей |
||
| 162 | $(item).parents('ul').children('li').each(function(){
|
||
| 163 | var listItem = {};
|
||
| 164 | listItem['id'] = $(this).attr('data-textblock-id');
|
||
| 165 | listItem['position'] = position; |
||
| 166 | |||
| 167 | list.push(listItem); |
||
| 168 | position++; |
||
| 169 | }); |
||
| 170 | |||
| 171 | if( !hasError ) |
||
| 172 | $.post(url, {'sort' : list, 'type' : 'textblock'});
|
||
| 173 | } |
||
| 174 | |||
| 175 | // ------------------------------------------------------------------------------------------- |
||
| 176 | // Удаление полей |
||
| 177 | $('.btn.delete').on('click', function(e){
|
||
| 178 | e.preventDefault(); |
||
| 179 | |||
| 180 | var id = $(this).attr('data-fid');
|
||
| 181 | var type = 'field'; |
||
| 182 | |||
| 183 | if( id === undefined ) |
||
| 184 | {
|
||
| 185 | id = $(this).attr('data-textblock-id');
|
||
| 186 | type = 'textblock'; |
||
| 187 | } |
||
| 188 | |||
| 189 | if( id === undefined ) |
||
| 190 | return false; |
||
| 191 | |||
| 192 | var url = '" . $this->createUrl('delete') . "';
|
||
| 193 | var self = $(this); |
||
| 194 | |||
| 195 | var callback = function(resp){
|
||
| 196 | $(self).parents('li').fadeOut();
|
||
| 197 | }; |
||
| 198 | |||
| 199 | $.post(url, {'delete' : {'id' : id, 'type' : type}}, callback);
|
||
| 200 | }); |
||
| 201 | |||
| 202 | // ------------------------------------------------------------------------------------------- |
||
| 203 | // Удаление группы полей |
||
| 204 | $('span.delete-group').on('click', function(){
|
||
| 205 | if( !confirm('Вы уверены, что хотите удалить группу полей?') )
|
||
| 206 | return false; |
||
| 207 | |||
| 208 | var url = '" . $this->createUrl('delete') . "';
|
||
| 209 | var id = $(this).attr('data-group');
|
||
| 210 | |||
| 211 | var callback = function(resp){
|
||
| 212 | $('td.field-add-cell[data-group=' + id + ']').parents('tr').fadeOut();
|
||
| 213 | }; |
||
| 214 | |||
| 215 | $.post(url, {'delete' : {'id' : id, 'type' : 'group'}}, callback);
|
||
| 216 | }); |
||
| 217 | |||
| 218 | });", |
||
| 219 | CClientScript::POS_END |
||
| 220 | ); |
||
| 221 | |||
| 222 | parent::actionUpdate($id); |
||
| 223 | } |
||
| 224 | |||
| 449 | } |
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.