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 SessionManager |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Valid until. |
||
| 28 | * |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $access_token_ttl = 3600; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Database. |
||
| 35 | * |
||
| 36 | * @var Database |
||
| 37 | */ |
||
| 38 | protected $db; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Host manager. |
||
| 42 | * |
||
| 43 | * @var HostManager |
||
| 44 | */ |
||
| 45 | protected $host_manager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Server. |
||
| 49 | */ |
||
| 50 | protected $server; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Session. |
||
| 54 | */ |
||
| 55 | public function __construct(Database $db, Server $server, HostManager $host_manager, array $config = []) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Set options. |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function setOptions(array $config = []): SessionManager |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Authenticate. |
||
| 84 | */ |
||
| 85 | public function authenticate(string $token): User |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get session by id. |
||
| 103 | */ |
||
| 104 | public function getByToken(File $file, string $token): SessionInterface |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Create session. |
||
| 125 | */ |
||
| 126 | public function create(File $file, User $user): SessionInterface |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create token. |
||
| 143 | */ |
||
| 144 | protected function createToken(): string |
||
| 148 | } |
||
| 149 |
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.