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 |
||
18 | final class PeekLock |
||
19 | { |
||
20 | /** |
||
21 | * The path to the file to lock. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $filePath; |
||
26 | |||
27 | /** |
||
28 | * The handle of the file that can be locked. |
||
29 | * |
||
30 | * @var resource |
||
31 | */ |
||
32 | private $fileHandle; |
||
33 | |||
34 | /** |
||
35 | * Initializes a new instance of this class. |
||
36 | * |
||
37 | * @param string $path The path to the file to lock. |
||
38 | * @throws InvalidFileException Thrown when the path is invalid. |
||
39 | */ |
||
40 | 3 | public function __construct(string $path) |
|
54 | |||
55 | /** |
||
56 | * Cleans up all resources used by this class. |
||
57 | */ |
||
58 | public function __destruct() |
||
62 | |||
63 | /** |
||
64 | * Locks the file. |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws LockException Thrown when the lock could not be created. |
||
68 | */ |
||
69 | View Code Duplication | public function blockTillLock() |
|
77 | |||
78 | /** |
||
79 | * Checks if the lock is active. |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isLocked(): bool |
||
93 | |||
94 | /** |
||
95 | * Releases the locked file. |
||
96 | * |
||
97 | * @return void |
||
98 | * @throws LockException Thrown when the lock could not be released. |
||
99 | */ |
||
100 | View Code Duplication | public function release() |
|
108 | } |
||
109 |
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.