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 | class TimberImageOperationRetina extends TimberImageOperation { |
||
13 | |||
14 | private $factor; |
||
15 | |||
16 | /** |
||
17 | * Construct our operation |
||
18 | * @param float $factor to multiply original dimensions by |
||
19 | */ |
||
20 | function __construct($factor) { |
||
23 | |||
24 | /** |
||
25 | * Generates the final filename based on the source's name and extension |
||
26 | * |
||
27 | * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
||
28 | * @param string $src_extension the extension (ex: .jpg) |
||
29 | * @return string the final filename to be used (ex: [email protected]) |
||
30 | */ |
||
31 | function filename($src_filename, $src_extension) { |
||
36 | |||
37 | /** |
||
38 | * Performs the actual image manipulation, |
||
39 | * including saving the target file. |
||
40 | * |
||
41 | * @param string $load_filename filepath (not URL) to source file |
||
42 | * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
||
43 | * @param string $save_filename filepath (not URL) where result file should be saved |
||
44 | * (ex: /src/var/www/wp-content/uploads/[email protected]) |
||
45 | * @return bool true if everything went fine, false otherwise |
||
46 | */ |
||
47 | function run($load_filename, $save_filename){ |
||
74 | } |
||
75 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.