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 |
||
24 | class EmailAttachment implements EmailAttachmentInterface, Serializable |
||
25 | { |
||
26 | const ERROR_INVALID_FILE_NAME = 'Invalid file name provided.'; |
||
27 | const ERROR_INVALID_CONTENT = 'Invalid content provided.'; |
||
28 | const ERROR_INVALID_CONTENT_TYPE = 'Invalid content type provided.'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $fileName; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $contentType; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $content; |
||
44 | |||
45 | /** |
||
46 | * @throws EmailAttachmentException |
||
47 | */ |
||
48 | public function __construct(string $fileName, string $contentType, string $content) |
||
54 | |||
55 | public function getFileName(): string |
||
59 | |||
60 | /** |
||
61 | * @throws \Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAttachmentException |
||
62 | */ |
||
63 | View Code Duplication | protected function setFileName(string $fileName): void |
|
71 | |||
72 | public function getContentType(): string |
||
76 | |||
77 | /** |
||
78 | * @throws \Acme\App\Core\Port\Notification\Client\Email\Exception\EmailAttachmentException |
||
79 | */ |
||
80 | View Code Duplication | protected function setContentType(string $contentType): void |
|
88 | |||
89 | public function getContent(): string |
||
93 | |||
94 | /** |
||
95 | * @throws EmailAttachmentException |
||
96 | */ |
||
97 | View Code Duplication | protected function setContent(string $content): void |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function serialize() |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function unserialize($serialized): void |
||
126 | } |
||
127 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.