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 |
||
| 19 | class NetworkPost |
||
| 20 | { |
||
| 21 | const POST_TYPE_QUESTION = 'question'; |
||
| 22 | const POST_TYPE_ANSWER = 'answer'; |
||
| 23 | |||
| 24 | private $id; |
||
| 25 | private $postType; |
||
| 26 | private $score; |
||
| 27 | private $title; |
||
| 28 | |||
| 29 | public static function fromProperties($id, $postType, $score, $title) |
||
| 33 | |||
| 34 | public static function fromJson(array $data) |
||
| 43 | |||
| 44 | private function __construct($id = null, $postType = null, $score = null, $title = null) |
||
| 51 | |||
| 52 | public function getId() |
||
| 56 | |||
| 57 | public function setId($id) |
||
| 63 | |||
| 64 | public function getPostType() |
||
| 68 | |||
| 69 | View Code Duplication | public function setPostType($postType) |
|
| 77 | |||
| 78 | public function getScore() |
||
| 82 | |||
| 83 | public function setScore($score) |
||
| 89 | |||
| 90 | public function getTitle() |
||
| 94 | |||
| 95 | public function setTitle($title) |
||
| 101 | } |
||
| 102 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.