Conditions | 21 |
Paths | 42 |
Total Lines | 122 |
Code Lines | 84 |
Lines | 20 |
Ratio | 16.39 % |
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 |
||
35 | public function view() { |
||
36 | // _context[0] => entry values |
||
37 | // _context[1] => fieldId |
||
38 | if (!is_array($this->_context) || empty($this->_context)) { |
||
39 | $this->_Result->appendChild(new XMLElement('error', __('Parameters not found'))); |
||
40 | return; |
||
41 | } |
||
42 | else if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) { |
||
43 | $this->_Result->appendChild(new XMLElement('error', __('Not enough parameters'))); |
||
44 | return; |
||
45 | } |
||
46 | else if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) { |
||
47 | $this->_Result->appendChild(new XMLElement('error', __('Too many parameters'))); |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | $entriesId = explode(',', MySQL::cleanValue($this->_context[0])); |
||
52 | $entriesId = array_map(array('General', 'intval'), $entriesId); |
||
53 | View Code Duplication | if (!is_array($entriesId) || empty($entriesId)) { |
|
|
|||
54 | $this->_Result->appendChild(new XMLElement('error', __('No entry no found'))); |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | $parentFieldId = General::intval($this->_context[1]); |
||
59 | if ($parentFieldId < 1) { |
||
60 | $this->_Result->appendChild(new XMLElement('error', __('Parent field id not valid'))); |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | $parentField = $this->fieldManager->fetch($parentFieldId); |
||
65 | View Code Duplication | if (!$parentField || empty($parentField)) { |
|
66 | $this->_Result->appendChild(new XMLElement('error', __('Parent field not found'))); |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | if ($parentField->get('type') != 'entry_relationship') { |
||
71 | $this->_Result->appendChild(new XMLElement('error', __('Parent field is `%s`, not `entry_relationship`', array($parentField->get('type'))))); |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | // Get entries one by one since they may belong to |
||
76 | // different sections, which prevents us from |
||
77 | // passing an array of entryId. |
||
78 | foreach ($entriesId as $key => $entryId) { |
||
79 | $entry = $this->entryManager->fetch($entryId); |
||
80 | if (empty($entry)) { |
||
81 | $li = new XMLElement('li', null, array( |
||
82 | 'data-entry-id' => $entryId |
||
83 | )); |
||
84 | $header = new XMLElement('header', null, array('class' => 'frame-header no-content ignore-collapsible')); |
||
85 | $title = new XMLElement('h4'); |
||
86 | $title->appendChild(new XMLElement('strong', __('Entry %s not found', array($entryId)))); |
||
87 | $header->appendChild($title); |
||
88 | $options = new XMLElement('div', null, array('class' => 'destructor')); |
||
89 | View Code Duplication | if ($parentField->is('allow_delete') || $parentField->is('allow_link')) { |
|
90 | $options->appendChild(new XMLElement('a', __('Un-link'), array( |
||
91 | 'class' => 'unlink ignore-collapsible', |
||
92 | 'data-unlink' => $entryId, |
||
93 | ))); |
||
94 | } |
||
95 | $header->appendChild($options); |
||
96 | $li->appendChild($header); |
||
97 | $this->_Result->appendChild($li); |
||
98 | } else { |
||
99 | $entry = $entry[0]; |
||
100 | $entrySection = $this->sectionManager->fetch($entry->get('section_id')); |
||
101 | $entryVisibleFields = $entrySection->fetchVisibleColumns(); |
||
102 | $entryFields = $entrySection->fetchFields(); |
||
103 | $entrySectionHandle = $this->getSectionName($entry, 'handle'); |
||
104 | |||
105 | $li = new XMLElement('li', null, array( |
||
106 | 'data-entry-id' => $entryId, |
||
107 | 'data-section' => $entrySectionHandle, |
||
108 | 'data-section-id' => $entrySection->get('id'), |
||
109 | )); |
||
110 | $header = new XMLElement('header', null, array('class' => 'frame-header')); |
||
111 | $title = new XMLElement('h4', null, array('class' => 'ignore-collapsible')); |
||
112 | $title->appendChild(new XMLElement('strong', $this->getEntryTitle($entry, $entryVisibleFields, $entryFields), array('class' => 'ignore-collapsible'))); |
||
113 | $title->appendChild(new XMLElement('span', $this->getSectionName($entry), array('class' => 'ignore-collapsible'))); |
||
114 | $header->appendChild($title); |
||
115 | $options = new XMLElement('div', null, array('class' => 'destructor')); |
||
116 | if ($parentField->is('allow_edit')) { |
||
117 | $title->setAttribute('data-edit', $entryId); |
||
118 | $options->appendChild(new XMLElement('a', __('Edit'), array( |
||
119 | 'class' => 'edit ignore-collapsible', |
||
120 | 'data-edit' => $entryId, |
||
121 | ))); |
||
122 | } |
||
123 | if ($parentField->is('allow_delete')) { |
||
124 | $options->appendChild(new XMLElement('a', __('Delete'), array( |
||
125 | 'class' => 'delete ignore-collapsible', |
||
126 | 'data-delete' => $entryId, |
||
127 | ))); |
||
128 | } |
||
129 | if ($parentField->is('allow_link')) { |
||
130 | $options->appendChild(new XMLElement('a', __('Replace'), array( |
||
131 | 'class' => 'unlink ignore-collapsible', |
||
132 | 'data-replace' => $entryId, |
||
133 | ))); |
||
134 | } |
||
135 | View Code Duplication | if ($parentField->is('allow_delete') || $parentField->is('allow_link')) { |
|
136 | $options->appendChild(new XMLElement('a', __('Un-link'), array( |
||
137 | 'class' => 'unlink ignore-collapsible', |
||
138 | 'data-unlink' => $entryId, |
||
139 | ))); |
||
140 | } |
||
141 | $header->appendChild($options); |
||
142 | $li->appendChild($header); |
||
143 | |||
144 | $content = ERFXSLTUTilities::entryToXml($parentField, $entry, $entrySectionHandle, $entryFields); |
||
145 | |||
146 | if ($content) { |
||
147 | $li->appendChild($content); |
||
148 | } |
||
149 | else { |
||
150 | $header->setAttribute('class', $header->getAttribute('class') . ' no-content'); |
||
151 | } |
||
152 | |||
153 | $this->_Result->appendChild($li); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
176 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.