| Conditions | 13 |
| Paths | 72 |
| Total Lines | 67 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 20 | public function convert(JsonDefinition $definition) |
||
| 21 | { |
||
| 22 | $schema = [ |
||
| 23 | "x-documentClass" => $definition->getId() |
||
| 24 | ]; |
||
| 25 | |||
| 26 | if (!$definition->getDef()->getTarget()) { |
||
| 27 | // return $schema; |
||
| 28 | } |
||
| 29 | |||
| 30 | $requiredFields = []; |
||
| 31 | $searchableFields = []; |
||
| 32 | $readOnlyFields = []; |
||
| 33 | |||
| 34 | |||
| 35 | $schema = [ |
||
| 36 | "x-documentClass" => $definition->getId(), |
||
| 37 | "title" => $definition->getTitle(), |
||
| 38 | "description" => $definition->getDescription(), |
||
| 39 | "x-recordOriginModifiable" => $definition->isRecordOriginModifiable(), |
||
| 40 | "properties" => [] |
||
| 41 | ]; |
||
| 42 | if (method_exists($definition, 'isVersionedService')) { |
||
| 43 | $schema["x-versioning"] = $definition->isVersionedService(); |
||
| 44 | } |
||
| 45 | |||
| 46 | $exposeFieldNames = array_flip(['title', 'type', 'readOnly', 'description', 'recordOriginException']); |
||
| 47 | |||
| 48 | if (!$definition->getDef()->getTarget()) { |
||
| 49 | return $schema; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** @var JsonDefinitionField $field */ |
||
| 53 | foreach ($definition->getFields() as $field) { |
||
| 54 | $def = $field->getDefAsArray(); |
||
| 55 | $key = $field->getName(); |
||
| 56 | if (isset($def['required']) && $def['required']) { |
||
| 57 | $requiredFields[] = $field->getName(); |
||
| 58 | } |
||
| 59 | if (isset($def['searchable']) && $def['searchable']) { |
||
| 60 | $searchableFields[] = $field->getName(); |
||
| 61 | } |
||
| 62 | if (isset($def['readOnly']) && $def['readOnly']) { |
||
| 63 | $readOnlyFields[] = $field->getName(); |
||
| 64 | } |
||
| 65 | |||
| 66 | $data = array_intersect_key($def,$exposeFieldNames); |
||
| 67 | $schema["properties"][$key] = $data; |
||
| 68 | |||
| 69 | /** @var XDynamicKey $xkex */ |
||
| 70 | if (method_exists($field, 'getXDynamicKey') && $xkex = $field->getXDynamicKey()) { |
||
|
|
|||
| 71 | $schema["properties"][$key]["x-dynamic-key"] = [ |
||
| 72 | "document-id" => $xkex->getDocumentId(), |
||
| 73 | "repository-method" => $xkex->getRepositoryMethod(), |
||
| 74 | "ref-field" => $xkex->getRefField() |
||
| 75 | ]; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $schema["required"] = $requiredFields; |
||
| 80 | $schema["searchable"] = $searchableFields; |
||
| 81 | $schema["readOnlyFields"] = $readOnlyFields; |
||
| 82 | |||
| 83 | return $schema; |
||
| 84 | |||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 211 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: