Conditions | 24 |
Paths | 245 |
Total Lines | 87 |
Lines | 26 |
Ratio | 29.89 % |
Changes | 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 |
||
92 | public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
||
93 | { |
||
94 | if (!array_key_exists('LocationCreate', $data) || !is_array($data['LocationCreate'])) { |
||
95 | throw new Exceptions\Parser("Missing or invalid 'LocationCreate' element for ContentCreate."); |
||
96 | } |
||
97 | |||
98 | $locationCreateStruct = $this->locationCreateParser->parse($data['LocationCreate'], $parsingDispatcher); |
||
99 | |||
100 | if (!array_key_exists('ContentType', $data) || !is_array($data['ContentType'])) { |
||
101 | throw new Exceptions\Parser("Missing or invalid 'ContentType' element for ContentCreate."); |
||
102 | } |
||
103 | |||
104 | if (!array_key_exists('_href', $data['ContentType'])) { |
||
105 | throw new Exceptions\Parser("Missing '_href' attribute for ContentType element in ContentCreate."); |
||
106 | } |
||
107 | |||
108 | if (!array_key_exists('mainLanguageCode', $data)) { |
||
109 | throw new Exceptions\Parser("Missing 'mainLanguageCode' element for ContentCreate."); |
||
110 | } |
||
111 | |||
112 | $contentType = $this->contentTypeService->loadContentType( |
||
113 | $this->requestParser->parseHref($data['ContentType']['_href'], 'contentTypeId') |
||
114 | ); |
||
115 | |||
116 | $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $data['mainLanguageCode']); |
||
117 | |||
118 | if (array_key_exists('Section', $data) && is_array($data['Section'])) { |
||
119 | if (!array_key_exists('_href', $data['Section'])) { |
||
120 | throw new Exceptions\Parser("Missing '_href' attribute for Section element in ContentCreate."); |
||
121 | } |
||
122 | |||
123 | $contentCreateStruct->sectionId = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId'); |
||
124 | } |
||
125 | |||
126 | if (array_key_exists('alwaysAvailable', $data)) { |
||
127 | $contentCreateStruct->alwaysAvailable = $this->parserTools->parseBooleanValue($data['alwaysAvailable']); |
||
128 | } |
||
129 | |||
130 | if (array_key_exists('remoteId', $data)) { |
||
131 | $contentCreateStruct->remoteId = $data['remoteId']; |
||
132 | } |
||
133 | |||
134 | if (array_key_exists('modificationDate', $data)) { |
||
135 | $contentCreateStruct->modificationDate = new DateTime($data['modificationDate']); |
||
136 | } |
||
137 | |||
138 | if (array_key_exists('User', $data) && is_array($data['User'])) { |
||
139 | if (!array_key_exists('_href', $data['User'])) { |
||
140 | throw new Exceptions\Parser("Missing '_href' attribute for User element in ContentCreate."); |
||
141 | } |
||
142 | |||
143 | $contentCreateStruct->ownerId = $this->requestParser->parseHref($data['User']['_href'], 'userId'); |
||
144 | } |
||
145 | |||
146 | if (!array_key_exists('fields', $data) || !is_array($data['fields']) || !is_array($data['fields']['field'])) { |
||
147 | throw new Exceptions\Parser("Missing or invalid 'fields' element for ContentCreate."); |
||
148 | } |
||
149 | |||
150 | View Code Duplication | foreach ($data['fields']['field'] as $fieldData) { |
|
|
|||
151 | if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) { |
||
152 | throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for ContentCreate."); |
||
153 | } |
||
154 | |||
155 | $fieldDefinition = $contentType->getFieldDefinition($fieldData['fieldDefinitionIdentifier']); |
||
156 | if (!$fieldDefinition) { |
||
157 | throw new Exceptions\Parser( |
||
158 | "'{$fieldData['fieldDefinitionIdentifier']}' is invalid field definition identifier for '{$contentType->identifier}' content type in ContentCreate." |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | if (!array_key_exists('fieldValue', $fieldData)) { |
||
163 | throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in ContentCreate."); |
||
164 | } |
||
165 | |||
166 | $fieldValue = $this->fieldTypeParser->parseValue($fieldDefinition->fieldTypeIdentifier, |
||
167 | $fieldData['fieldValue']); |
||
168 | |||
169 | $languageCode = null; |
||
170 | if (array_key_exists('languageCode', $fieldData)) { |
||
171 | $languageCode = $fieldData['languageCode']; |
||
172 | } |
||
173 | |||
174 | $contentCreateStruct->setField($fieldData['fieldDefinitionIdentifier'], $fieldValue, $languageCode); |
||
175 | } |
||
176 | |||
177 | return new RestContentCreateStruct($contentCreateStruct, $locationCreateStruct); |
||
178 | } |
||
179 | } |
||
180 |
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.