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:
| 1 | <?php |
||
| 9 | class Author |
||
|
|
|||
| 10 | { |
||
| 11 | const ALL_AUTHORS_ID = "cops:authors"; |
||
| 12 | |||
| 13 | const AUTHOR_COLUMNS = "authors.id as id, authors.name as name, authors.sort as sort, count(*) as count"; |
||
| 14 | const SQL_AUTHORS_BY_FIRST_LETTER = "select {0} from authors, books_authors_link where author = authors.id and upper (authors.sort) like ? group by authors.id, authors.name, authors.sort order by sort"; |
||
| 15 | const SQL_AUTHORS_FOR_SEARCH = "select {0} from authors, books_authors_link where author = authors.id and (upper (authors.sort) like ? or upper (authors.name) like ?) group by authors.id, authors.name, authors.sort order by sort"; |
||
| 16 | const SQL_ALL_AUTHORS = "select {0} from authors, books_authors_link where author = authors.id group by authors.id, authors.name, authors.sort order by sort"; |
||
| 17 | |||
| 18 | public $id; |
||
| 19 | public $name; |
||
| 20 | public $sort; |
||
| 21 | |||
| 22 | 59 | public function __construct($post) { |
|
| 27 | |||
| 28 | 55 | public function getUri () { |
|
| 31 | |||
| 32 | 20 | public function getEntryId () { |
|
| 35 | |||
| 36 | 3 | public static function getEntryIdByLetter ($startingLetter) { |
|
| 39 | |||
| 40 | 18 | public static function getCount() { |
|
| 44 | |||
| 45 | 2 | View Code Duplication | public static function getAllAuthorsByFirstLetter() { |
| 59 | |||
| 60 | 1 | public static function getAuthorsByStartingLetter($letter) { |
|
| 63 | |||
| 64 | 23 | public static function getAuthorsForSearch($query) { |
|
| 67 | |||
| 68 | 1 | public static function getAllAuthors() { |
|
| 71 | |||
| 72 | 25 | public static function getEntryArray ($query, $params) { |
|
| 75 | |||
| 76 | 7 | public static function getAuthorById ($authorId) { |
|
| 82 | |||
| 83 | 49 | View Code Duplication | public static function getAuthorByBookId ($bookId) { |
| 94 | } |
||
| 95 |
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.