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 |
||
| 28 | class OrganizationController extends Controller |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Returns all the organizations of current user, it admits sort, limit and offset. |
||
| 32 | * |
||
| 33 | * @param ParamFetcher $paramFetcher The param fetcher |
||
| 34 | * |
||
| 35 | * @QueryParam(name="limit", requirements="\d+", default="9999", description="Amount of orgs to be returned") |
||
| 36 | * @QueryParam(name="offset", requirements="\d+", default="0", description="Offset in pages") |
||
| 37 | * |
||
| 38 | * @ApiDoc(resource=true, statusCodes={200}) |
||
| 39 | * @View(statusCode=200, serializerGroups={"organizationList"}) |
||
| 40 | * |
||
| 41 | * @return \Kreta\Component\Organization\Model\Interfaces\OrganizationInterface[] |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function getOrganizationsAction(ParamFetcher $paramFetcher) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the organization for given id. |
||
| 55 | * |
||
| 56 | * @param Request $request The request |
||
| 57 | * @param string $organizationId The id of organization |
||
| 58 | * |
||
| 59 | * @ApiDoc(statusCodes={200, 403, 404}) |
||
| 60 | * @View(statusCode=200, serializerGroups={"organization"}) |
||
| 61 | * @Organization() |
||
| 62 | * |
||
| 63 | * @return \Kreta\Component\Organization\Model\Interfaces\OrganizationInterface |
||
| 64 | */ |
||
| 65 | public function getOrganizationAction(Request $request, $organizationId) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Creates new organization for name given. |
||
| 72 | * |
||
| 73 | * @param Request $request The request |
||
| 74 | * |
||
| 75 | * @ApiDoc(statusCodes={201, 400}) |
||
| 76 | * @View(statusCode=201, serializerGroups={"organization"}) |
||
| 77 | * |
||
| 78 | * @return \Kreta\Component\Organization\Model\Interfaces\OrganizationInterface |
||
| 79 | */ |
||
| 80 | public function postOrganizationsAction(Request $request) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Updates the organization of id given. |
||
| 87 | * |
||
| 88 | * @param Request $request The request |
||
| 89 | * @param string $organizationId The organization id |
||
| 90 | * |
||
| 91 | * @ApiDoc(statusCodes={200, 400, 403, 404}) |
||
| 92 | * @View(statusCode=200, serializerGroups={"organization"}) |
||
| 93 | * @Organization("edit") |
||
| 94 | * |
||
| 95 | * @return \Kreta\Component\Organization\Model\Interfaces\OrganizationInterface |
||
| 96 | */ |
||
| 97 | public function putOrganizationsAction(Request $request, $organizationId) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Returns all the projects of given organization, it admits limit and offset. |
||
| 106 | * |
||
| 107 | * @param Request $request The request |
||
| 108 | * @param ParamFetcher $paramFetcher The param fetcher |
||
| 109 | * @param string $organizationId The organization id |
||
| 110 | * |
||
| 111 | * @QueryParam(name="limit", requirements="\d+", default="9999", description="Amount of projects to be returned") |
||
| 112 | * @QueryParam(name="offset", requirements="\d+", default="0", description="Offset in pages") |
||
| 113 | * |
||
| 114 | * @ApiDoc(statusCodes={200, 403, 404}) |
||
| 115 | * @View(statusCode=200, serializerGroups={"projectList"}) |
||
| 116 | * @Organization() |
||
| 117 | * |
||
| 118 | * @return \Kreta\Component\Project\Model\Interfaces\ProjectInterface[] |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function getOrganizationProjectsAction(Request $request, ParamFetcher $paramFetcher, $organizationId) |
|
| 130 | } |
||
| 131 |
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.