Conditions | 11 |
Paths | 8 |
Total Lines | 36 |
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 |
||
30 | public function __call($name, $arguments) |
||
31 | { |
||
32 | if (strpos($name, 'get') === 0) { |
||
33 | $property = lcfirst(substr($name, 3)); |
||
34 | $mapper = $this->getRepository()->getMapper(); |
||
35 | if (property_exists($this, $property)) { |
||
36 | $reference = $this->getRepository()->getSpace()->getReference($property); |
||
37 | if ($reference) { |
||
38 | return $mapper->findOrFail($reference, [ |
||
39 | 'id' => $this->$property, |
||
40 | ]); |
||
41 | } |
||
42 | } else if(strpos($property, 'Collection') !== false) { |
||
43 | $property = substr($property, 0, -10); |
||
44 | $targetSpace = $mapper->getSchema()->toUnderscore($property); |
||
45 | if ($mapper->getSchema()->hasSpace($targetSpace)) { |
||
46 | $localSpace = $this->getRepository()->getSpace()->getName(); |
||
47 | $candidates = []; |
||
48 | foreach ($mapper->getSchema()->getSpace($targetSpace)->getFormat() as $row) { |
||
49 | if (array_key_exists('reference', $row) && $row['reference'] == $localSpace) { |
||
50 | $candidates[] = $row['name']; |
||
51 | } |
||
52 | } |
||
53 | if (count($candidates) == 1) { |
||
54 | return $mapper->find($targetSpace, [ |
||
55 | $candidates[0] => $this->id |
||
|
|||
56 | ]); |
||
57 | } |
||
58 | if (count($candidates) > 1) { |
||
59 | throw new Exception("Multiple references from $targetSpace to $localSpace"); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | throw new BadMethodCallException("Call to undefined method ". get_class($this).'::'.$name); |
||
65 | } |
||
66 | |||
85 |
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: