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 |
||
14 | class PredisShutdown implements ShutdownInterface |
||
15 | { |
||
16 | /** @var string */ |
||
17 | private $key; |
||
18 | |||
19 | /** @var Client */ |
||
20 | private $redis; |
||
21 | |||
22 | public function __construct(Client $redis, string $key = 'hermes_shutdown') |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | * |
||
31 | * Returns true: |
||
32 | * |
||
33 | * - if shutdown timestamp is set, |
||
34 | * - and timestamp is not in future, |
||
35 | * - and hermes was started ($startTime) before timestamp |
||
36 | */ |
||
37 | View Code Duplication | public function shouldShutdown(DateTime $startTime): bool |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | * |
||
62 | * Sets to Redis value `$shutdownTime` (or current DateTime) to `$key` defined in constructor. |
||
63 | */ |
||
64 | View Code Duplication | public function shutdown(DateTime $shutdownTime = null): bool |
|
75 | } |
||
76 |
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.