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 |
||
12 | View Code Duplication | class Gzip implements |
|
13 | CompressionTypeInterface, |
||
14 | CompressorInterface, |
||
15 | DeCompressorInterface, |
||
16 | LoggerAwareInterface, |
||
17 | ProcessFactoryAwareInterface |
||
18 | { |
||
19 | use GetOptionTrait; |
||
20 | use FileProcessTrait; |
||
21 | use OptionalLoggerTrait; |
||
22 | use CompressorTrait; |
||
23 | use DeCompressorTrait; |
||
24 | |||
25 | const NAME = 'gzip'; |
||
26 | |||
27 | /** |
||
28 | * Get the extension used by this compressor |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | 10 | public function getExtension() |
|
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | 22 | public function getName() |
|
44 | |||
45 | /** |
||
46 | * Get the command line to compress a file |
||
47 | * |
||
48 | * @param LocalFileNodeInterface $from |
||
49 | * @param LocalFileNodeInterface $to |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 10 | public function getCompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to) |
|
57 | |||
58 | /** |
||
59 | * Get the command line to decompress a file |
||
60 | * |
||
61 | * @param LocalFileNodeInterface $from |
||
62 | * @param LocalFileNodeInterface $to |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 6 | public function getDecompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to) |
|
70 | } |
||
71 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.