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 Publisher |
||
|
|
|||
| 10 | { |
||
| 11 | const ALL_PUBLISHERS_ID = "cops:publishers"; |
||
| 12 | const PUBLISHERS_COLUMNS = "publishers.id as id, publishers.name as name, count(*) as count"; |
||
| 13 | const SQL_ALL_PUBLISHERS = "select {0} from publishers, books_publishers_link where publishers.id = publisher group by publishers.id, publishers.name order by publishers.name"; |
||
| 14 | const SQL_PUBLISHERS_FOR_SEARCH = "select {0} from publishers, books_publishers_link where publishers.id = publisher and upper (publishers.name) like ? group by publishers.id, publishers.name order by publishers.name"; |
||
| 15 | |||
| 16 | |||
| 17 | public $id; |
||
| 18 | public $name; |
||
| 19 | |||
| 20 | 12 | public function __construct($post) { |
|
| 24 | |||
| 25 | 10 | public function getUri () { |
|
| 28 | |||
| 29 | 7 | public function getEntryId () { |
|
| 32 | |||
| 33 | 18 | public static function getCount() { |
|
| 37 | |||
| 38 | 5 | View Code Duplication | public static function getPublisherByBookId ($bookId) { |
| 48 | |||
| 49 | 1 | View Code Duplication | public static function getPublisherById ($publisherId) { |
| 58 | |||
| 59 | 2 | public static function getAllPublishers() { |
|
| 62 | |||
| 63 | 23 | public static function getAllPublishersByQuery($query) { |
|
| 66 | } |
||
| 67 |
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.