Conditions | 13 |
Paths | 15 |
Total Lines | 86 |
Code Lines | 57 |
Lines | 16 |
Ratio | 18.6 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | protected function addMetadataFor(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata) |
||
29 | { |
||
30 | foreach ($xmlRoot->xpath('//cubiche:collection') as $item) { |
||
31 | // get the field tag |
||
32 | $field = $item->xpath('..')[0]; |
||
33 | $fieldMapping = $this->getMappingAttributes($field); |
||
34 | $fieldName = isset($fieldMapping['name']) ? $fieldMapping['name'] : $fieldMapping['field']; |
||
35 | |||
36 | $itemMapping = $this->getMappingAttributes($item, array('of' => null)); |
||
37 | if (!isset($itemMapping['type'])) { |
||
38 | throw MappingException::inField( |
||
39 | 'The cubiche:collection definition should have a "type" value', |
||
40 | $classMetadata->name, |
||
41 | $fieldName |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | $collectionType = $itemMapping['type']; |
||
46 | $collectionOf = $itemMapping['of']; |
||
47 | |||
48 | $typeClassName = sprintf( |
||
49 | 'Cubiche\\Infrastructure\\Collections\\Doctrine\\ODM\\MongoDB\\Types\\%sType', |
||
50 | $collectionType |
||
51 | ); |
||
52 | |||
53 | $persistenClassName = sprintf( |
||
54 | 'Cubiche\\Infrastructure\\Collections\\Doctrine\\Common\\Collections\\Persistent%s', |
||
55 | $collectionType |
||
56 | ); |
||
57 | |||
58 | if ($field->getName() == 'field') { |
||
59 | View Code Duplication | if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) { |
|
|
|||
60 | throw MappingException::inField( |
||
61 | 'The cubiche:collection configuration is only for field tags that is not an id', |
||
62 | $classMetadata->name, |
||
63 | $fieldName |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | if (!isset($fieldMapping['type']) || |
||
68 | (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType') |
||
69 | ) { |
||
70 | throw MappingException::inField( |
||
71 | 'The cubiche:collection parent should have a "type" value equal to CubicheType', |
||
72 | $classMetadata->name, |
||
73 | $fieldName |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName); |
||
78 | |||
79 | $propertyMetadata->addMetadata('namespace', 'collection'); |
||
80 | $propertyMetadata->addMetadata('type', $collectionType); |
||
81 | $propertyMetadata->addMetadata('of', $collectionOf); |
||
82 | $propertyMetadata->addMetadata('typeClassName', $typeClassName); |
||
83 | $propertyMetadata->addMetadata('persistenClassName', $persistenClassName); |
||
84 | |||
85 | $classMetadata->addPropertyMetadata($propertyMetadata); |
||
86 | } elseif ($field->getName() == 'embed-many' || $field->getName() == 'reference-many') { |
||
87 | View Code Duplication | if (isset($fieldMapping['field'])) { |
|
88 | $field = $fieldMapping['field']; |
||
89 | } else { |
||
90 | throw MappingException::inField( |
||
91 | 'Cannot infer a field', |
||
92 | $classMetadata->name, |
||
93 | $fieldName |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | $propertyMetadata = new PropertyMetadata($classMetadata->name, $field); |
||
98 | |||
99 | $propertyMetadata->addMetadata('namespace', 'collection'); |
||
100 | $propertyMetadata->addMetadata('type', $collectionType); |
||
101 | $propertyMetadata->addMetadata('typeClassName', $typeClassName); |
||
102 | $propertyMetadata->addMetadata('persistenClassName', $persistenClassName); |
||
103 | |||
104 | $classMetadata->addPropertyMetadata($propertyMetadata); |
||
105 | } else { |
||
106 | throw MappingException::inField( |
||
107 | 'The cubiche:collection configuration is only for field, embed-many or reference-many tags', |
||
108 | $classMetadata->name, |
||
109 | $fieldName |
||
110 | ); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 |
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.