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 |
||
| 26 | class Commit |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var string 7 characters commit hash |
||
| 30 | */ |
||
| 31 | protected $_hash; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string string representation of commit |
||
| 35 | */ |
||
| 36 | protected $_label; |
||
| 37 | protected $_comments = []; |
||
| 38 | |||
| 39 | protected $_date; |
||
| 40 | protected $_author; |
||
| 41 | protected $_subject; |
||
| 42 | |||
| 43 | 3 | public function __construct($hash, $label = null, array $comments = []) |
|
| 49 | |||
| 50 | public function set(Commit $commit) |
||
| 56 | |||
| 57 | 3 | public function setHash($hash) |
|
| 61 | |||
| 62 | 3 | public function getHash() |
|
| 66 | |||
| 67 | 3 | public function setLabel($label) |
|
| 68 | { |
||
| 69 | 3 | if ($label) { |
|
| 70 | 3 | $this->_label = $label; |
|
| 71 | 3 | if (preg_match('/^(\d{4}-\d{2}-\d{2})\s*(.*?)\s*\[(.*?)\]$/', $label, $m) || |
|
| 72 | 3 | preg_match('/^(\d{4}-\d{2}-\d{2})\s*(.*?)\s*\((.*?)\)$/', $label, $m)) { |
|
| 73 | 3 | $this->setDate($m[1]); |
|
| 74 | 3 | $this->setSubject($m[2]); |
|
| 75 | 3 | $this->setAuthor($m[3]); |
|
| 76 | } |
||
| 77 | } |
||
| 78 | 3 | } |
|
| 79 | |||
| 80 | 3 | public function getLabel() |
|
| 81 | { |
||
| 82 | 3 | return $this->_subject || $this->_date || $this->_author |
|
| 83 | 3 | ? $this->getDate() . ' ' . $this->getSubject() . ' [' . $this->getAuthor() . ']' |
|
| 84 | 3 | : $this->_label |
|
| 85 | ; |
||
| 86 | } |
||
| 87 | |||
| 88 | 2 | public function addComment($comment) |
|
| 92 | |||
| 93 | 3 | public function addComments(array $comments) |
|
| 94 | { |
||
| 95 | 3 | foreach ($comments as $comment) { |
|
| 96 | 1 | $this->addComment($comment); |
|
| 97 | } |
||
| 98 | 3 | } |
|
| 99 | |||
| 100 | 3 | public function setComments($comments) |
|
| 105 | |||
| 106 | 3 | public function getComments() |
|
| 110 | |||
| 111 | 3 | View Code Duplication | public function setDate($value) |
| 112 | { |
||
| 113 | 3 | $timestamp = strtotime($value); |
|
| 114 | 3 | if ($timestamp !== false) { |
|
| 115 | 3 | $this->_date = $timestamp; |
|
| 116 | } |
||
| 117 | |||
| 118 | 3 | return $this; |
|
| 119 | } |
||
| 120 | |||
| 121 | 3 | public function getDate() |
|
| 125 | |||
| 126 | 3 | public function setSubject($value) |
|
| 132 | |||
| 133 | 3 | public function getSubject() |
|
| 137 | |||
| 138 | 3 | public function setAuthor($value) |
|
| 144 | |||
| 145 | 3 | public function getAuthor() |
|
| 149 | } |
||
| 150 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.