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 |
||
| 23 | class FileLocator extends Locator { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string $file_mask |
||
| 27 | */ |
||
| 28 | protected $file_mask = '*.php'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array $filepaths |
||
| 32 | */ |
||
| 33 | protected $filepaths = array(); |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $file_mask |
||
| 39 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 40 | */ |
||
| 41 | public function setFileMask( $file_mask ) { |
||
| 42 | if ( ! is_string( $file_mask ) ) { |
||
| 43 | throw new InvalidDataTypeException( '$file_mask', $file_mask, 'string' ); |
||
| 44 | } |
||
| 45 | $this->file_mask = $file_mask; |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * @access public |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function getFilePaths() { |
||
| 55 | return $this->filepaths; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @access public |
||
| 63 | * @return int |
||
| 64 | */ |
||
| 65 | public function count() { |
||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * given a path to a valid directory, or an array of valid paths, |
||
| 73 | * will find all files that match the provided mask |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | * @param array|string $directory_paths |
||
| 77 | * @return \FilesystemIterator |
||
| 78 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function locate( $directory_paths ) { |
|
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * given a path to a valid directory, will find all files that match the provided mask |
||
| 96 | * |
||
| 97 | * @access protected |
||
| 98 | * @param string $directory_path |
||
| 99 | * @return \FilesystemIterator |
||
| 100 | */ |
||
| 101 | protected function findFilesByPath( $directory_path = '' ) { |
||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | } |
||
| 114 | // End of file FileLocator.php |
||
| 115 | // Location: /FileLocator.php |
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.