Conditions | 6 |
Paths | 9 |
Total Lines | 66 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
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 |
||
132 | protected function generateRelationshipMethods(PhpTrait $class, Table $model) { |
||
133 | if ($model->isReadOnly()) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $rels = []; |
||
138 | $relationships = $this->modelService->getRelationships($model); |
||
139 | |||
140 | if ($relationships->size() > 0) { |
||
141 | $class->addUseStatement('Tobscure\\JsonApi\\Relationship'); |
||
142 | } |
||
143 | |||
144 | foreach ($relationships->getAll() as $rel) { |
||
145 | |||
146 | // to-many |
||
147 | if ($rel instanceof ManyRelationship) { |
||
148 | $foreign = $rel->getForeign(); |
||
149 | $relatedName = $rel->getRelatedName(); |
||
150 | |||
151 | $typeName = $rel->getRelatedTypeName(); |
||
152 | $method = NameUtils::toCamelCase($typeName); |
||
153 | $rels[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
154 | $class->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
155 | $class->addUseStatement('Tobscure\\JsonApi\\Collection'); |
||
156 | |||
157 | // read |
||
158 | $body = $this->twig->render('to-many-read.twig', [ |
||
159 | 'getter' => NameUtils::pluralize($relatedName), |
||
160 | 'class' => $relatedName, |
||
161 | 'related' => $typeName |
||
162 | ]); |
||
163 | } |
||
164 | |||
165 | // to-one |
||
166 | else if ($rel instanceof Relationship) { |
||
167 | $foreign = $rel->getForeign(); |
||
168 | $relatedName = $rel->getRelatedName(); |
||
169 | |||
170 | $typeName = $rel->getRelatedTypeName(); |
||
171 | $method = NameUtils::toCamelCase($typeName); |
||
172 | $rels[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
173 | $class->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
174 | $class->addUseStatement('Tobscure\\JsonApi\\Resource'); |
||
175 | |||
176 | // read |
||
177 | $body = $this->twig->render('to-one-read.twig', [ |
||
178 | 'ref' => $relatedName, |
||
179 | 'class' => $foreign->getPhpName(), |
||
180 | 'related' => $typeName |
||
181 | ]); |
||
182 | } |
||
183 | |||
184 | // needs to go down |
||
185 | $class->setMethod(PhpMethod::create($method) |
||
186 | ->addParameter(PhpParameter::create('model')) |
||
187 | ->setBody($body) |
||
188 | ->setType('Relationship') |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | $class->setMethod(PhpMethod::create('getRelationships') |
||
193 | ->setBody($this->twig->render('getRelationships.twig', [ |
||
194 | 'fields' => $rels |
||
195 | ])) |
||
196 | ); |
||
197 | } |
||
198 | } |
It seems like you are relying on a variable being defined by an iteration: