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 DashboardController extends Controller |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Start date. |
||
| 25 | * |
||
| 26 | * @var \Jenssegers\Date\Date |
||
| 27 | */ |
||
| 28 | protected $startDate; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The timezone the system is running in. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $timeZone; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Creates a new dashboard controller. |
||
| 39 | * |
||
| 40 | * @return void |
||
|
|
|||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Shows the dashboard view. |
||
| 50 | * |
||
| 51 | * @return \Illuminate\View\View |
||
| 52 | */ |
||
| 53 | public function indexAction() |
||
| 54 | { |
||
| 55 | $projects = Project::get(); |
||
| 56 | $issues = $this->getIssues(); |
||
| 57 | $subscribers = $this->getSubscribers(); |
||
| 58 | |||
| 59 | return View::make('dashboard.index') |
||
| 60 | ->withPageTitle(trans('dashboard.dashboard')) |
||
| 61 | ->withProjects($projects) |
||
| 62 | ->withIssues($issues) |
||
| 63 | ->withSubscribers($subscribers); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Shows the notifications view. |
||
| 68 | * |
||
| 69 | * @return \Illuminate\View\View |
||
| 70 | */ |
||
| 71 | public function showNotifications() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Fetches all of the issues over the last 30 days. |
||
| 79 | * |
||
| 80 | * @return \Illuminate\Support\Collection |
||
| 81 | */ |
||
| 82 | View Code Duplication | protected function getIssues() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Fetches all of the subscribers over the last 30 days. |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Support\Collection |
||
| 113 | */ |
||
| 114 | View Code Duplication | protected function getSubscribers() |
|
| 140 | } |
||
| 141 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.