| Conditions | 5 |
| Paths | 9 |
| Total Lines | 81 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 82 | public function run() |
||
| 83 | { |
||
| 84 | if (!is_array($this->treeDataRoute)) { |
||
| 85 | throw new InvalidConfigException("Attribute treeDataRoute is required to use TreeWidget."); |
||
| 86 | } |
||
| 87 | |||
| 88 | $id = $this->getId(); |
||
| 89 | |||
| 90 | $options = [ |
||
| 91 | 'plugins' => $this->plugins, |
||
| 92 | 'core' => [ |
||
| 93 | 'check_callback' => true, |
||
| 94 | 'data' => [ |
||
| 95 | 'url' => new JsExpression( |
||
| 96 | "function (node) { |
||
| 97 | return " . Json::encode(Url::to($this->treeDataRoute)) . "; |
||
| 98 | }" |
||
| 99 | ), |
||
| 100 | 'data' => new JsExpression( |
||
| 101 | "function (node) { |
||
| 102 | return { 'id' : node.id }; |
||
| 103 | }" |
||
| 104 | ), |
||
| 105 | ] |
||
| 106 | ] |
||
| 107 | ]; |
||
| 108 | |||
| 109 | // merge with contextmenu configuration |
||
| 110 | $options = ArrayHelper::merge($options, $this->contextMenuOptions()); |
||
| 111 | |||
| 112 | // merge with attribute-provided options |
||
| 113 | $options = ArrayHelper::merge($options, $this->options); |
||
| 114 | |||
| 115 | $options = Json::encode($options); |
||
| 116 | |||
| 117 | $this->getView()->registerAssetBundle('devgroup\JsTreeWidget\JsTreeAssetBundle'); |
||
| 118 | |||
| 119 | $doubleClick = ''; |
||
| 120 | if ($this->doubleClickAction !== false) { |
||
| 121 | $doubleClick = " |
||
| 122 | jsTree_$id.on('dblclick.jstree', function (e) { |
||
| 123 | var node = $(e.target).closest('.jstree-node').children('.jstree-anchor'); |
||
| 124 | var callback = " . $this->doubleClickAction . "; |
||
| 125 | callback(node); |
||
| 126 | return false; |
||
| 127 | });\n"; |
||
| 128 | } |
||
| 129 | $changeParent = ''; |
||
| 130 | if ($this->changeParentAction !== false) { |
||
| 131 | $changeParent =" |
||
| 132 | jsTree_$id.bind('move_node.jstree', function(e, data) { |
||
| 133 | jQuery.get( |
||
| 134 | '" . $this->changeParentAction . "', |
||
| 135 | { |
||
| 136 | 'id': data.node.id, |
||
| 137 | 'parent_id': data.parent |
||
| 138 | } |
||
| 139 | ); |
||
| 140 | return false; |
||
| 141 | });\n"; |
||
| 142 | } |
||
| 143 | $reorder = ''; |
||
| 144 | if ($this->reorderAction !== false) { |
||
| 145 | $reorder =" |
||
| 146 | jsTree_$id.bind('move_node.jstree', function(e, data) { |
||
| 147 | var params = []; |
||
| 148 | jQuery('.jstree-node').each(function(i, e) { |
||
| 149 | params[e.id] = i; |
||
| 150 | }); |
||
| 151 | jQuery.post( |
||
| 152 | '" . $this->reorderAction . "', |
||
| 153 | {'order':params } |
||
| 154 | ); |
||
| 155 | return false; |
||
| 156 | });\n"; |
||
| 157 | } |
||
| 158 | $this->getView()->registerJs(" |
||
| 159 | var jsTree_$id = \$('#$id').jstree($options); |
||
| 160 | $doubleClick $changeParent $reorder", View::POS_READY); |
||
| 161 | return Html::tag('div', '', ['id' => $id]); |
||
| 162 | } |
||
| 163 | |||
| 183 | } |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.