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 |
||
| 25 | class QueryParameters implements EncodingParametersInterface |
||
| 26 | { |
||
| 27 | const INVALID_INCLUDE_PATHS = '9f4922b8-8e8b-4847-baf2-5831adfd6813'; |
||
| 28 | const INVALID_FIELD_SET = 'ec7d2c6b-97d1-4f94-ba94-d141d985fc6f'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string[]|null |
||
| 32 | * @Reva2\JsonApi\Annotations\Property(path="[include]", parser="parseIncludePaths") |
||
| 33 | * @Symfony\Component\Validator\Constraints\Type(type="array") |
||
| 34 | * @Symfony\Component\Validator\Constraints\All({ |
||
| 35 | * @Symfony\Component\Validator\Constraints\Type(type="string") |
||
| 36 | * }) |
||
| 37 | */ |
||
| 38 | protected $includePaths; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array[]|null |
||
| 42 | * @Reva2\JsonApi\Annotations\Property(path="[fields]", parser="parseFieldSets") |
||
| 43 | * @Symfony\Component\Validator\Constraints\Type(type="array") |
||
| 44 | * @Symfony\Component\Validator\Constraints\All({ |
||
| 45 | * @Symfony\Component\Validator\Constraints\Type(type="string") |
||
| 46 | * }) |
||
| 47 | */ |
||
| 48 | protected $fieldSets; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @inheritdoc |
||
| 52 | */ |
||
| 53 | 2 | public function getIncludePaths() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Sets include paths |
||
| 60 | * |
||
| 61 | * @param string[]|null $paths |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | 3 | public function setIncludePaths(array $paths = null) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @inheritdoc |
||
| 73 | */ |
||
| 74 | 1 | public function getFieldSets() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | 1 | public function getFieldSet($type) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param \array[]|null $fieldSets |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | 3 | public function setFieldSets(array $fieldSets = null) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | 1 | public function getSortParameters() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritdoc |
||
| 108 | */ |
||
| 109 | 1 | public function getPaginationParameters() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @inheritdoc |
||
| 116 | */ |
||
| 117 | 1 | public function getFilteringParameters() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | public function getUnrecognizedParameters() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | */ |
||
| 133 | 1 | public function isEmpty() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Parse value of parameter that store include paths |
||
| 149 | * which should be included into response |
||
| 150 | * |
||
| 151 | * @param string|null $value |
||
| 152 | * @return array|null |
||
| 153 | */ |
||
| 154 | 2 | public function parseIncludePaths($value = null) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Parse value of parameter that store fields which |
||
| 168 | * should be included into response |
||
| 169 | * |
||
| 170 | * @param array|null $value |
||
| 171 | * @return array|null |
||
| 172 | */ |
||
| 173 | 2 | public function parseFieldSets($value = null) { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Validate specified include paths |
||
| 191 | * |
||
| 192 | * @param ExecutionContextInterface $context |
||
| 193 | * @Symfony\Component\Validator\Constraints\Callback() |
||
| 194 | */ |
||
| 195 | 1 | public function validateIncludePaths(ExecutionContextInterface $context) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Validate specified fields sets |
||
| 216 | * |
||
| 217 | * @param ExecutionContextInterface $context |
||
| 218 | * @Symfony\Component\Validator\Constraints\Callback() |
||
| 219 | */ |
||
| 220 | 1 | public function validateFieldSets(ExecutionContextInterface $context) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Returns list of allowed include paths |
||
| 244 | * |
||
| 245 | * @return string[] |
||
| 246 | */ |
||
| 247 | 1 | protected function getAllowedIncludePaths() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Returns list of fields available in specified resource |
||
| 254 | * |
||
| 255 | * @param string $resource |
||
| 256 | * @return array[] |
||
| 257 | */ |
||
| 258 | 1 | protected function getAllowedFields($resource) |
|
| 262 | } |
||
| 263 |