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 |
||
| 54 | class Book extends Base { |
||
| 55 | const ALL_BOOKS_UUID = "urn:uuid"; |
||
| 56 | const ALL_BOOKS_ID = "cops:books"; |
||
| 57 | const ALL_RECENT_BOOKS_ID = "cops:recentbooks"; |
||
| 58 | const BOOK_COLUMNS = BOOK_COLUMNS; |
||
| 59 | |||
| 60 | const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN; |
||
| 61 | const SQL_BOOKS_ALL = SQL_BOOKS_ALL; |
||
| 62 | const SQL_BOOKS_BY_PUBLISHER = SQL_BOOKS_BY_PUBLISHER; |
||
| 63 | const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER; |
||
| 64 | const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR; |
||
| 65 | const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE; |
||
| 66 | const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG; |
||
| 67 | const SQL_BOOKS_BY_LANGUAGE = SQL_BOOKS_BY_LANGUAGE; |
||
| 68 | const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM; |
||
| 69 | const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY; |
||
| 70 | const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT; |
||
| 71 | const SQL_BOOKS_BY_RATING = SQL_BOOKS_BY_RATING; |
||
| 72 | |||
| 73 | const BAD_SEARCH = "QQQQQ"; |
||
| 74 | |||
| 75 | public $id; |
||
| 76 | public $title; |
||
| 77 | public $timestamp; |
||
| 78 | public $pubdate; |
||
| 79 | public $path; |
||
| 80 | public $uuid; |
||
| 81 | public $hasCover; |
||
| 82 | public $relativePath; |
||
| 83 | public $seriesIndex; |
||
| 84 | public $comment; |
||
| 85 | public $rating; |
||
| 86 | public $datas = NULL; |
||
| 87 | public $authors = NULL; |
||
| 88 | public $publisher = NULL; |
||
| 89 | public $serie = NULL; |
||
| 90 | public $tags = NULL; |
||
| 91 | public $languages = NULL; |
||
| 92 | public $format = array (); |
||
| 93 | 69 | ||
| 94 | 69 | ||
| 95 | 69 | public function __construct($line) { |
|
| 112 | 40 | ||
| 113 | public function getEntryId () { |
||
| 116 | 4 | ||
| 117 | public static function getEntryIdByLetter ($startingLetter) { |
||
| 120 | 3 | ||
| 121 | public function getUri () { |
||
| 124 | 3 | ||
| 125 | 3 | public function getDetailUrl () { |
|
| 126 | 3 | $urlParam = $this->getUri (); |
|
| 127 | if (!is_null (GetUrlParam (DB))) { |
||
| 128 | $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB)); |
||
| 129 | 41 | $urlParam = addURLParameter ($urlParam, VL, GetUrlParam (VL, 0)); |
|
| 130 | 41 | } |
|
| 131 | return 'index.php' . $urlParam; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getTitle () { |
||
| 135 | 46 | return $this->title; |
|
| 136 | 46 | } |
|
| 137 | 46 | ||
| 138 | 46 | /* Other class (author, series, tag, ...) initialization and accessors */ |
|
| 139 | 46 | ||
| 140 | public function getAuthors () { |
||
| 141 | if (is_null ($this->authors)) { |
||
| 142 | $this->authors = Author::getAuthorByBookId ($this->id); |
||
| 143 | } |
||
| 144 | return $this->authors; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getAuthorsName () { |
||
| 150 | 4 | ||
| 151 | 4 | public function getAuthorsSort () { |
|
| 152 | 4 | return implode (", ", array_map (function ($author) { return $author->sort; }, $this->getAuthors ())); |
|
| 153 | 4 | } |
|
| 154 | 4 | ||
| 155 | public function getPublisher () { |
||
| 161 | 45 | ||
| 162 | public function getSerie () { |
||
| 163 | if (is_null ($this->serie)) { |
||
| 164 | 9 | $this->serie = Serie::getSerieByBookId ($this->id); |
|
| 165 | 9 | } |
|
| 166 | 9 | return $this->serie; |
|
| 167 | } |
||
| 168 | |||
| 169 | public function getLanguages () { |
||
| 170 | 9 | $lang = array (); |
|
| 171 | 9 | $result = parent::getDb ()->prepare('select languages.lang_code |
|
|
|
|||
| 172 | 9 | from books_languages_link, languages |
|
| 173 | where books_languages_link.lang_code = languages.id |
||
| 174 | 9 | and book = ? |
|
| 175 | 9 | order by item_order'); |
|
| 176 | 9 | $result->execute (array ($this->id)); |
|
| 177 | while ($post = $result->fetchObject ()) |
||
| 178 | { |
||
| 179 | 9 | array_push ($lang, Language::getLanguageString($post->lang_code)); |
|
| 180 | 9 | } |
|
| 181 | 9 | return implode (", ", $lang); |
|
| 182 | } |
||
| 183 | 9 | ||
| 184 | public function getTags () { |
||
| 185 | if (is_null ($this->tags)) { |
||
| 186 | $this->tags = array (); |
||
| 187 | 9 | ||
| 188 | 9 | $result = parent::getDb ()->prepare('select tags.id as id, name |
|
| 189 | 9 | from books_tags_link, tags |
|
| 190 | where tag = tags.id |
||
| 191 | 9 | and book = ? |
|
| 192 | 9 | order by name'); |
|
| 193 | 9 | $result->execute (array ($this->id)); |
|
| 194 | 9 | while ($post = $result->fetchObject ()) |
|
| 195 | { |
||
| 196 | array_push ($this->tags, new Tag ($post)); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | return $this->tags; |
||
| 200 | } |
||
| 201 | 59 | ||
| 202 | public function getTagsName () { |
||
| 203 | 59 | return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ())); |
|
| 204 | 59 | } |
|
| 205 | 59 | ||
| 206 | 59 | public function getDatas () |
|
| 207 | { |
||
| 208 | if (is_null ($this->datas)) { |
||
| 209 | $this->datas = Data::getDataByBook ($this); |
||
| 210 | } |
||
| 211 | 56 | return $this->datas; |
|
| 212 | 56 | } |
|
| 213 | 56 | ||
| 214 | /* End of other class (author, series, tag, ...) initialization and accessors */ |
||
| 215 | 3 | ||
| 216 | 3 | public static function getFilterString () { |
|
| 234 | 4 | ||
| 235 | 4 | public function GetMostInterestingDataToSendToKindle () |
|
| 236 | 4 | { |
|
| 237 | 4 | $bestFormatForKindle = array ("EPUB", "PDF", "AZW3", "MOBI"); |
|
| 238 | 4 | $bestRank = -1; |
|
| 239 | 4 | $bestData = NULL; |
|
| 240 | 4 | foreach ($this->getDatas () as $data) { |
|
| 241 | 4 | $key = array_search ($data->format, $bestFormatForKindle); |
|
| 242 | 4 | if ($key !== false && $key > $bestRank) { |
|
| 243 | $bestRank = $key; |
||
| 244 | $bestData = $data; |
||
| 245 | 3 | } |
|
| 246 | } |
||
| 247 | return $bestData; |
||
| 248 | 3 | } |
|
| 249 | 3 | ||
| 250 | 3 | public function getDataById ($idData) |
|
| 257 | 5 | ||
| 258 | 5 | public function getRating () { |
|
| 259 | 5 | if (is_null ($this->rating) || $this->rating == 0) { |
|
| 260 | 5 | return ""; |
|
| 261 | 5 | } |
|
| 262 | 3 | $retour = ""; |
|
| 263 | 3 | for ($i = 0; $i < $this->rating / 2; $i++) { |
|
| 264 | 5 | $retour .= "★"; |
|
| 265 | } |
||
| 266 | for ($i = 0; $i < 5 - $this->rating / 2; $i++) { |
||
| 267 | 8 | $retour .= "☆"; |
|
| 268 | 8 | } |
|
| 269 | return $retour; |
||
| 270 | } |
||
| 271 | |||
| 272 | 8 | public function getPubDate () { |
|
| 273 | if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) { |
||
| 274 | return ""; |
||
| 275 | } |
||
| 276 | 41 | else { |
|
| 277 | 41 | return date ("Y", $this->pubdate); |
|
| 278 | 41 | } |
|
| 279 | 41 | } |
|
| 280 | 36 | ||
| 281 | 36 | public function getComment ($withSerie = true) { |
|
| 296 | 11 | ||
| 297 | public function getDataFormat ($format) { |
||
| 298 | $reduced = array_filter ($this->getDatas (), function ($data) use ($format) { |
||
| 299 | 69 | return $data->format == $format; |
|
| 300 | }); |
||
| 301 | 69 | return reset ($reduced); |
|
| 302 | 69 | } |
|
| 303 | 69 | ||
| 304 | 69 | public function getFilePath ($extension, $idData = NULL, $relative = false) |
|
| 305 | { |
||
| 306 | if ($extension == "jpg") |
||
| 307 | 2 | { |
|
| 308 | 2 | $file = "cover.jpg"; |
|
| 309 | 2 | } |
|
| 310 | else |
||
| 311 | { |
||
| 312 | $data = $this->getDataById ($idData); |
||
| 313 | 69 | if (!$data) return NULL; |
|
| 314 | 3 | $file = $data->name . "." . strtolower ($data->format); |
|
| 315 | } |
||
| 316 | |||
| 317 | if ($relative) |
||
| 318 | 69 | { |
|
| 319 | return $this->relativePath."/".$file; |
||
| 320 | } |
||
| 321 | else |
||
| 322 | { |
||
| 323 | return $this->path."/".$file; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getUpdatedEpub ($idData) |
||
| 328 | { |
||
| 329 | global $config; |
||
| 330 | $data = $this->getDataById ($idData); |
||
| 331 | |||
| 332 | try |
||
| 333 | { |
||
| 334 | $epub = new EPub ($data->getLocalPath ()); |
||
| 335 | |||
| 336 | $epub->Title ($this->title); |
||
| 337 | $authorArray = array (); |
||
| 338 | foreach ($this->getAuthors() as $author) { |
||
| 339 | $authorArray [$author->sort] = $author->name; |
||
| 340 | } |
||
| 341 | $epub->Authors ($authorArray); |
||
| 342 | $epub->Language ($this->getLanguages ()); |
||
| 343 | $epub->Description ($this->getComment (false)); |
||
| 344 | $epub->Subjects ($this->getTagsName ()); |
||
| 345 | $epub->Cover2 ($this->getFilePath ("jpg"), "image/jpeg"); |
||
| 346 | $epub->Calibre ($this->uuid); |
||
| 347 | $se = $this->getSerie (); |
||
| 348 | if (!is_null ($se)) { |
||
| 349 | $epub->Serie ($se->name); |
||
| 350 | $epub->SerieIndex ($this->seriesIndex); |
||
| 351 | } |
||
| 352 | if ($config['cops_provide_kepub'] == "1" && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) { |
||
| 353 | $epub->updateForKepub (); |
||
| 354 | } |
||
| 355 | $epub->download ($data->getUpdatedFilenameEpub ()); |
||
| 356 | } |
||
| 357 | catch (Exception $e) |
||
| 358 | 3 | { |
|
| 359 | 3 | echo "Exception : " . $e->getMessage(); |
|
| 360 | 1 | } |
|
| 361 | } |
||
| 362 | |||
| 363 | 3 | public function getThumbnail ($width, $height, $outputfile = NULL) { |
|
| 397 | 42 | ||
| 398 | 42 | public function getLinkArray () |
|
| 399 | 18 | { |
|
| 400 | $linkArray = array(); |
||
| 401 | 18 | ||
| 402 | 18 | if ($this->hasCover) |
|
| 403 | { |
||
| 404 | 42 | array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)); |
|
| 405 | |||
| 406 | 42 | array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL)); |
|
| 407 | 42 | } |
|
| 408 | 42 | ||
| 409 | 42 | foreach ($this->getDatas () as $data) |
|
| 410 | 42 | { |
|
| 411 | if ($data->isKnownType ()) |
||
| 412 | 42 | { |
|
| 413 | 42 | array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, $data->format)); |
|
| 414 | 42 | } |
|
| 415 | } |
||
| 416 | 42 | ||
| 417 | 42 | foreach ($this->getAuthors () as $author) { |
|
| 418 | 39 | array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name))); |
|
| 419 | 39 | } |
|
| 420 | |||
| 421 | 42 | $serie = $this->getSerie (); |
|
| 422 | if (!is_null ($serie)) { |
||
| 423 | array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name))); |
||
| 424 | } |
||
| 425 | 39 | ||
| 426 | 39 | return $linkArray; |
|
| 427 | 39 | } |
|
| 428 | 39 | ||
| 429 | |||
| 430 | public function getEntry () { |
||
| 431 | 3 | return new EntryBook ($this->getTitle (), $this->getEntryId (), |
|
| 432 | 3 | $this->getComment (), "text/html", |
|
| 433 | $this->getLinkArray (), $this); |
||
| 434 | } |
||
| 435 | 10 | ||
| 436 | 10 | public static function getBookCount($database = NULL, $virtualLib = NULL) { |
|
| 437 | 10 | return parent::executeFilteredQuerySingle('select count(*) from ({0}) as filter', $database, $virtualLib); |
|
| 438 | 10 | } |
|
| 439 | 10 | ||
| 440 | 10 | public static function getCount() { |
|
| 441 | 10 | global $config; |
|
| 442 | 10 | $nBooks = self::getBookCount(); |
|
| 443 | 10 | $result = array(); |
|
| 444 | 10 | $entry = new Entry (localize ("allbooks.title"), |
|
| 445 | 10 | self::ALL_BOOKS_ID, |
|
| 446 | 10 | str_format (localize ("allbooks.alphabetical", $nBooks), $nBooks), "text", |
|
| 447 | 10 | array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)), "", $nBooks); |
|
| 448 | 10 | array_push ($result, $entry); |
|
| 449 | 10 | if ($config['cops_recentbooks_limit'] > 0) { |
|
| 450 | 10 | $entry = new Entry (localize ("recent.title"), |
|
| 451 | 10 | self::ALL_RECENT_BOOKS_ID, |
|
| 452 | str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text", |
||
| 453 | array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)), "", $config['cops_recentbooks_limit']); |
||
| 454 | 8 | array_push ($result, $entry); |
|
| 455 | 8 | } |
|
| 456 | return $result; |
||
| 457 | } |
||
| 458 | 1 | ||
| 459 | 1 | public static function getBooksByAuthor($authorId, $n) { |
|
| 462 | 2 | ||
| 463 | 2 | public static function getBooksByRating($ratingId, $n) { |
|
| 466 | 2 | ||
| 467 | 2 | public static function getBooksByPublisher($publisherId, $n) { |
|
| 470 | 2 | ||
| 471 | 2 | public static function getBooksBySeries($serieId, $n) { |
|
| 474 | 2 | ||
| 475 | 2 | public static function getBooksByTag($tagId, $n) { |
|
| 478 | 3 | ||
| 479 | 3 | public static function getBooksByLanguage($languageId, $n) { |
|
| 480 | 3 | return self::getEntryArray (self::SQL_BOOKS_BY_LANGUAGE, array ($languageId), $n); |
|
| 481 | } |
||
| 482 | |||
| 483 | 30 | public static function getBooksByCustom($customId, $id, $n) { |
|
| 484 | 30 | $query = str_format (self::SQL_BOOKS_BY_CUSTOM, "{0}", CustomColumn::getTableLinkName ($customId), CustomColumn::getTableLinkColumn ($customId)); |
|
| 485 | 30 | return self::getEntryArray ($query, array ($id), $n); |
|
| 486 | 30 | } |
|
| 487 | 30 | ||
| 488 | 30 | public static function getBookById($bookId) { |
|
| 500 | 1 | ||
| 501 | 1 | public static function getBookByDataId($dataId) { |
|
| 516 | 2 | ||
| 517 | 2 | public static function getBooksByQuery($query, $n, $database = NULL, $virtualLib = NULL, $numberPerPage = NULL) { |
|
| 518 | 2 | $i = 0; |
|
| 519 | 2 | $critArray = array (); |
|
| 520 | 2 | foreach (array (PageQueryResult::SCOPE_AUTHOR, |
|
| 521 | 2 | PageQueryResult::SCOPE_TAG, |
|
| 522 | PageQueryResult::SCOPE_SERIES, |
||
| 523 | PageQueryResult::SCOPE_PUBLISHER, |
||
| 524 | PageQueryResult::SCOPE_BOOK) as $key) { |
||
| 525 | 2 | if (in_array($key, getCurrentOption ('ignored_categories')) || |
|
| 526 | (!array_key_exists ($key, $query) && !array_key_exists ("all", $query))) { |
||
| 527 | $critArray [$i] = self::BAD_SEARCH; |
||
| 528 | 2 | } |
|
| 529 | else { |
||
| 530 | if (array_key_exists ($key, $query)) { |
||
| 531 | 2 | $critArray [$i] = $query [$key]; |
|
| 532 | 2 | } else { |
|
| 533 | 2 | $critArray [$i] = $query ["all"]; |
|
| 534 | } |
||
| 535 | } |
||
| 536 | 1 | $i++; |
|
| 537 | 1 | } |
|
| 538 | 1 | return self::getEntryArray (self::SQL_BOOKS_QUERY, $critArray, $n, $database, $virtualLib, $numberPerPage); |
|
| 539 | } |
||
| 540 | |||
| 541 | 3 | public static function getBooks($n) { |
|
| 545 | 3 | ||
| 546 | 3 | public static function getAllBooks() { |
|
| 560 | 53 | ||
| 561 | 53 | public static function getBooksByStartingLetter($letter, $n, $database = NULL, $virtualLib = NULL, $numberPerPage = NULL) { |
|
| 562 | 53 | return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n, $database, $virtualLib, $numberPerPage); |
|
| 563 | 53 | } |
|
| 564 | |||
| 565 | 39 | public static function getEntryArray ($query, $params, $n, $database = NULL, $virtualLib = NULL, $numberPerPage = NULL) { |
|
| 566 | 39 | // TODO: include getFilterString() again. |
|
| 567 | 39 | list ($totalNumber, $result) = parent::executeFilteredQuery ($query, $params, $n, $database, $virtualLib, $numberPerPage); |
|
| 568 | 53 | $entryArray = array(); |
|
| 569 | while ($post = $result->fetchObject ()) |
||
| 570 | { |
||
| 571 | $book = new Book ($post); |
||
| 572 | 5 | array_push ($entryArray, $book->getEntry ()); |
|
| 573 | 5 | } |
|
| 574 | 5 | return array ($entryArray, $totalNumber); |
|
| 575 | 5 | } |
|
| 576 | |||
| 577 | |||
| 578 | public static function getAllRecentBooks() { |
||
| 579 | global $config; |
||
| 580 | list ($entryArray, ) = self::getEntryArray (self::SQL_BOOKS_RECENT . $config['cops_recentbooks_limit'], array (), -1); |
||
| 581 | return $entryArray; |
||
| 582 | } |
||
| 583 | |||
| 584 | } |
||
| 585 |
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 theSoncalls the wrong method in the parent class.