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 |
||
| 21 | class NotFoundException extends RuntimeException |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Creates an exception for a not found message name, method name or handler for a given message. |
||
| 25 | * |
||
| 26 | * @param string $type |
||
| 27 | * @param mixed $message |
||
| 28 | * @param int $code |
||
| 29 | * @param Exception|null $cause |
||
| 30 | * |
||
| 31 | * @return NotFoundException |
||
| 32 | */ |
||
| 33 | View Code Duplication | protected static function notFound($type, $message, $code = 0, Exception $cause = null) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param mixed $object |
||
| 44 | * @param Exception|null $cause |
||
| 45 | * |
||
| 46 | * @return NotFoundException |
||
| 47 | */ |
||
| 48 | public static function nameOfMessage($object, Exception $cause = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param mixed $object |
||
| 55 | * @param Exception|null $cause |
||
| 56 | * |
||
| 57 | * @return NotFoundException |
||
| 58 | */ |
||
| 59 | public static function nameOfCommand($object, Exception $cause = null) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param mixed $object |
||
| 66 | * @param Exception|null $cause |
||
| 67 | * |
||
| 68 | * @return NotFoundException |
||
| 69 | */ |
||
| 70 | public static function handlerMethodNameForObject($object, Exception $cause = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param mixed $object |
||
| 77 | * @param Exception|null $cause |
||
| 78 | * |
||
| 79 | * @return NotFoundException |
||
| 80 | */ |
||
| 81 | public static function nameOfQuery($object, Exception $cause = null) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $messageName |
||
| 88 | * @param Exception|null $cause |
||
| 89 | * |
||
| 90 | * @return NotFoundException |
||
| 91 | */ |
||
| 92 | public static function handlerFor($messageName, Exception $cause = null) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Creates an exception for a not found middleware. |
||
| 102 | * |
||
| 103 | * @param string $type |
||
| 104 | * @param Exception|null $cause |
||
| 105 | * |
||
| 106 | * @return NotFoundException |
||
| 107 | */ |
||
| 108 | public static function middlewareOfType($type, Exception $cause = null) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param mixed $object |
||
| 118 | * @param string $method |
||
| 119 | * @param Exception|null $cause |
||
| 120 | * |
||
| 121 | * @return NotFoundException |
||
| 122 | */ |
||
| 123 | public static function methodForObject($object, $method, Exception $cause = null) |
||
| 127 | } |
||
| 128 |
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.