Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Book often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Book, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
61 | class Book extends Base |
||
|
|||
62 | { |
||
63 | const ALL_BOOKS_UUID = 'urn:uuid'; |
||
64 | const ALL_BOOKS_ID = 'cops:books'; |
||
65 | const ALL_RECENT_BOOKS_ID = 'cops:recentbooks'; |
||
66 | const BOOK_COLUMNS = 'books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating'; |
||
67 | |||
68 | const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN; |
||
69 | const SQL_BOOKS_ALL = SQL_BOOKS_ALL; |
||
70 | const SQL_BOOKS_BY_PUBLISHER = SQL_BOOKS_BY_PUBLISHER; |
||
71 | const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER; |
||
72 | const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR; |
||
73 | const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE; |
||
74 | const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG; |
||
75 | const SQL_BOOKS_BY_LANGUAGE = SQL_BOOKS_BY_LANGUAGE; |
||
76 | const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM; |
||
77 | const SQL_BOOKS_BY_CUSTOM_BOOL_TRUE = SQL_BOOKS_BY_CUSTOM_BOOL_TRUE; |
||
78 | const SQL_BOOKS_BY_CUSTOM_BOOL_FALSE = SQL_BOOKS_BY_CUSTOM_BOOL_FALSE; |
||
79 | const SQL_BOOKS_BY_CUSTOM_BOOL_NULL = SQL_BOOKS_BY_CUSTOM_BOOL_NULL; |
||
80 | const SQL_BOOKS_BY_CUSTOM_RATING = SQL_BOOKS_BY_CUSTOM_RATING; |
||
81 | const SQL_BOOKS_BY_CUSTOM_RATING_NULL = SQL_BOOKS_BY_CUSTOM_RATING_NULL; |
||
82 | const SQL_BOOKS_BY_CUSTOM_DATE = SQL_BOOKS_BY_CUSTOM_DATE; |
||
83 | const SQL_BOOKS_BY_CUSTOM_DIRECT = SQL_BOOKS_BY_CUSTOM_DIRECT; |
||
84 | const SQL_BOOKS_BY_CUSTOM_DIRECT_ID = SQL_BOOKS_BY_CUSTOM_DIRECT_ID; |
||
85 | const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY; |
||
86 | const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT; |
||
87 | const SQL_BOOKS_BY_RATING = SQL_BOOKS_BY_RATING; |
||
88 | |||
89 | const BAD_SEARCH = 'QQQQQ'; |
||
90 | |||
91 | public $id; |
||
92 | public $title; |
||
93 | public $timestamp; |
||
94 | public $pubdate; |
||
95 | public $path; |
||
96 | public $uuid; |
||
97 | public $hasCover; |
||
98 | public $relativePath; |
||
99 | public $seriesIndex; |
||
100 | public $comment; |
||
101 | public $rating; |
||
102 | public $datas = NULL; |
||
103 | public $authors = NULL; |
||
104 | public $publisher = NULL; |
||
105 | public $serie = NULL; |
||
106 | public $tags = NULL; |
||
107 | public $languages = NULL; |
||
108 | public $format = array (); |
||
109 | |||
110 | |||
111 | 78 | public function __construct($line) { |
|
112 | 78 | $this->id = $line->id; |
|
113 | 78 | $this->title = $line->title; |
|
114 | 78 | $this->timestamp = strtotime($line->timestamp); |
|
115 | 78 | $this->pubdate = $line->pubdate; |
|
116 | 78 | $this->path = Base::getDbDirectory() . $line->path; |
|
117 | 78 | $this->relativePath = $line->path; |
|
118 | 78 | $this->seriesIndex = $line->series_index; |
|
119 | 78 | $this->comment = $line->comment; |
|
120 | 78 | $this->uuid = $line->uuid; |
|
121 | 78 | $this->hasCover = $line->has_cover; |
|
122 | 78 | if (!file_exists($this->getFilePath('jpg'))) { |
|
123 | // double check |
||
124 | 44 | $this->hasCover = 0; |
|
125 | } |
||
126 | 78 | $this->rating = $line->rating; |
|
127 | 78 | } |
|
128 | |||
129 | 42 | public function getEntryId() { |
|
132 | |||
133 | 4 | public static function getEntryIdByLetter ($startingLetter) { |
|
136 | |||
137 | 3 | public function getUri () { |
|
140 | |||
141 | 3 | public function getDetailUrl () { |
|
146 | |||
147 | 43 | public function getTitle () { |
|
150 | |||
151 | /* Other class (author, series, tag, ...) initialization and accessors */ |
||
152 | |||
153 | /** |
||
154 | * @return Author[] |
||
155 | */ |
||
156 | 49 | public function getAuthors () { |
|
157 | 49 | if (is_null($this->authors)) { |
|
158 | 49 | $this->authors = Author::getAuthorByBookId($this->id); |
|
159 | } |
||
160 | 49 | return $this->authors; |
|
161 | } |
||
162 | |||
163 | public function getAuthorsName () { |
||
166 | |||
167 | public function getAuthorsSort () { |
||
170 | |||
171 | 5 | public function getPublisher () { |
|
172 | 5 | if (is_null($this->publisher)) { |
|
173 | 5 | $this->publisher = Publisher::getPublisherByBookId($this->id); |
|
174 | } |
||
175 | 5 | return $this->publisher; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return Serie |
||
180 | */ |
||
181 | 48 | public function getSerie() { |
|
182 | 48 | if (is_null($this->serie)) { |
|
183 | 48 | $this->serie = Serie::getSerieByBookId($this->id); |
|
184 | } |
||
185 | 48 | return $this->serie; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 10 | public function getLanguages() { |
|
192 | 10 | $lang = array(); |
|
193 | 10 | $result = parent::getDb()->prepare('select languages.lang_code |
|
194 | from books_languages_link, languages |
||
195 | where books_languages_link.lang_code = languages.id |
||
196 | and book = ? |
||
197 | order by item_order'); |
||
198 | 10 | $result->execute(array($this->id)); |
|
199 | 10 | while ($post = $result->fetchObject()) |
|
200 | { |
||
201 | 10 | array_push($lang, Language::getLanguageString($post->lang_code)); |
|
202 | } |
||
203 | 10 | return implode(', ', $lang); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return Tag[] |
||
208 | */ |
||
209 | 10 | public function getTags() { |
|
210 | 10 | if (is_null ($this->tags)) { |
|
211 | 10 | $this->tags = array(); |
|
212 | |||
213 | 10 | $result = parent::getDb()->prepare('select tags.id as id, name |
|
214 | from books_tags_link, tags |
||
215 | where tag = tags.id |
||
216 | and book = ? |
||
217 | order by name'); |
||
218 | 10 | $result->execute(array($this->id)); |
|
219 | 10 | while ($post = $result->fetchObject()) |
|
220 | { |
||
221 | 9 | array_push($this->tags, new Tag($post)); |
|
222 | } |
||
223 | } |
||
224 | 10 | return $this->tags; |
|
225 | } |
||
226 | |||
227 | public function getTagsName() { |
||
230 | |||
231 | /** |
||
232 | * @return Data[] |
||
233 | */ |
||
234 | 62 | public function getDatas() |
|
235 | { |
||
236 | 62 | if (is_null($this->datas)) { |
|
237 | 62 | $this->datas = Data::getDataByBook($this); |
|
238 | } |
||
239 | 62 | return $this->datas; |
|
240 | } |
||
241 | |||
242 | /* End of other class (author, series, tag, ...) initialization and accessors */ |
||
243 | |||
244 | 58 | public static function getFilterString() { |
|
245 | 58 | $filter = getURLParam('tag', NULL); |
|
246 | 58 | if (empty($filter)) return ''; |
|
247 | |||
248 | 3 | $exists = true; |
|
249 | 3 | if (preg_match("/^!(.*)$/", $filter, $matches)) { |
|
250 | 1 | $exists = false; |
|
251 | 1 | $filter = $matches[1]; |
|
252 | } |
||
253 | |||
254 | 3 | $result = 'exists (select null from books_tags_link, tags where books_tags_link.book = books.id and books_tags_link.tag = tags.id and tags.name = "' . $filter . '")'; |
|
255 | |||
256 | 3 | if (!$exists) { |
|
257 | 1 | $result = 'not ' . $result; |
|
258 | } |
||
259 | |||
260 | 3 | return 'and ' . $result; |
|
261 | } |
||
262 | |||
263 | 4 | public function GetMostInterestingDataToSendToKindle() |
|
264 | { |
||
265 | 4 | $bestFormatForKindle = array('EPUB', 'PDF', 'AZW3', 'MOBI'); |
|
266 | 4 | $bestRank = -1; |
|
267 | 4 | $bestData = NULL; |
|
268 | 4 | foreach ($this->getDatas() as $data) { |
|
269 | 4 | $key = array_search($data->format, $bestFormatForKindle); |
|
270 | 4 | if ($key !== false && $key > $bestRank) { |
|
271 | 4 | $bestRank = $key; |
|
272 | 4 | $bestData = $data; |
|
273 | } |
||
274 | } |
||
275 | 4 | return $bestData; |
|
276 | } |
||
277 | |||
278 | 3 | public function getDataById($idData) |
|
285 | |||
286 | 8 | public function getRating() { |
|
287 | 8 | if (is_null($this->rating) || $this->rating == 0) { |
|
288 | 3 | return ''; |
|
289 | } |
||
290 | 5 | $retour = ''; |
|
291 | 5 | for ($i = 0; $i < $this->rating / 2; $i++) { |
|
292 | 5 | $retour .= '★'; |
|
293 | } |
||
294 | 5 | for ($i = 0; $i < 5 - $this->rating / 2; $i++) { |
|
295 | 3 | $retour .= '☆'; |
|
296 | } |
||
297 | 5 | return $retour; |
|
298 | } |
||
299 | |||
300 | 15 | public function getPubDate() { |
|
310 | |||
311 | 43 | public function getComment($withSerie = true) { |
|
312 | 43 | $addition = ''; |
|
313 | 43 | $se = $this->getSerie (); |
|
314 | 43 | if (!is_null ($se) && $withSerie) { |
|
315 | 38 | $addition = $addition . '<strong>' . localize('content.series') . '</strong>' . str_format(localize('content.series.data'), $this->seriesIndex, htmlspecialchars($se->name)) . "<br />\n"; |
|
316 | } |
||
317 | 43 | if (preg_match('/<\/(div|p|a|span)>/', $this->comment)) |
|
318 | { |
||
319 | 37 | return $addition . html2xhtml($this->comment); |
|
320 | } |
||
321 | else |
||
322 | { |
||
323 | 31 | return $addition . htmlspecialchars($this->comment); |
|
324 | } |
||
325 | } |
||
326 | |||
327 | public function getDataFormat($format) { |
||
333 | |||
334 | 78 | public function getFilePath($extension, $idData = NULL, $relative = false) |
|
335 | { |
||
336 | 78 | if ($extension == 'jpg') |
|
337 | { |
||
338 | 78 | $file = 'cover.jpg'; |
|
339 | } |
||
340 | else |
||
341 | { |
||
342 | 2 | $data = $this->getDataById($idData); |
|
343 | 2 | if (!$data) return NULL; |
|
344 | 2 | $file = $data->name . '.' . strtolower($data->format); |
|
345 | } |
||
346 | |||
347 | 78 | if ($relative) |
|
348 | { |
||
349 | 3 | return $this->relativePath.'/'.$file; |
|
350 | } |
||
351 | else |
||
352 | { |
||
353 | 78 | return $this->path.'/'.$file; |
|
354 | } |
||
355 | } |
||
356 | |||
357 | public function getUpdatedEpub($idData) |
||
358 | { |
||
359 | global $config; |
||
360 | $data = $this->getDataById($idData); |
||
361 | |||
362 | try |
||
363 | { |
||
364 | $epub = new EPub($data->getLocalPath()); |
||
365 | |||
366 | $epub->Title($this->title); |
||
367 | $authorArray = array(); |
||
368 | foreach ($this->getAuthors() as $author) { |
||
369 | $authorArray[$author->sort] = $author->name; |
||
370 | } |
||
371 | $epub->Authors($authorArray); |
||
372 | $epub->Language($this->getLanguages()); |
||
373 | $epub->Description ($this->getComment(false)); |
||
374 | $epub->Subjects($this->getTagsName()); |
||
375 | $epub->Cover2($this->getFilePath('jpg'), 'image/jpeg'); |
||
376 | $epub->Calibre($this->uuid); |
||
377 | $se = $this->getSerie(); |
||
378 | if (!is_null($se)) { |
||
379 | $epub->Serie($se->name); |
||
380 | $epub->SerieIndex($this->seriesIndex); |
||
381 | } |
||
382 | $filename = $data->getUpdatedFilenameEpub(); |
||
383 | if ($config['cops_provide_kepub'] == '1' && preg_match('/Kobo/', $_SERVER['HTTP_USER_AGENT'])) { |
||
384 | $epub->updateForKepub(); |
||
385 | $filename = $data->getUpdatedFilenameKepub(); |
||
386 | } |
||
387 | $epub->download($filename); |
||
388 | } |
||
389 | catch (Exception $e) |
||
390 | { |
||
391 | echo 'Exception : ' . $e->getMessage(); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | 3 | public function getThumbnail($width, $height, $outputfile = NULL) { |
|
396 | 3 | if (is_null($width) && is_null($height)) { |
|
397 | 1 | return false; |
|
398 | } |
||
399 | |||
400 | 3 | $file = $this->getFilePath('jpg'); |
|
401 | // get image size |
||
402 | 3 | if ($size = GetImageSize($file)) { |
|
403 | 3 | $w = $size[0]; |
|
404 | 3 | $h = $size[1]; |
|
405 | //set new size |
||
406 | 3 | if (!is_null($width)) { |
|
407 | 2 | $nw = $width; |
|
408 | 2 | if ($nw >= $w) { return false; } |
|
409 | 1 | $nh = ($nw*$h)/$w; |
|
410 | } else { |
||
411 | 2 | $nh = $height; |
|
412 | 2 | if ($nh >= $h) { return false; } |
|
413 | 2 | $nw = ($nh*$w)/$h; |
|
414 | } |
||
415 | } else { |
||
416 | return false; |
||
417 | } |
||
418 | |||
419 | //draw the image |
||
420 | 2 | $src_img = imagecreatefromjpeg($file); |
|
421 | 2 | $dst_img = imagecreatetruecolor($nw,$nh); |
|
422 | 2 | imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image |
|
423 | 2 | imagejpeg($dst_img,$outputfile,80); |
|
424 | 2 | imagedestroy($src_img); |
|
425 | 2 | imagedestroy($dst_img); |
|
426 | |||
427 | 2 | return true; |
|
428 | } |
||
429 | |||
430 | 44 | public function getLinkArray () |
|
431 | { |
||
432 | 44 | $linkArray = array(); |
|
433 | |||
434 | 44 | if ($this->hasCover) |
|
435 | { |
||
436 | 18 | array_push($linkArray, Data::getLink($this, 'jpg', 'image/jpeg', Link::OPDS_IMAGE_TYPE, 'cover.jpg', NULL)); |
|
437 | |||
438 | 18 | array_push($linkArray, Data::getLink($this, 'jpg', 'image/jpeg', Link::OPDS_THUMBNAIL_TYPE, 'cover.jpg', NULL)); |
|
439 | } |
||
440 | |||
441 | 44 | foreach ($this->getDatas() as $data) |
|
442 | { |
||
443 | 44 | if ($data->isKnownType()) |
|
444 | { |
||
445 | 44 | array_push($linkArray, $data->getDataLink(Link::OPDS_ACQUISITION_TYPE, $data->format)); |
|
446 | } |
||
447 | } |
||
448 | |||
449 | 44 | foreach ($this->getAuthors() as $author) { |
|
450 | /* @var $author Author */ |
||
451 | 44 | array_push($linkArray, new LinkNavigation($author->getUri(), 'related', str_format(localize('bookentry.author'), localize('splitByLetter.book.other'), $author->name))); |
|
452 | } |
||
453 | |||
454 | 44 | $serie = $this->getSerie(); |
|
455 | 44 | if (!is_null ($serie)) { |
|
456 | 41 | array_push($linkArray, new LinkNavigation($serie->getUri(), 'related', str_format(localize('content.series.data'), $this->seriesIndex, $serie->name))); |
|
457 | } |
||
458 | |||
459 | 44 | return $linkArray; |
|
460 | } |
||
461 | |||
462 | |||
463 | 41 | public function getEntry() { |
|
464 | 41 | return new EntryBook($this->getTitle(), $this->getEntryId(), |
|
465 | 41 | $this->getComment(), 'text/html', |
|
466 | 41 | $this->getLinkArray(), $this); |
|
467 | } |
||
468 | |||
469 | 3 | public static function getBookCount($database = NULL) { |
|
470 | 3 | return parent::executeQuerySingle('select count(*) from books', $database); |
|
471 | } |
||
472 | |||
473 | 21 | public static function getCount() { |
|
491 | |||
492 | 8 | public static function getBooksByAuthor($authorId, $n) { |
|
493 | 8 | return self::getEntryArray(self::SQL_BOOKS_BY_AUTHOR, array($authorId), $n); |
|
494 | } |
||
495 | |||
496 | 1 | public static function getBooksByRating($ratingId, $n) { |
|
497 | 1 | return self::getEntryArray(self::SQL_BOOKS_BY_RATING, array($ratingId), $n); |
|
498 | } |
||
499 | |||
500 | 2 | public static function getBooksByPublisher($publisherId, $n) { |
|
501 | 2 | return self::getEntryArray(self::SQL_BOOKS_BY_PUBLISHER, array($publisherId), $n); |
|
502 | } |
||
503 | |||
504 | 2 | public static function getBooksBySeries($serieId, $n) { |
|
505 | 2 | return self::getEntryArray(self::SQL_BOOKS_BY_SERIE, array($serieId), $n); |
|
506 | } |
||
507 | |||
508 | 2 | public static function getBooksByTag($tagId, $n) { |
|
509 | 2 | return self::getEntryArray(self::SQL_BOOKS_BY_TAG, array($tagId), $n); |
|
510 | } |
||
511 | |||
512 | 2 | public static function getBooksByLanguage($languageId, $n) { |
|
513 | 2 | return self::getEntryArray(self::SQL_BOOKS_BY_LANGUAGE, array($languageId), $n); |
|
514 | } |
||
515 | |||
516 | /** |
||
517 | * @param $customColumn CustomColumn |
||
518 | * @param $id integer |
||
519 | * @param $n integer |
||
520 | * @return array |
||
521 | */ |
||
522 | 4 | public static function getBooksByCustom($customColumn, $id, $n) { |
|
523 | 4 | list($query, $params) = $customColumn->getQuery($id); |
|
524 | |||
525 | 4 | return self::getEntryArray($query, $params, $n); |
|
526 | } |
||
527 | |||
528 | 37 | public static function getBookById($bookId) { |
|
540 | |||
541 | 1 | public static function getBookByDataId($dataId) { |
|
556 | |||
557 | 2 | public static function getBooksByQuery($query, $n, $database = NULL, $numberPerPage = NULL) { |
|
558 | 2 | $i = 0; |
|
559 | 2 | $critArray = array(); |
|
560 | 2 | foreach (array(PageQueryResult::SCOPE_AUTHOR, |
|
561 | 2 | PageQueryResult::SCOPE_TAG, |
|
562 | 2 | PageQueryResult::SCOPE_SERIES, |
|
563 | 2 | PageQueryResult::SCOPE_PUBLISHER, |
|
564 | 2 | PageQueryResult::SCOPE_BOOK) as $key) { |
|
565 | 2 | if (in_array($key, getCurrentOption('ignored_categories')) || |
|
566 | 2 | (!array_key_exists($key, $query) && !array_key_exists('all', $query))) { |
|
567 | $critArray[$i] = self::BAD_SEARCH; |
||
568 | } |
||
569 | else { |
||
570 | 2 | if (array_key_exists($key, $query)) { |
|
571 | $critArray[$i] = $query[$key]; |
||
572 | } else { |
||
573 | 2 | $critArray[$i] = $query["all"]; |
|
574 | } |
||
575 | } |
||
576 | 2 | $i++; |
|
577 | } |
||
578 | 2 | return self::getEntryArray(self::SQL_BOOKS_QUERY, $critArray, $n, $database, $numberPerPage); |
|
579 | } |
||
580 | |||
581 | 1 | public static function getBooks($n) { |
|
585 | |||
586 | 3 | View Code Duplication | public static function getAllBooks() { |
603 | |||
604 | 25 | public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) { |
|
607 | |||
608 | 55 | public static function getEntryArray($query, $params, $n, $database = NULL, $numberPerPage = NULL) { |
|
621 | |||
622 | 5 | public static function getAllRecentBooks() { |
|
627 | |||
628 | /** |
||
629 | * The values of all the specified columns |
||
630 | * |
||
631 | * @param string[] $columns |
||
632 | * @return CustomColumn[] |
||
633 | */ |
||
634 | 5 | public function getCustomColumnValues($columns, $asArray = false) { |
|
653 | } |
||
654 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.