Conditions | 22 |
Paths | 26 |
Total Lines | 117 |
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 |
||
105 | public function NextEntity(&$TORRENT, &$offset) { |
||
106 | // https://fileformats.fandom.com/wiki/Torrent_file |
||
107 | // https://en.wikipedia.org/wiki/Torrent_file |
||
108 | // https://en.wikipedia.org/wiki/Bencode |
||
109 | |||
110 | if ($offset >= strlen($TORRENT)) { |
||
111 | $this->error('cannot read beyond end of file '.$offset); |
||
112 | return false; |
||
113 | } |
||
114 | $type = $TORRENT[$offset++]; |
||
115 | if ($type == 'i') { |
||
116 | |||
117 | // Integers are stored as i<integer>e: |
||
118 | // i90e |
||
119 | $value = $this->ReadSequentialDigits($TORRENT, $offset, true); |
||
120 | if ($TORRENT[$offset++] == 'e') { |
||
121 | //echo '<li>int: '.$value.'</li>'; |
||
122 | return (int) $value; |
||
123 | } |
||
124 | $this->error('unexpected('.__LINE__.') input "'.$value.'" at offset '.($offset - 1)); |
||
125 | return false; |
||
126 | |||
127 | } elseif ($type == 'd') { |
||
128 | |||
129 | // Dictionaries are stored as d[key1][value1][key2][value2][...]e. Keys and values appear alternately. |
||
130 | // Keys must be strings and must be ordered alphabetically. |
||
131 | // For example, {apple-red, lemon-yellow, violet-blue, banana-yellow} is stored as: |
||
132 | // d5:apple3:red6:banana6:yellow5:lemon6:yellow6:violet4:bluee |
||
133 | $values = array(); |
||
134 | //echo 'DICTIONARY @ '.$offset.'<ul>'; |
||
135 | $info_dictionary_start = null; // dummy declaration to prevent "Variable might not be defined" warnings |
||
136 | while (true) { |
||
137 | if ($TORRENT[$offset] === 'e') { |
||
138 | break; |
||
139 | } |
||
140 | $thisentry = array(); |
||
141 | $key = $this->NextEntity($TORRENT, $offset); |
||
142 | if ($key == 'info') { |
||
143 | $info_dictionary_start = $offset; |
||
144 | } |
||
145 | if ($key === false) { |
||
146 | $this->error('unexpected('.__LINE__.') input at offset '.$offset); |
||
147 | return false; |
||
148 | } |
||
149 | $value = $this->NextEntity($TORRENT, $offset); |
||
150 | if ($key == 'info') { |
||
151 | $info_dictionary_end = $offset; |
||
152 | $this->infohash = sha1(substr($TORRENT, $info_dictionary_start, $info_dictionary_end - $info_dictionary_start)); |
||
153 | } |
||
154 | if ($value === false) { |
||
155 | $this->error('unexpected('.__LINE__.') input at offset '.$offset); |
||
156 | return false; |
||
157 | } |
||
158 | $values[$key] = $value; |
||
159 | } |
||
160 | if ($TORRENT[$offset++] == 'e') { |
||
161 | //echo '</ul>'; |
||
162 | return $values; |
||
163 | } |
||
164 | $this->error('unexpected('.__LINE__.') input "'.$TORRENT[($offset - 1)].'" at offset '.($offset - 1)); |
||
165 | return false; |
||
166 | |||
167 | } elseif ($type == 'l') { |
||
168 | |||
169 | //echo 'LIST @ '.$offset.'<ul>'; |
||
170 | // Lists are stored as l[value 1][value2][value3][...]e. For example, {spam, eggs, cheeseburger} is stored as: |
||
171 | // l4:spam4:eggs12:cheeseburgere |
||
172 | $values = array(); |
||
173 | while (true) { |
||
174 | if ($TORRENT[$offset] === 'e') { |
||
175 | break; |
||
176 | } |
||
177 | $NextEntity = $this->NextEntity($TORRENT, $offset); |
||
178 | if ($NextEntity === false) { |
||
179 | $this->error('unexpected('.__LINE__.') input at offset '.($offset - 1)); |
||
180 | return false; |
||
181 | } |
||
182 | $values[] = $NextEntity; |
||
183 | } |
||
184 | if ($TORRENT[$offset++] == 'e') { |
||
185 | //echo '</ul>'; |
||
186 | return $values; |
||
187 | } |
||
188 | $this->error('unexpected('.__LINE__.') input "'.$TORRENT[($offset - 1)].'" at offset '.($offset - 1)); |
||
189 | return false; |
||
190 | |||
191 | } elseif (ctype_digit($type)) { |
||
192 | |||
193 | // Strings are stored as <length of string>:<string>: |
||
194 | // 4:wiki |
||
195 | $length = $type; |
||
196 | while (true) { |
||
197 | $char = $TORRENT[$offset++]; |
||
198 | if ($char == ':') { |
||
199 | break; |
||
200 | } elseif (!ctype_digit($char)) { |
||
201 | $this->error('unexpected('.__LINE__.') input "'.$char.'" at offset '.($offset - 1)); |
||
202 | return false; |
||
203 | } |
||
204 | $length .= $char; |
||
205 | } |
||
206 | if (($offset + $length) > strlen($TORRENT)) { |
||
207 | $this->error('string at offset '.$offset.' claims to be '.$length.' bytes long but only '.(strlen($TORRENT) - $offset).' bytes of data left in file'); |
||
208 | return false; |
||
209 | } |
||
210 | $string = substr($TORRENT, $offset, $length); |
||
211 | $offset += $length; |
||
212 | //echo '<li>string: '.$string.'</li>'; |
||
213 | return (string) $string; |
||
214 | |||
215 | } else { |
||
216 | |||
217 | $this->error('unexpected('.__LINE__.') input "'.$type.'" at offset '.($offset - 1)); |
||
218 | return false; |
||
219 | |||
220 | } |
||
221 | } |
||
222 | |||
248 |
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.