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 |
||
| 20 | class Resource |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Server. |
||
| 24 | * |
||
| 25 | * @var Server |
||
| 26 | */ |
||
| 27 | protected $server; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * User. |
||
| 31 | * |
||
| 32 | * @var User |
||
| 33 | */ |
||
| 34 | protected $user; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Attribute decorator. |
||
| 38 | * |
||
| 39 | * @var AttributeDecorator |
||
| 40 | */ |
||
| 41 | protected $decorator; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Initialize. |
||
| 45 | * |
||
| 46 | * @param Server $server |
||
| 47 | * @param AttributeDecorator $decorator |
||
| 48 | */ |
||
| 49 | public function __construct(Server $server, AttributeDecorator $decorator) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @api {get} /resource/acl-roles?q=:query Query available acl roles |
||
| 58 | * @apiVersion 2.0.0 |
||
| 59 | * @apiName getAclRoles |
||
| 60 | * @apiGroup Resource |
||
| 61 | * @apiPermission none |
||
| 62 | * @apiDescription Query available acl roles (user and groups) |
||
| 63 | * |
||
| 64 | * @apiExample Example usage: |
||
| 65 | * curl -XGET "https://SERVER/api/v2/user/acl-roles?q=peter" |
||
| 66 | * |
||
| 67 | * @apiParam (GET Parameter) {string} [q] Search query (user/group) |
||
| 68 | * @apiParam (GET Parameter) {boolean} [single] Search request for a single user (Don't have to be in namespace) |
||
| 69 | * @apiParam (GET Parameter) {array} [attributes] Specify user/group attributes |
||
| 70 | * @apiSuccess {number} status Status Code |
||
| 71 | * @apiSuccess {object[]} data All roles found with query search string |
||
| 72 | * @apiSuccess {string} data.type ACL role type (user|group) |
||
| 73 | * @apiSuccess {object} data.role Role attributes (user/group) |
||
| 74 | * @apiSuccessExample {json} Success-Response: |
||
| 75 | * HTTP/1.1 200 OK |
||
| 76 | * { |
||
| 77 | * "status": 200, |
||
| 78 | * "data": [ |
||
| 79 | * { |
||
| 80 | * "type": "user", |
||
| 81 | * "role": {, |
||
| 82 | * "id": "5a2e6e43db830e003c3eb3b2", |
||
| 83 | * "username": "peter.example" |
||
| 84 | * } |
||
| 85 | * }, |
||
| 86 | * { |
||
| 87 | * "type": "group", |
||
| 88 | * "role": { |
||
| 89 | * "id": "5a2e6e43db830e003c3eb3ca", |
||
| 90 | * "name": "testgroup" |
||
| 91 | * } |
||
| 92 | * } |
||
| 93 | * ] |
||
| 94 | * } |
||
| 95 | * |
||
| 96 | * @param string $q |
||
| 97 | * @param bool $single |
||
| 98 | * @param array $attributes |
||
| 99 | * |
||
| 100 | * @return Response |
||
| 101 | */ |
||
| 102 | public function getAclRoles(string $q, bool $single = false, array $attributes = []): Response |
||
| 142 | } |
||
| 143 |
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.