| Conditions | 30 |
| Paths | 2880 |
| Total Lines | 187 |
| Code Lines | 117 |
| Lines | 16 |
| Ratio | 8.56 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 135 | public function getModelSchema($modelName, DocumentModel $model, $online = true) |
||
| 136 | { |
||
| 137 | // build up schema data |
||
| 138 | $schema = new Schema; |
||
| 139 | |||
| 140 | if (!empty($model->getTitle())) { |
||
| 141 | $schema->setTitle($model->getTitle()); |
||
| 142 | } else { |
||
| 143 | $schema->setTitle(ucfirst($modelName)); |
||
| 144 | } |
||
| 145 | |||
| 146 | $schema->setDescription($model->getDescription()); |
||
| 147 | $schema->setType('object'); |
||
| 148 | |||
| 149 | // grab schema info from model |
||
| 150 | $repo = $model->getRepository(); |
||
| 151 | $meta = $repo->getClassMetadata(); |
||
| 152 | |||
| 153 | // Init sub searchable fields |
||
| 154 | $subSearchableFields = array(); |
||
| 155 | |||
| 156 | // look for translatables in document class |
||
| 157 | $documentReflection = new \ReflectionClass($repo->getClassName()); |
||
| 158 | if ($documentReflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) { |
||
| 159 | /** @var TranslatableDocumentInterface $documentInstance */ |
||
| 160 | $documentInstance = $documentReflection->newInstanceWithoutConstructor(); |
||
| 161 | $translatableFields = array_merge( |
||
| 162 | $documentInstance->getTranslatableFields(), |
||
| 163 | $documentInstance->getPreTranslatedFields() |
||
| 164 | ); |
||
| 165 | } else { |
||
| 166 | $translatableFields = []; |
||
| 167 | } |
||
| 168 | |||
| 169 | // exposed fields |
||
| 170 | $documentFieldNames = isset($this->documentFieldNames[$repo->getClassName()]) ? |
||
| 171 | $this->documentFieldNames[$repo->getClassName()] : |
||
| 172 | []; |
||
| 173 | |||
| 174 | if ($online) { |
||
| 175 | $languages = array_map( |
||
| 176 | function (Language $language) { |
||
| 177 | return $language->getId(); |
||
| 178 | }, |
||
| 179 | $this->languageRepository->findAll() |
||
| 180 | ); |
||
| 181 | } else { |
||
| 182 | $languages = [ |
||
| 183 | $this->defaultLocale |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | |||
| 187 | // exposed events.. |
||
| 188 | $classShortName = $documentReflection->getShortName(); |
||
| 189 | if (isset($this->eventMap[$classShortName])) { |
||
| 190 | $schema->setEventNames(array_unique($this->eventMap[$classShortName]['events'])); |
||
| 191 | } |
||
| 192 | |||
| 193 | foreach ($meta->getFieldNames() as $field) { |
||
| 194 | // don't describe hidden fields |
||
| 195 | if (!isset($documentFieldNames[$field])) { |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | // hide realId field (I was aiming at a cleaner solution than the macig realId string initially) |
||
| 199 | if ($meta->getTypeOfField($field) == 'id' && $field == 'realId') { |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | |||
| 203 | $property = new Schema(); |
||
| 204 | $property->setTitle($model->getTitleOfField($field)); |
||
| 205 | $property->setDescription($model->getDescriptionOfField($field)); |
||
| 206 | |||
| 207 | $property->setType($meta->getTypeOfField($field)); |
||
| 208 | $property->setReadOnly($model->getReadOnlyOfField($field)); |
||
| 209 | |||
| 210 | if ($meta->getTypeOfField($field) === 'many') { |
||
| 211 | $propertyModel = $model->manyPropertyModelForTarget($meta->getAssociationTargetClass($field)); |
||
| 212 | |||
| 213 | if ($model->hasDynamicKey($field)) { |
||
| 214 | $property->setType('object'); |
||
| 215 | |||
| 216 | if ($online) { |
||
| 217 | // we generate a complete list of possible keys when we have access to mongodb |
||
| 218 | // this makes everything work with most json-schema v3 implementations (ie. schemaform.io) |
||
| 219 | $dynamicKeySpec = $model->getDynamicKeySpec($field); |
||
| 220 | |||
| 221 | $documentId = $dynamicKeySpec->{'document-id'}; |
||
| 222 | $dynamicRepository = $this->repositoryFactory->get($documentId); |
||
| 223 | |||
| 224 | $repositoryMethod = $dynamicKeySpec->{'repository-method'}; |
||
| 225 | $records = $dynamicRepository->$repositoryMethod(); |
||
| 226 | |||
| 227 | $dynamicProperties = array_map( |
||
| 228 | function ($record) { |
||
| 229 | return $record->getId(); |
||
| 230 | }, |
||
| 231 | $records |
||
| 232 | ); |
||
| 233 | foreach ($dynamicProperties as $propertyName) { |
||
| 234 | $property->addProperty( |
||
| 235 | $propertyName, |
||
| 236 | $this->getModelSchema($field, $propertyModel, $online) |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | } else { |
||
| 240 | // in the swagger case we can use additionPorerties which where introduced by json-schema v4 |
||
| 241 | $property->setAdditionalProperties($this->getModelSchema($field, $propertyModel, $online)); |
||
| 242 | } |
||
| 243 | } else { |
||
| 244 | $property->setItems($this->getModelSchema($field, $propertyModel, $online)); |
||
| 245 | $property->setType('array'); |
||
| 246 | } |
||
| 247 | } elseif ($meta->getTypeOfField($field) === 'one') { |
||
| 248 | $propertyModel = $model->manyPropertyModelForTarget($meta->getAssociationTargetClass($field)); |
||
| 249 | $property = $this->getModelSchema($field, $propertyModel, $online); |
||
| 250 | |||
| 251 | if ($property->getSearchable()) { |
||
| 252 | foreach ($property->getSearchable() as $searchableSubField) { |
||
| 253 | $subSearchableFields[] = $field . '.' . $searchableSubField; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } elseif (in_array($field, $translatableFields, true)) { |
||
| 257 | $property = $this->makeTranslatable($property, $languages); |
||
| 258 | } elseif (in_array($field.'[]', $translatableFields, true)) { |
||
| 259 | $property = $this->makeArrayTranslatable($property, $languages); |
||
| 260 | } elseif ($meta->getTypeOfField($field) === 'extref') { |
||
| 261 | $urls = array(); |
||
| 262 | $refCollections = $model->getRefCollectionOfField($field); |
||
| 263 | foreach ($refCollections as $collection) { |
||
| 264 | if (isset($this->extrefServiceMapping[$collection])) { |
||
| 265 | $urls[] = $this->router->generate( |
||
| 266 | $this->extrefServiceMapping[$collection].'.all', |
||
| 267 | [], |
||
| 268 | true |
||
|
|
|||
| 269 | ); |
||
| 270 | } elseif ($collection === '*') { |
||
| 271 | $urls[] = '*'; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | $property->setRefCollection($urls); |
||
| 275 | View Code Duplication | } elseif ($meta->getTypeOfField($field) === 'collection') { |
|
| 276 | $itemSchema = new Schema(); |
||
| 277 | $property->setType('array'); |
||
| 278 | $itemSchema->setType($this->getCollectionItemType($meta->name, $field)); |
||
| 279 | |||
| 280 | $property->setItems($itemSchema); |
||
| 281 | $property->setFormat(null); |
||
| 282 | } elseif ($meta->getTypeOfField($field) === 'datearray') { |
||
| 283 | $itemSchema = new Schema(); |
||
| 284 | $property->setType('array'); |
||
| 285 | $itemSchema->setType('string'); |
||
| 286 | $itemSchema->setFormat('date-time'); |
||
| 287 | |||
| 288 | $property->setItems($itemSchema); |
||
| 289 | $property->setFormat(null); |
||
| 290 | View Code Duplication | } elseif ($meta->getTypeOfField($field) === 'hasharray') { |
|
| 291 | $itemSchema = new Schema(); |
||
| 292 | $itemSchema->setType('object'); |
||
| 293 | |||
| 294 | $property->setType('array'); |
||
| 295 | $property->setItems($itemSchema); |
||
| 296 | $property->setFormat(null); |
||
| 297 | } |
||
| 298 | $schema->addProperty($documentFieldNames[$field], $property); |
||
| 299 | } |
||
| 300 | |||
| 301 | if ($meta->isEmbeddedDocument && !in_array('id', $model->getRequiredFields())) { |
||
| 302 | $schema->removeProperty('id'); |
||
| 303 | } |
||
| 304 | |||
| 305 | $requiredFields = []; |
||
| 306 | foreach ($model->getRequiredFields() as $field) { |
||
| 307 | // don't describe hidden fields |
||
| 308 | if (!isset($documentFieldNames[$field])) { |
||
| 309 | continue; |
||
| 310 | } |
||
| 311 | |||
| 312 | $requiredFields[] = $documentFieldNames[$field]; |
||
| 313 | } |
||
| 314 | $schema->setRequired($requiredFields); |
||
| 315 | |||
| 316 | $searchableFields = array_merge($subSearchableFields, $model->getSearchableFields()); |
||
| 317 | |||
| 318 | $schema->setSearchable($searchableFields); |
||
| 319 | |||
| 320 | return $schema; |
||
| 321 | } |
||
| 322 | |||
| 409 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: