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 | View Code Duplication | class Search extends Controller |
|
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Elasticsearch. |
||
| 25 | * |
||
| 26 | * @var Elasticsearch |
||
| 27 | */ |
||
| 28 | protected $es; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Attribut decorator. |
||
| 32 | * |
||
| 33 | * @var AttributeDecorator |
||
| 34 | */ |
||
| 35 | protected $decorator; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Logger. |
||
| 39 | * |
||
| 40 | * @var LoggerInterface |
||
| 41 | */ |
||
| 42 | protected $logger; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor. |
||
| 46 | * |
||
| 47 | * @param Elasticsearch $es |
||
| 48 | * @param AttributeDecorator $decorator |
||
| 49 | * @param LoggerInterface $logger |
||
| 50 | */ |
||
| 51 | public function __construct(Elasticsearch $es, AttributeDecorator $decorator, LoggerInterface $logger) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @api {get} /api/v1/node/search Search |
||
| 60 | * @apiVersion 1.0.0 |
||
| 61 | * @apiName getSearch |
||
| 62 | * @apiGroup Node |
||
| 63 | * @apiPermission none |
||
| 64 | * @apiDescription Extended search query using elasticsearch |
||
| 65 | * @apiUse _nodeAttributes |
||
| 66 | * |
||
| 67 | * @apiExample (cURL) example: |
||
| 68 | * #Fulltext search and search for a name |
||
| 69 | * curl -XGET -H 'Content-Type: application/json' "https://SERVER/api/v2/node/search?pretty" -d '{ |
||
| 70 | * "body": { |
||
| 71 | * "query": { |
||
| 72 | * "bool": { |
||
| 73 | * "should": [ |
||
| 74 | * { |
||
| 75 | * "match": { |
||
| 76 | * "content": "house" |
||
| 77 | * } |
||
| 78 | * }, |
||
| 79 | * { |
||
| 80 | * "match": { |
||
| 81 | * "name": "file.txt" |
||
| 82 | * } |
||
| 83 | * } |
||
| 84 | * ] |
||
| 85 | * } |
||
| 86 | * } |
||
| 87 | * } |
||
| 88 | * }' |
||
| 89 | * |
||
| 90 | * @apiParam (GET Parameter) {object} query Elasticsearch query object |
||
| 91 | * @apiParam (GET Parameter) {string[]} [attributes] Filter node attributes |
||
| 92 | * @apiParam (GET Parameter) {number} [deleted=0] Wherever include deleted nodes or not, possible values:</br> |
||
| 93 | * - 0 Exclude deleted</br> |
||
| 94 | * - 1 Only deleted</br> |
||
| 95 | * - 2 Include deleted</br> |
||
| 96 | * |
||
| 97 | * @apiSuccess (200 OK) {object[]} data Node list (matched nodes) |
||
| 98 | * @apiSuccessExample {json} Success-Response: |
||
| 99 | * HTTP/1.1 200 OK |
||
| 100 | * { |
||
| 101 | * "status":200, |
||
| 102 | * "data": [{...}, {...}] |
||
| 103 | * } |
||
| 104 | * } |
||
| 105 | * |
||
| 106 | * @param array $query |
||
| 107 | * @param array $attributes |
||
| 108 | * @param int $deleted |
||
| 109 | * |
||
| 110 | * @return Response |
||
| 111 | */ |
||
| 112 | public function get(array $query, array $attributes = [], int $deleted = 0): Response |
||
| 131 | } |
||
| 132 |
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.