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 | 75 | public function __construct($line) { |
|
| 94 | 75 | $this->id = $line->id; |
|
| 95 | 75 | $this->title = $line->title; |
|
| 96 | 75 | $this->timestamp = strtotime ($line->timestamp); |
|
| 97 | 75 | $this->pubdate = $line->pubdate; |
|
| 98 | 75 | $this->path = Base::getDbDirectory () . $line->path; |
|
| 99 | 75 | $this->relativePath = $line->path; |
|
| 100 | 75 | $this->seriesIndex = $line->series_index; |
|
| 101 | 75 | $this->comment = $line->comment; |
|
| 102 | 75 | $this->uuid = $line->uuid; |
|
| 103 | 75 | $this->hasCover = $line->has_cover; |
|
| 104 | 75 | if (!file_exists ($this->getFilePath ("jpg"))) { |
|
| 105 | // double check |
||
| 106 | 41 | $this->hasCover = 0; |
|
| 107 | 41 | } |
|
| 108 | 75 | $this->rating = $line->rating; |
|
| 109 | 75 | } |
|
| 110 | |||
| 111 | 40 | public function getEntryId () { |
|
| 112 | 40 | return self::ALL_BOOKS_UUID.":".$this->uuid; |
|
| 113 | } |
||
| 114 | |||
| 115 | 4 | public static function getEntryIdByLetter ($startingLetter) { |
|
| 116 | 4 | return self::ALL_BOOKS_ID.":letter:".$startingLetter; |
|
| 117 | } |
||
| 118 | |||
| 119 | 3 | public function getUri () { |
|
| 120 | 3 | return "?page=".parent::PAGE_BOOK_DETAIL."&id=$this->id"; |
|
| 121 | } |
||
| 122 | |||
| 123 | 3 | public function getDetailUrl () { |
|
| 124 | 3 | $urlParam = $this->getUri (); |
|
| 125 | 3 | if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB)); |
|
| 126 | 3 | return 'index.php' . $urlParam; |
|
| 127 | } |
||
| 128 | |||
| 129 | 41 | public function getTitle () { |
|
| 130 | 41 | return $this->title; |
|
| 131 | } |
||
| 132 | |||
| 133 | /* Other class (author, series, tag, ...) initialization and accessors */ |
||
| 134 | |||
| 135 | 46 | public function getAuthors () { |
|
| 136 | 46 | if (is_null ($this->authors)) { |
|
| 137 | 46 | $this->authors = Author::getAuthorByBookId ($this->id); |
|
| 138 | 46 | } |
|
| 139 | 46 | return $this->authors; |
|
| 140 | } |
||
| 141 | |||
| 142 | public function getAuthorsName () { |
||
| 143 | return implode (", ", array_map (function ($author) { return $author->name; }, $this->getAuthors ())); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getAuthorsSort () { |
||
| 147 | return implode (", ", array_map (function ($author) { return $author->sort; }, $this->getAuthors ())); |
||
| 148 | } |
||
| 149 | |||
| 150 | 4 | public function getPublisher () { |
|
| 151 | 4 | if (is_null ($this->publisher)) { |
|
| 152 | 4 | $this->publisher = Publisher::getPublisherByBookId ($this->id); |
|
| 153 | 4 | } |
|
| 154 | 4 | return $this->publisher; |
|
| 155 | } |
||
| 156 | |||
| 157 | 45 | public function getSerie () { |
|
| 158 | 45 | if (is_null ($this->serie)) { |
|
| 159 | 45 | $this->serie = Serie::getSerieByBookId ($this->id); |
|
| 160 | 45 | } |
|
| 161 | 45 | return $this->serie; |
|
| 162 | } |
||
| 163 | |||
| 164 | 9 | public function getLanguages () { |
|
| 165 | 9 | $lang = array (); |
|
| 166 | 9 | $result = parent::getDb ()->prepare('select languages.lang_code |
|
|
|
|||
| 167 | from books_languages_link, languages |
||
| 168 | where books_languages_link.lang_code = languages.id |
||
| 169 | and book = ? |
||
| 170 | 9 | order by item_order'); |
|
| 171 | 9 | $result->execute (array ($this->id)); |
|
| 172 | 9 | while ($post = $result->fetchObject ()) |
|
| 173 | { |
||
| 174 | 9 | array_push ($lang, Language::getLanguageString($post->lang_code)); |
|
| 175 | 9 | } |
|
| 176 | 9 | return implode (", ", $lang); |
|
| 177 | } |
||
| 178 | |||
| 179 | 9 | public function getTags () { |
|
| 180 | 9 | if (is_null ($this->tags)) { |
|
| 181 | 9 | $this->tags = array (); |
|
| 182 | |||
| 183 | 9 | $result = parent::getDb ()->prepare('select tags.id as id, name |
|
| 184 | from books_tags_link, tags |
||
| 185 | where tag = tags.id |
||
| 186 | and book = ? |
||
| 187 | 9 | order by name'); |
|
| 188 | 9 | $result->execute (array ($this->id)); |
|
| 189 | 9 | while ($post = $result->fetchObject ()) |
|
| 190 | { |
||
| 191 | 9 | array_push ($this->tags, new Tag ($post)); |
|
| 192 | 9 | } |
|
| 193 | 9 | } |
|
| 194 | 9 | return $this->tags; |
|
| 195 | } |
||
| 196 | |||
| 197 | public function getTagsName () { |
||
| 198 | return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ())); |
||
| 199 | } |
||
| 200 | |||
| 201 | 59 | public function getDatas () |
|
| 202 | { |
||
| 203 | 59 | if (is_null ($this->datas)) { |
|
| 204 | 59 | $this->datas = Data::getDataByBook ($this); |
|
| 205 | 59 | } |
|
| 206 | 59 | return $this->datas; |
|
| 207 | } |
||
| 208 | |||
| 209 | /* End of other class (author, series, tag, ...) initialization and accessors */ |
||
| 210 | |||
| 211 | 56 | public static function getFilterString () { |
|
| 212 | 56 | $filter = getURLParam ("tag", NULL); |
|
| 213 | 56 | if (empty ($filter)) return ""; |
|
| 214 | |||
| 215 | 3 | $exists = true; |
|
| 216 | 3 | if (preg_match ("/^!(.*)$/", $filter, $matches)) { |
|
| 217 | 1 | $exists = false; |
|
| 218 | 1 | $filter = $matches[1]; |
|
| 219 | 1 | } |
|
| 220 | |||
| 221 | 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 . "')"; |
|
| 222 | |||
| 223 | 3 | if (!$exists) { |
|
| 224 | 1 | $result = "not " . $result; |
|
| 225 | 1 | } |
|
| 226 | |||
| 227 | 3 | return "and " . $result; |
|
| 228 | } |
||
| 229 | |||
| 230 | 4 | public function GetMostInterestingDataToSendToKindle () |
|
| 231 | { |
||
| 232 | 4 | $bestFormatForKindle = array ("EPUB", "PDF", "AZW3", "MOBI"); |
|
| 233 | 4 | $bestRank = -1; |
|
| 234 | 4 | $bestData = NULL; |
|
| 235 | 4 | foreach ($this->getDatas () as $data) { |
|
| 236 | 4 | $key = array_search ($data->format, $bestFormatForKindle); |
|
| 237 | 4 | if ($key !== false && $key > $bestRank) { |
|
| 238 | 4 | $bestRank = $key; |
|
| 239 | 4 | $bestData = $data; |
|
| 240 | 4 | } |
|
| 241 | 4 | } |
|
| 242 | 4 | return $bestData; |
|
| 243 | } |
||
| 244 | |||
| 245 | 3 | public function getDataById ($idData) |
|
| 246 | { |
||
| 247 | $reduced = array_filter ($this->getDatas (), function ($data) use ($idData) { |
||
| 248 | 3 | return $data->id == $idData; |
|
| 249 | 3 | }); |
|
| 250 | 3 | return reset ($reduced); |
|
| 251 | } |
||
| 252 | |||
| 253 | 7 | public function getRating () { |
|
| 254 | 7 | if (is_null ($this->rating) || $this->rating == 0) { |
|
| 255 | 2 | return ""; |
|
| 256 | } |
||
| 257 | 5 | $retour = ""; |
|
| 258 | 5 | for ($i = 0; $i < $this->rating / 2; $i++) { |
|
| 259 | 5 | $retour .= "★"; |
|
| 260 | 5 | } |
|
| 261 | 5 | for ($i = 0; $i < 5 - $this->rating / 2; $i++) { |
|
| 262 | 3 | $retour .= "☆"; |
|
| 263 | 3 | } |
|
| 264 | 5 | return $retour; |
|
| 265 | } |
||
| 266 | |||
| 267 | 14 | public function getPubDate () { |
|
| 277 | |||
| 278 | 41 | public function getComment ($withSerie = true) { |
|
| 279 | 41 | $addition = ""; |
|
| 280 | 41 | $se = $this->getSerie (); |
|
| 281 | 41 | if (!is_null ($se) && $withSerie) { |
|
| 282 | 36 | $addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n"; |
|
| 283 | 36 | } |
|
| 284 | 41 | if (preg_match ("/<\/(div|p|a|span)>/", $this->comment)) |
|
| 285 | 41 | { |
|
| 286 | 35 | return $addition . html2xhtml ($this->comment); |
|
| 287 | } |
||
| 288 | else |
||
| 289 | { |
||
| 290 | 29 | return $addition . htmlspecialchars ($this->comment); |
|
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | public function getDataFormat ($format) { |
||
| 295 | 11 | $reduced = array_filter ($this->getDatas (), function ($data) use ($format) { |
|
| 296 | 11 | return $data->format == $format; |
|
| 297 | 11 | }); |
|
| 300 | |||
| 301 | 75 | public function getFilePath ($extension, $idData = NULL, $relative = false) |
|
| 323 | |||
| 324 | public function getUpdatedEpub ($idData) |
||
| 359 | |||
| 360 | 3 | public function getThumbnail ($width, $height, $outputfile = NULL) { |
|
| 394 | |||
| 395 | 42 | public function getLinkArray () |
|
| 425 | |||
| 426 | |||
| 427 | 39 | public function getEntry () { |
|
| 432 | |||
| 433 | 3 | public static function getBookCount($database = NULL) { |
|
| 436 | |||
| 437 | 10 | public static function getCount() { |
|
| 455 | |||
| 456 | 8 | public static function getBooksByAuthor($authorId, $n) { |
|
| 459 | |||
| 460 | 1 | public static function getBooksByRating($ratingId, $n) { |
|
| 463 | |||
| 464 | 2 | public static function getBooksByPublisher($publisherId, $n) { |
|
| 467 | |||
| 468 | 2 | public static function getBooksBySeries($serieId, $n) { |
|
| 471 | |||
| 472 | 2 | public static function getBooksByTag($tagId, $n) { |
|
| 475 | |||
| 476 | 2 | public static function getBooksByLanguage($languageId, $n) { |
|
| 479 | |||
| 480 | 3 | public static function getBooksByCustom($customId, $id, $n) { |
|
| 484 | |||
| 485 | 36 | public static function getBookById($bookId) { |
|
| 497 | |||
| 498 | 1 | public static function getBookByDataId($dataId) { |
|
| 513 | |||
| 514 | 2 | public static function getBooksByQuery($query, $n, $database = NULL, $numberPerPage = NULL) { |
|
| 537 | |||
| 538 | 1 | public static function getBooks($n) { |
|
| 542 | |||
| 543 | 3 | public static function getAllBooks() { |
|
| 557 | |||
| 558 | 25 | public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) { |
|
| 561 | |||
| 562 | 53 | public static function getEntryArray ($query, $params, $n, $database = NULL, $numberPerPage = NULL) { |
|
| 572 | |||
| 573 | |||
| 574 | 5 | public static function getAllRecentBooks() { |
|
| 579 | |||
| 580 | } |
||
| 581 |
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.