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 |
||
| 9 | View Code Duplication | class PageImageController extends Controller |
|
|
|
|||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Service class for content images. |
||
| 13 | * |
||
| 14 | * @var Larafolio\Http\Content\ContentImages |
||
| 15 | */ |
||
| 16 | protected $contentImages; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Construct. |
||
| 20 | * |
||
| 21 | * @param Larafolio\Http\Content\ContentImages $contentImages Service class for content images. |
||
| 22 | */ |
||
| 23 | public function __construct(ContentImages $contentImages) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Show images for project. |
||
| 32 | * |
||
| 33 | * @param \Illuminate\Http\Request $request Request from user. |
||
| 34 | * @param Larafolio\Models\Page $page Page to show. |
||
| 35 | * |
||
| 36 | * @return \Illuminate\Http\Response |
||
| 37 | */ |
||
| 38 | public function index(Request $request, Page $page) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Add a new page image. |
||
| 45 | * |
||
| 46 | * @param \Illuminate\Http\Request $request Form request. |
||
| 47 | * @param Larafolio\Models\Page $page The page to add the image too. |
||
| 48 | */ |
||
| 49 | public function store(Request $request, Page $page) |
||
| 53 | } |
||
| 54 |
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.