| Conditions | 11 |
| Paths | 8 |
| Total Lines | 36 |
| Code Lines | 24 |
| 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 |
||
| 27 | public function __call($name, $arguments) |
||
| 28 | { |
||
| 29 | if (strpos($name, 'get') === 0) { |
||
| 30 | $property = lcfirst(substr($name, 3)); |
||
| 31 | $mapper = $this->getRepository()->getMapper(); |
||
| 32 | if (property_exists($this, $property)) { |
||
| 33 | $reference = $this->getRepository()->getSpace()->getReference($property); |
||
| 34 | if ($reference) { |
||
| 35 | return $mapper->findOrFail($reference, [ |
||
| 36 | 'id' => $this->$property, |
||
| 37 | ]); |
||
| 38 | } |
||
| 39 | } else if(strpos($property, 'Collection') !== false) { |
||
| 40 | $property = substr($property, 0, -10); |
||
| 41 | $targetSpace = $mapper->getSchema()->toUnderscore($property); |
||
| 42 | if ($mapper->getSchema()->hasSpace($targetSpace)) { |
||
| 43 | $localSpace = $this->getRepository()->getSpace()->getName(); |
||
| 44 | $candidates = []; |
||
| 45 | foreach ($mapper->getSchema()->getSpace($targetSpace)->getFormat() as $row) { |
||
| 46 | if (array_key_exists('reference', $row) && $row['reference'] == $localSpace) { |
||
| 47 | $candidates[] = $row['name']; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | if (count($candidates) == 1) { |
||
| 51 | return $mapper->find($targetSpace, [ |
||
| 52 | $candidates[0] => $this->id |
||
|
|
|||
| 53 | ]); |
||
| 54 | } |
||
| 55 | if (count($candidates) > 1) { |
||
| 56 | throw new Exception("Multiple references from $targetSpace to $localSpace"); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | throw new BadMethodCallException("Call to undefined method ". get_class($this).'::'.$name); |
||
| 62 | } |
||
| 63 | |||
| 77 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: