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 |
||
| 20 | class ConvertStreamFilter extends php_user_filter |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Name used when registering with stream_filter_register. |
||
| 24 | */ |
||
| 25 | const STREAM_FILTER_NAME = 'convert.*'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string function to call for encoding/decoding |
||
| 29 | */ |
||
| 30 | private $fnFilterName; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Sets up which function should be called. |
||
| 34 | * |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | public function onCreate() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Filter implementation converts calls the relevant encode/decode filter |
||
| 55 | * and chunk_split if needed, before returning PSFS_PASS_ON. |
||
| 56 | * |
||
| 57 | * @param resource $in |
||
| 58 | * @param resource $out |
||
| 59 | * @param int $consumed |
||
| 60 | * @param bool $closing |
||
| 61 | * @return int |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function filter($in, $out, &$consumed, $closing) |
|
| 71 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: