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 |
||
52 | class Book extends Base { |
||
53 | const ALL_BOOKS_UUID = "urn:uuid"; |
||
54 | const ALL_BOOKS_ID = "cops:books"; |
||
55 | const ALL_RECENT_BOOKS_ID = "cops:recentbooks"; |
||
56 | const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating"; |
||
57 | |||
58 | const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN; |
||
59 | const SQL_BOOKS_ALL = SQL_BOOKS_ALL; |
||
60 | const SQL_BOOKS_BY_PUBLISHER = SQL_BOOKS_BY_PUBLISHER; |
||
61 | const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER; |
||
62 | const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR; |
||
63 | const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE; |
||
64 | const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG; |
||
65 | const SQL_BOOKS_BY_LANGUAGE = SQL_BOOKS_BY_LANGUAGE; |
||
66 | const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM; |
||
67 | const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY; |
||
68 | const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT; |
||
69 | const SQL_BOOKS_BY_RATING = SQL_BOOKS_BY_RATING; |
||
70 | |||
71 | const BAD_SEARCH = "QQQQQ"; |
||
72 | |||
73 | public $id; |
||
74 | public $title; |
||
75 | public $timestamp; |
||
76 | public $pubdate; |
||
77 | public $path; |
||
78 | public $uuid; |
||
79 | public $hasCover; |
||
80 | public $relativePath; |
||
81 | public $seriesIndex; |
||
82 | public $comment; |
||
83 | public $rating; |
||
84 | public $datas = NULL; |
||
85 | public $authors = NULL; |
||
86 | public $publisher = NULL; |
||
87 | public $serie = NULL; |
||
88 | public $tags = NULL; |
||
89 | public $languages = NULL; |
||
90 | public $format = array (); |
||
91 | |||
92 | |||
93 | 69 | public function __construct($line) { |
|
110 | |||
111 | 40 | public function getEntryId () { |
|
114 | |||
115 | 4 | public static function getEntryIdByLetter ($startingLetter) { |
|
118 | |||
119 | 3 | public function getUri () { |
|
122 | |||
123 | 3 | public function getDetailUrl () { |
|
124 | 3 | $urlParam = $this->getUri (); |
|
125 | 3 | if (!is_null (GetUrlParam (DB))) { |
|
126 | $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB)); |
||
127 | $urlParam = addURLParameter ($urlParam, VL, GetUrlParam (VL, 0)); |
||
128 | } |
||
129 | 3 | return 'index.php' . $urlParam; |
|
130 | } |
||
131 | |||
132 | 41 | public function getTitle () { |
|
133 | 41 | return $this->title; |
|
134 | } |
||
135 | |||
136 | /* Other class (author, series, tag, ...) initialization and accessors */ |
||
137 | |||
138 | 46 | public function getAuthors () { |
|
139 | 46 | if (is_null ($this->authors)) { |
|
140 | 46 | $this->authors = Author::getAuthorByBookId ($this->id); |
|
141 | 46 | } |
|
142 | 46 | return $this->authors; |
|
143 | } |
||
144 | |||
145 | public function getAuthorsName () { |
||
148 | |||
149 | public function getAuthorsSort () { |
||
150 | return implode (", ", array_map (function ($author) { return $author->sort; }, $this->getAuthors ())); |
||
151 | } |
||
152 | |||
153 | 4 | public function getPublisher () { |
|
154 | 4 | if (is_null ($this->publisher)) { |
|
155 | 4 | $this->publisher = Publisher::getPublisherByBookId ($this->id); |
|
156 | 4 | } |
|
157 | 4 | return $this->publisher; |
|
158 | } |
||
159 | |||
160 | 45 | public function getSerie () { |
|
161 | 45 | if (is_null ($this->serie)) { |
|
162 | 45 | $this->serie = Serie::getSerieByBookId ($this->id); |
|
163 | 45 | } |
|
164 | 45 | return $this->serie; |
|
165 | } |
||
166 | |||
167 | 9 | public function getLanguages () { |
|
168 | 9 | $lang = array (); |
|
169 | 9 | $result = parent::getDb ()->prepare('select languages.lang_code |
|
|
|||
170 | from books_languages_link, languages |
||
171 | where books_languages_link.lang_code = languages.id |
||
172 | and book = ? |
||
173 | 9 | order by item_order'); |
|
174 | 9 | $result->execute (array ($this->id)); |
|
175 | 9 | while ($post = $result->fetchObject ()) |
|
176 | { |
||
177 | 9 | array_push ($lang, Language::getLanguageString($post->lang_code)); |
|
178 | 9 | } |
|
179 | 9 | return implode (", ", $lang); |
|
180 | } |
||
181 | |||
182 | 9 | public function getTags () { |
|
183 | 9 | if (is_null ($this->tags)) { |
|
184 | 9 | $this->tags = array (); |
|
185 | |||
186 | 9 | $result = parent::getDb ()->prepare('select tags.id as id, name |
|
187 | from books_tags_link, tags |
||
188 | where tag = tags.id |
||
189 | and book = ? |
||
190 | 9 | order by name'); |
|
191 | 9 | $result->execute (array ($this->id)); |
|
192 | 9 | while ($post = $result->fetchObject ()) |
|
193 | { |
||
194 | 9 | array_push ($this->tags, new Tag ($post)); |
|
195 | 9 | } |
|
196 | 9 | } |
|
197 | 9 | return $this->tags; |
|
198 | } |
||
199 | |||
200 | public function getTagsName () { |
||
201 | return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ())); |
||
202 | } |
||
203 | |||
204 | 59 | public function getDatas () |
|
205 | { |
||
206 | 59 | if (is_null ($this->datas)) { |
|
207 | 59 | $this->datas = Data::getDataByBook ($this); |
|
208 | 59 | } |
|
209 | 59 | return $this->datas; |
|
210 | } |
||
211 | |||
212 | /* End of other class (author, series, tag, ...) initialization and accessors */ |
||
213 | |||
214 | 56 | public static function getFilterString () { |
|
215 | 56 | $filter = getURLParam ("tag", NULL); |
|
216 | 56 | if (empty ($filter)) return ""; |
|
217 | |||
218 | 3 | $exists = true; |
|
219 | 3 | if (preg_match ("/^!(.*)$/", $filter, $matches)) { |
|
220 | 1 | $exists = false; |
|
221 | 1 | $filter = $matches[1]; |
|
222 | 1 | } |
|
223 | |||
224 | 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 . "')"; |
|
225 | |||
226 | 3 | if (!$exists) { |
|
227 | 1 | $result = "not " . $result; |
|
228 | 1 | } |
|
229 | |||
230 | 3 | return "and " . $result; |
|
231 | } |
||
232 | |||
233 | 4 | public function GetMostInterestingDataToSendToKindle () |
|
234 | { |
||
235 | 4 | $bestFormatForKindle = array ("EPUB", "PDF", "AZW3", "MOBI"); |
|
236 | 4 | $bestRank = -1; |
|
237 | 4 | $bestData = NULL; |
|
238 | 4 | foreach ($this->getDatas () as $data) { |
|
239 | 4 | $key = array_search ($data->format, $bestFormatForKindle); |
|
240 | 4 | if ($key !== false && $key > $bestRank) { |
|
241 | 4 | $bestRank = $key; |
|
242 | 4 | $bestData = $data; |
|
243 | 4 | } |
|
244 | 4 | } |
|
245 | 4 | return $bestData; |
|
246 | } |
||
247 | |||
248 | 3 | public function getDataById ($idData) |
|
249 | { |
||
250 | $reduced = array_filter ($this->getDatas (), function ($data) use ($idData) { |
||
251 | 3 | return $data->id == $idData; |
|
252 | 3 | }); |
|
253 | 3 | return reset ($reduced); |
|
254 | } |
||
255 | |||
256 | 7 | public function getRating () { |
|
257 | 7 | if (is_null ($this->rating) || $this->rating == 0) { |
|
258 | 2 | return ""; |
|
259 | } |
||
260 | 5 | $retour = ""; |
|
261 | 5 | for ($i = 0; $i < $this->rating / 2; $i++) { |
|
262 | 5 | $retour .= "★"; |
|
263 | 5 | } |
|
264 | 5 | for ($i = 0; $i < 5 - $this->rating / 2; $i++) { |
|
265 | 3 | $retour .= "☆"; |
|
266 | 3 | } |
|
267 | 5 | return $retour; |
|
268 | } |
||
269 | |||
270 | 8 | public function getPubDate () { |
|
271 | 8 | if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) { |
|
272 | return ""; |
||
273 | } |
||
274 | else { |
||
275 | 8 | return date ("Y", $this->pubdate); |
|
276 | } |
||
277 | } |
||
278 | |||
279 | 41 | public function getComment ($withSerie = true) { |
|
280 | 41 | $addition = ""; |
|
281 | 41 | $se = $this->getSerie (); |
|
282 | 41 | if (!is_null ($se) && $withSerie) { |
|
283 | 36 | $addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n"; |
|
284 | 36 | } |
|
285 | 41 | if (preg_match ("/<\/(div|p|a|span)>/", $this->comment)) |
|
286 | 41 | { |
|
287 | 35 | return $addition . html2xhtml ($this->comment); |
|
288 | } |
||
289 | else |
||
290 | { |
||
291 | 29 | return $addition . htmlspecialchars ($this->comment); |
|
292 | } |
||
293 | } |
||
294 | |||
295 | public function getDataFormat ($format) { |
||
296 | 11 | $reduced = array_filter ($this->getDatas (), function ($data) use ($format) { |
|
297 | 11 | return $data->format == $format; |
|
298 | 11 | }); |
|
299 | 11 | return reset ($reduced); |
|
300 | } |
||
301 | |||
302 | 69 | public function getFilePath ($extension, $idData = NULL, $relative = false) |
|
303 | { |
||
304 | 69 | if ($extension == "jpg") |
|
305 | 69 | { |
|
306 | 69 | $file = "cover.jpg"; |
|
307 | 69 | } |
|
308 | else |
||
309 | { |
||
310 | 2 | $data = $this->getDataById ($idData); |
|
311 | 2 | if (!$data) return NULL; |
|
312 | 2 | $file = $data->name . "." . strtolower ($data->format); |
|
313 | } |
||
314 | |||
315 | if ($relative) |
||
316 | 69 | { |
|
317 | 3 | return $this->relativePath."/".$file; |
|
318 | } |
||
319 | else |
||
320 | { |
||
321 | 69 | return $this->path."/".$file; |
|
322 | } |
||
323 | } |
||
324 | |||
325 | public function getUpdatedEpub ($idData) |
||
326 | { |
||
327 | global $config; |
||
328 | $data = $this->getDataById ($idData); |
||
329 | |||
330 | try |
||
331 | { |
||
332 | $epub = new EPub ($data->getLocalPath ()); |
||
333 | |||
334 | $epub->Title ($this->title); |
||
335 | $authorArray = array (); |
||
336 | foreach ($this->getAuthors() as $author) { |
||
337 | $authorArray [$author->sort] = $author->name; |
||
338 | } |
||
339 | $epub->Authors ($authorArray); |
||
340 | $epub->Language ($this->getLanguages ()); |
||
341 | $epub->Description ($this->getComment (false)); |
||
342 | $epub->Subjects ($this->getTagsName ()); |
||
343 | $epub->Cover2 ($this->getFilePath ("jpg"), "image/jpeg"); |
||
344 | $epub->Calibre ($this->uuid); |
||
345 | $se = $this->getSerie (); |
||
346 | if (!is_null ($se)) { |
||
347 | $epub->Serie ($se->name); |
||
348 | $epub->SerieIndex ($this->seriesIndex); |
||
349 | } |
||
350 | if ($config['cops_provide_kepub'] == "1" && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) { |
||
351 | $epub->updateForKepub (); |
||
352 | } |
||
353 | $epub->download ($data->getUpdatedFilenameEpub ()); |
||
354 | } |
||
355 | catch (Exception $e) |
||
356 | { |
||
357 | echo "Exception : " . $e->getMessage(); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | 3 | public function getThumbnail ($width, $height, $outputfile = NULL) { |
|
362 | 3 | if (is_null ($width) && is_null ($height)) { |
|
363 | 1 | return false; |
|
364 | } |
||
365 | |||
366 | 3 | $file = $this->getFilePath ("jpg"); |
|
367 | // get image size |
||
368 | 3 | if ($size = GetImageSize($file)) { |
|
369 | 3 | $w = $size[0]; |
|
370 | 3 | $h = $size[1]; |
|
371 | //set new size |
||
372 | 3 | if (!is_null ($width)) { |
|
373 | 2 | $nw = $width; |
|
374 | 2 | if ($nw >= $w) { return false; } |
|
375 | 1 | $nh = ($nw*$h)/$w; |
|
376 | 1 | } else { |
|
377 | 2 | $nh = $height; |
|
378 | 2 | if ($nh >= $h) { return false; } |
|
379 | 1 | $nw = ($nh*$w)/$h; |
|
380 | } |
||
381 | 2 | } else { |
|
382 | return false; |
||
383 | } |
||
384 | |||
385 | //draw the image |
||
386 | 2 | $src_img = imagecreatefromjpeg($file); |
|
387 | 2 | $dst_img = imagecreatetruecolor($nw,$nh); |
|
388 | 2 | imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image |
|
389 | 2 | imagejpeg($dst_img,$outputfile,80); |
|
390 | 2 | imagedestroy($src_img); |
|
391 | 2 | imagedestroy($dst_img); |
|
392 | |||
393 | 2 | return true; |
|
394 | } |
||
395 | |||
396 | 42 | public function getLinkArray () |
|
397 | { |
||
398 | 42 | $linkArray = array(); |
|
399 | |||
400 | 42 | if ($this->hasCover) |
|
401 | 42 | { |
|
402 | 18 | array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)); |
|
403 | |||
404 | 18 | array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL)); |
|
405 | 18 | } |
|
406 | |||
407 | 42 | foreach ($this->getDatas () as $data) |
|
408 | { |
||
409 | 42 | if ($data->isKnownType ()) |
|
410 | 42 | { |
|
411 | 42 | array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, $data->format)); |
|
412 | 42 | } |
|
413 | 42 | } |
|
414 | |||
415 | 42 | foreach ($this->getAuthors () as $author) { |
|
416 | 42 | array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name))); |
|
417 | 42 | } |
|
418 | |||
419 | 42 | $serie = $this->getSerie (); |
|
420 | 42 | if (!is_null ($serie)) { |
|
421 | 39 | array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name))); |
|
422 | 39 | } |
|
423 | |||
424 | 42 | return $linkArray; |
|
425 | } |
||
426 | |||
427 | |||
428 | 39 | public function getEntry () { |
|
429 | 39 | return new EntryBook ($this->getTitle (), $this->getEntryId (), |
|
430 | 39 | $this->getComment (), "text/html", |
|
431 | 39 | $this->getLinkArray (), $this); |
|
432 | } |
||
433 | |||
434 | 3 | public static function getBookCount($database = NULL) { |
|
437 | |||
438 | 10 | public static function getCount() { |
|
439 | 10 | global $config; |
|
440 | 10 | $nBooks = parent::executeQuerySingle ('select count(*) from books'); |
|
441 | 10 | $result = array(); |
|
442 | 10 | $entry = new Entry (localize ("allbooks.title"), |
|
443 | 10 | self::ALL_BOOKS_ID, |
|
444 | 10 | str_format (localize ("allbooks.alphabetical", $nBooks), $nBooks), "text", |
|
445 | 10 | array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)), "", $nBooks); |
|
446 | 10 | array_push ($result, $entry); |
|
447 | 10 | if ($config['cops_recentbooks_limit'] > 0) { |
|
448 | 10 | $entry = new Entry (localize ("recent.title"), |
|
449 | 10 | self::ALL_RECENT_BOOKS_ID, |
|
450 | 10 | str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text", |
|
451 | 10 | array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)), "", $config['cops_recentbooks_limit']); |
|
452 | 10 | array_push ($result, $entry); |
|
453 | 10 | } |
|
454 | 10 | return $result; |
|
455 | } |
||
456 | |||
457 | 8 | public static function getBooksByAuthor($authorId, $n) { |
|
460 | |||
461 | 1 | public static function getBooksByRating($ratingId, $n) { |
|
464 | |||
465 | 2 | public static function getBooksByPublisher($publisherId, $n) { |
|
468 | |||
469 | 2 | public static function getBooksBySeries($serieId, $n) { |
|
472 | |||
473 | 2 | public static function getBooksByTag($tagId, $n) { |
|
476 | |||
477 | 2 | public static function getBooksByLanguage($languageId, $n) { |
|
480 | |||
481 | 3 | public static function getBooksByCustom($customId, $id, $n) { |
|
482 | 3 | $query = str_format (self::SQL_BOOKS_BY_CUSTOM, "{0}", "{1}", CustomColumn::getTableLinkName ($customId), CustomColumn::getTableLinkColumn ($customId)); |
|
483 | 3 | return self::getEntryArray ($query, array ($id), $n); |
|
484 | } |
||
485 | |||
486 | 30 | public static function getBookById($bookId) { |
|
487 | 30 | $result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ' |
|
488 | 30 | from books ' . self::SQL_BOOKS_LEFT_JOIN . ' |
|
489 | 30 | where books.id = ?'); |
|
490 | 30 | $result->execute (array ($bookId)); |
|
491 | 30 | while ($post = $result->fetchObject ()) |
|
492 | { |
||
493 | 29 | $book = new Book ($post); |
|
494 | 29 | return $book; |
|
495 | } |
||
496 | 1 | return NULL; |
|
497 | } |
||
498 | |||
499 | 1 | public static function getBookByDataId($dataId) { |
|
500 | 1 | $result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ', data.name, data.format |
|
501 | 1 | from data, books ' . self::SQL_BOOKS_LEFT_JOIN . ' |
|
502 | 1 | where data.book = books.id and data.id = ?'); |
|
503 | 1 | $result->execute (array ($dataId)); |
|
504 | 1 | while ($post = $result->fetchObject ()) |
|
505 | { |
||
506 | 1 | $book = new Book ($post); |
|
507 | 1 | $data = new Data ($post, $book); |
|
508 | 1 | $data->id = $dataId; |
|
509 | 1 | $book->datas = array ($data); |
|
510 | 1 | return $book; |
|
511 | } |
||
512 | return NULL; |
||
513 | } |
||
514 | |||
515 | 2 | public static function getBooksByQuery($query, $n, $database = NULL, $numberPerPage = NULL) { |
|
516 | 2 | $i = 0; |
|
517 | 2 | $critArray = array (); |
|
518 | 2 | foreach (array (PageQueryResult::SCOPE_AUTHOR, |
|
519 | 2 | PageQueryResult::SCOPE_TAG, |
|
520 | 2 | PageQueryResult::SCOPE_SERIES, |
|
521 | 2 | PageQueryResult::SCOPE_PUBLISHER, |
|
522 | 2 | PageQueryResult::SCOPE_BOOK) as $key) { |
|
523 | 2 | if (in_array($key, getCurrentOption ('ignored_categories')) || |
|
524 | 2 | (!array_key_exists ($key, $query) && !array_key_exists ("all", $query))) { |
|
525 | $critArray [$i] = self::BAD_SEARCH; |
||
526 | } |
||
527 | else { |
||
528 | 2 | if (array_key_exists ($key, $query)) { |
|
529 | $critArray [$i] = $query [$key]; |
||
530 | } else { |
||
531 | 2 | $critArray [$i] = $query ["all"]; |
|
532 | } |
||
533 | } |
||
534 | 2 | $i++; |
|
535 | 2 | } |
|
536 | 2 | return self::getEntryArray (self::SQL_BOOKS_QUERY, $critArray, $n, $database, $numberPerPage); |
|
537 | } |
||
538 | |||
539 | 1 | public static function getBooks($n) { |
|
540 | 1 | list ($entryArray, $totalNumber) = self::getEntryArray (self::SQL_BOOKS_ALL , array (), $n); |
|
541 | 1 | return array ($entryArray, $totalNumber); |
|
542 | } |
||
543 | |||
544 | 3 | public static function getAllBooks() { |
|
558 | |||
559 | 25 | public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) { |
|
562 | |||
563 | 53 | public static function getEntryArray ($query, $params, $n, $database = NULL, $numberPerPage = NULL) { |
|
564 | 53 | list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database, $numberPerPage); |
|
565 | 53 | $entryArray = array(); |
|
566 | 53 | while ($post = $result->fetchObject ()) |
|
567 | { |
||
568 | 39 | $book = new Book ($post); |
|
569 | 39 | array_push ($entryArray, $book->getEntry ()); |
|
570 | 39 | } |
|
571 | 53 | return array ($entryArray, $totalNumber); |
|
572 | } |
||
573 | |||
574 | |||
575 | 5 | public static function getAllRecentBooks() { |
|
576 | 5 | global $config; |
|
577 | 5 | list ($entryArray, ) = self::getEntryArray (self::SQL_BOOKS_RECENT . $config['cops_recentbooks_limit'], array (), -1); |
|
578 | 5 | return $entryArray; |
|
579 | } |
||
580 | |||
581 | } |
||
582 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.