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 SendMessages extends SendMessagesAbstract |
||
27 | { |
||
28 | protected $template = 'update_note'; |
||
29 | |||
30 | /** |
||
31 | * Returns the subject text. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | 1 | protected function getSubject() |
|
39 | |||
40 | /** |
||
41 | * Note: changes is belongs to the project (no issue here). |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | 1 | protected function getIssue() |
|
49 | |||
50 | /** |
||
51 | * Returns an instance of Project. |
||
52 | * |
||
53 | * @return Project |
||
54 | */ |
||
55 | 1 | View Code Duplication | protected function getProject() |
1 ignored issue
–
show
|
|||
56 | { |
||
57 | 1 | if ($this->getModel()) { |
|
58 | 1 | return $this->getModel()->project; |
|
59 | } |
||
60 | |||
61 | // Possible deleted note |
||
62 | if (null === $this->project) { |
||
63 | $projectId = $this->latestMessage->getDataFromPayload('origin.project_id'); |
||
64 | $this->project = (new Project())->find($projectId); |
||
65 | } |
||
66 | |||
67 | return $this->project; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns the project id. |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | 1 | protected function getProjectId() |
|
79 | |||
80 | /** |
||
81 | * Returns message data belongs to adding a note. |
||
82 | * |
||
83 | * @param Queue $queue |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | 1 | protected function getMessageDataForAddNote(Queue $queue) |
|
99 | |||
100 | /** |
||
101 | * Returns message data belongs to updating a note. |
||
102 | * |
||
103 | * @param Queue $queue |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 1 | protected function getMessageDataForUpdateNote(Queue $queue) |
|
120 | |||
121 | /** |
||
122 | * Returns message data belongs to deleting a note. |
||
123 | * |
||
124 | * @param Queue $queue |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | View Code Duplication | protected function getMessageDataForDeleteNote(Queue $queue) |
|
1 ignored issue
–
show
|
|||
129 | { |
||
130 | $messageData = []; |
||
131 | $messageData['changeByHeading'] = $queue->changeBy->fullname . ' deleted a note in ' . link_to($this->getProject()->to(), $this->getProject()->name); |
||
132 | $messageData['changes']['note'] = [ |
||
133 | 'noLabel' => true, |
||
134 | 'date' => $queue->created_at, |
||
135 | 'was' => \Html::format($queue->getDataFromPayload('origin.body')), |
||
136 | 'now' => '', |
||
137 | ]; |
||
138 | |||
139 | return $messageData; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Return text to be used for the message heading. |
||
144 | * |
||
145 | * @param Queue $queue |
||
146 | * @param Collection|null $changes |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | protected function getMessageHeading(Queue $queue, Collection $changes = null) |
|
157 | |||
158 | /** |
||
159 | * Check that the project is loaded and the note is belongs to the project. |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 1 | protected function validateData() |
|
167 | |||
168 | /** |
||
169 | * Check if the latest message is about deleting a note. |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 1 | public function isStatusMessage() |
|
177 | } |
||
178 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.