| Conditions | 10 |
| Paths | 14 |
| Total Lines | 93 |
| Code Lines | 67 |
| 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 |
||
| 49 | protected function generateRelationshipOperation(Paths $paths, $actionName) { |
||
| 50 | $this->logger->notice('Generating Relationship Operation for: ' . $actionName); |
||
| 51 | $parsed = $this->factory->getActionNameGenerator()->parseRelationship($actionName); |
||
| 52 | $type = $parsed['type']; |
||
| 53 | $modelName = $parsed['modelName']; |
||
| 54 | $model = $this->modelService->getModel($modelName); |
||
| 55 | $relatedTypeName = NameUtils::dasherize($parsed['relatedName']); |
||
| 56 | $relationship = $this->modelService->getRelationship($model, $relatedTypeName); |
||
| 57 | |||
| 58 | if ($relationship === null) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | // see if either one of them is excluded |
||
| 63 | $relatedName = $relationship->getRelatedName(); |
||
| 64 | $foreignModelName = $relationship->getForeign()->getOriginCommonName(); |
||
| 65 | $excluded = $this->project->getGeneratorDefinition()->getExcludedApi(); |
||
| 66 | if ($excluded->contains($modelName) || $excluded->contains($foreignModelName)) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | // continue if neither model nor related model is excluded |
||
| 71 | $action = $this->packageService->getAction($actionName); |
||
| 72 | $method = $this->getMethod($type); |
||
| 73 | $endpoint = '/' . NameUtils::pluralize(NameUtils::dasherize($modelName)) . '/{id}/relationship/' . |
||
| 74 | NameUtils::dasherize($relationship->isOneToOne() |
||
| 75 | ? $relatedTypeName |
||
| 76 | : NameUtils::pluralize($relatedTypeName)); |
||
| 77 | |||
| 78 | $path = $paths->get($endpoint); |
||
| 79 | $method = $this->getMethod($type); |
||
| 80 | $operation = $path->getOperation($method); |
||
| 81 | $operation->setDescription($action->getTitle()); |
||
| 82 | $operation->setOperationId($action->getName()); |
||
| 83 | // $operation->getTags()->clear(); |
||
| 84 | // $operation->getTags()->add(new Tag($this->package->getKeeko()->getModule()->getSlug())); |
||
| 85 | |||
| 86 | $params = $operation->getParameters(); |
||
| 87 | $responses = $operation->getResponses(); |
||
| 88 | |||
| 89 | // general model related params |
||
| 90 | // params |
||
| 91 | $id = $params->getByName('id'); |
||
| 92 | $id->setIn('path'); |
||
| 93 | $id->setDescription(sprintf('The %s id', $modelName)); |
||
| 94 | $id->setRequired(true); |
||
| 95 | $id->setType('integer'); |
||
| 96 | |||
| 97 | if ($type == Types::ADD || $type == Types::UPDATE) { |
||
| 98 | $body = $params->getByName('body'); |
||
| 99 | $body->setName('body'); |
||
| 100 | $body->setIn('body'); |
||
| 101 | $body->setDescription(sprintf('%ss %s', ucfirst($type), $relatedName)); |
||
| 102 | $body->setRequired(true); |
||
| 103 | |||
| 104 | $props = $body->getSchema()->setType('object')->getProperties(); |
||
| 105 | $data = $props->get('data'); |
||
| 106 | |||
| 107 | if ($relationship->isOneToOne()) { |
||
| 108 | $data->setRef('#/definitions/ResourceIdentifier'); |
||
| 109 | } else { |
||
| 110 | $data |
||
| 111 | ->setType('array') |
||
| 112 | ->getItems()->setRef('#/definitions/ResourceIdentifier'); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // response |
||
| 117 | $ok = $responses->get('200'); |
||
| 118 | $ok->setDescription('Retrieve ' . $relatedName . ' from ' . $modelName); |
||
| 119 | $props = $ok->getSchema()->setType('object')->getProperties(); |
||
| 120 | $links = $props->get('links')->setType('object')->getProperties(); |
||
| 121 | $links->get('self')->setType('string'); |
||
| 122 | if ($relationship->isOneToOne()) { |
||
| 123 | $links->get('related')->setType('string'); |
||
| 124 | } |
||
| 125 | $data = $props->get('data'); |
||
| 126 | if ($relationship->isOneToOne()) { |
||
| 127 | $data->setType('object')->setRef('#/definitions/ResourceIdentifier'); |
||
| 128 | } else { |
||
| 129 | $data |
||
| 130 | ->setType('array') |
||
| 131 | ->getItems()->setRef('#/definitions/ResourceIdentifier'); |
||
| 132 | } |
||
| 133 | |||
| 134 | $invalid = $responses->get('400'); |
||
| 135 | $invalid->setDescription('Invalid ID supplied'); |
||
| 136 | $invalid->getSchema()->setRef('#/definitions/Errors'); |
||
| 137 | |||
| 138 | $notfound = $responses->get('404'); |
||
| 139 | $notfound->setDescription(sprintf('No %s found', $modelName)); |
||
| 140 | $notfound->getSchema()->setRef('#/definitions/Errors'); |
||
| 141 | } |
||
| 142 | |||
| 157 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: