| Conditions | 11 |
| Paths | 16 |
| Total Lines | 58 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public function observeBaseEntity(ClassType $class): ClassType |
||
| 41 | { |
||
| 42 | $name = $this->property->getName(); |
||
| 43 | $type = $this->property->getType(); |
||
| 44 | |||
| 45 | if (is_string($type) && (class_exists($type) || strpos($type, '\\') !== false)) { |
||
| 46 | /** @scrutinizer ignore-deprecated */ $class->getNamespace()->addUse($type, null, $alias); |
||
| 47 | } else { |
||
| 48 | $alias = $type; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($this->property->getMapper()->getEntityStyle() === Mapper::ENTITY_STYLE_PROPERTIES) { |
||
| 52 | $class->addComment(sprintf( |
||
| 53 | '@property %s $%s', |
||
| 54 | $alias ? $alias . ($this->property->getNullable() ? '|null' : '') : 'mixed', |
||
| 55 | $name |
||
| 56 | )); |
||
| 57 | |||
| 58 | if (($body = $this->property->getSetterBody())) { |
||
| 59 | $setter = $class->addMethod(Str::methodName($name . ' Attribute', 'set')); |
||
| 60 | $setter->setVisibility(ClassType::VISIBILITY_PROTECTED); |
||
| 61 | $setter->addParameter('value') |
||
| 62 | ->setNullable($this->property->getNullable()) |
||
| 63 | ->setType($alias); |
||
| 64 | $setter->setBody($body); |
||
| 65 | $setter->setComment($this->property->getSetterComment()); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (($body = $this->property->getGetterBody())) { |
||
| 69 | $getter = $class->addMethod(Str::methodName($name . ' Attribute', 'get')); |
||
| 70 | $getter->setVisibility(ClassType::VISIBILITY_PROTECTED); |
||
| 71 | $getter->setReturnType($alias); |
||
| 72 | $getter->setReturnNullable($this->property->getNullable()); |
||
| 73 | $getter->setBody($body); |
||
| 74 | $getter->setComment($this->property->getGetterComment()); |
||
| 75 | } |
||
| 76 | } else { |
||
| 77 | if (($body = $this->property->getSetterBody())) { |
||
| 78 | $setter = $class->addMethod(Str::methodName($name, 'set')); |
||
| 79 | $setter->setVisibility(ClassType::VISIBILITY_PUBLIC); |
||
| 80 | $setter->addParameter('value') |
||
| 81 | ->setType($type) |
||
| 82 | ->setNullable($this->property->getNullable()); |
||
| 83 | $setter->setBody($body); |
||
| 84 | $setter->setComment($this->property->getSetterComment()); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (($body = $this->property->getGetterBody())) { |
||
| 88 | $getter = $class->addMethod(Str::methodName($name, 'get')); |
||
| 89 | $getter->setVisibility(ClassType::VISIBILITY_PUBLIC); |
||
| 90 | $getter->setBody($body); |
||
| 91 | $getter->setReturnType($type); |
||
| 92 | $getter->setReturnNullable($this->property->getNullable()); |
||
| 93 | $getter->setComment($this->property->getGetterComment()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $class; |
||
| 98 | } |
||
| 100 |