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 |
||
| 15 | class AttributeProcessor extends AbstractProcessor |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Pattern of "type" parameter of attribute annotation |
||
| 19 | */ |
||
| 20 | const DATATYPE_PATTERN = '~^(?<type>[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*)\s*(?:\((?<params>[^\)]*)\))?(?<many>\[\])?$~i'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Process annotations |
||
| 24 | * |
||
| 25 | * @param \ReflectionClass $reflection |
||
| 26 | * @param Definition $definition |
||
| 27 | */ |
||
| 28 | 20 | public function process(\ReflectionClass $reflection, Definition $definition) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Process property of class |
||
| 43 | * |
||
| 44 | * @param \ReflectionProperty $property |
||
| 45 | * @param Definition $definition |
||
| 46 | */ |
||
| 47 | 18 | protected function processProperty(\ReflectionProperty $property, Definition $definition) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Process method of class |
||
| 64 | * |
||
| 65 | * @param \ReflectionMethod $method |
||
| 66 | * @param Definition $definition |
||
| 67 | */ |
||
| 68 | 19 | protected function processMethod(\ReflectionMethod $method, Definition $definition) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Validate method with attribute definition |
||
| 85 | * |
||
| 86 | * @param AttributeAnnotation $annotation |
||
| 87 | * @param \ReflectionMethod $method |
||
| 88 | * @throws \LogicException |
||
| 89 | */ |
||
| 90 | 2 | protected function validateMethodAttribute(AttributeAnnotation $annotation, \ReflectionMethod $method) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Create attribute by annotation of property |
||
| 109 | * |
||
| 110 | * @param AttributeAnnotation $annotation |
||
| 111 | * @param \ReflectionProperty $property |
||
| 112 | * @return Attribute |
||
| 113 | */ |
||
| 114 | 18 | protected function createAttributeByProperty(AttributeAnnotation $annotation, \ReflectionProperty $property): Attribute |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Process optional properties of attribute |
||
| 142 | * |
||
| 143 | * @param AttributeAnnotation $annotation |
||
| 144 | * @param Attribute $attribute |
||
| 145 | */ |
||
| 146 | 18 | protected function processAttributeOptions(AttributeAnnotation $annotation, Attribute $attribute) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Create attribute by annotation of method |
||
| 163 | * |
||
| 164 | * @param AttributeAnnotation $annotation |
||
| 165 | * @param \ReflectionMethod $method |
||
| 166 | * @return Attribute |
||
| 167 | */ |
||
| 168 | 2 | protected function createAttributeByMethod(AttributeAnnotation $annotation, \ReflectionMethod $method): Attribute |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Resolve name of attribute by method |
||
| 189 | * |
||
| 190 | * @param \ReflectionMethod $method |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 2 | protected function resolveNameByMethod(\ReflectionMethod $method): string |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Process data-type |
||
| 206 | * |
||
| 207 | * @param string $definition |
||
| 208 | * @param Attribute $attribute |
||
| 209 | */ |
||
| 210 | 18 | View Code Duplication | protected function processDataType(string $definition, Attribute $attribute) |
| 231 | } |