| Conditions | 11 |
| Paths | 56 |
| Total Lines | 38 |
| Code Lines | 23 |
| 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 |
||
| 29 | private function cloneProperties(array $properties = null) |
||
| 30 | { |
||
| 31 | if (null === $properties) { |
||
| 32 | $properties = isset($this->cloneProperties) ? $this->cloneProperties : []; |
||
|
|
|||
| 33 | } |
||
| 34 | |||
| 35 | foreach ($properties as $property) { |
||
| 36 | if (0 === strpos($property, '!')) { |
||
| 37 | $property = substr($property, 1); |
||
| 38 | $loop = false; |
||
| 39 | } else { |
||
| 40 | $loop = true; |
||
| 41 | } |
||
| 42 | |||
| 43 | $value = $this->{$property}; |
||
| 44 | if ($value instanceOf Collection && $loop) { |
||
| 45 | $collection = new ArrayCollection(); |
||
| 46 | foreach ($value as $item) { |
||
| 47 | $collection->add(clone $item); |
||
| 48 | } |
||
| 49 | $value = $collection; |
||
| 50 | } elseif(null === $value) { |
||
| 51 | |||
| 52 | } else { |
||
| 53 | $value = clone $value; |
||
| 54 | } |
||
| 55 | |||
| 56 | $this->{$property} = $value; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($this instanceOf IdentifiableEntityInterface) { |
||
| 60 | $this->setId(null); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (is_callable('parent::__clone')) { |
||
| 64 | parent::__clone(); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
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: