Conditions | 12 |
Paths | 21 |
Total Lines | 43 |
Code Lines | 20 |
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 |
||
66 | public function updateSeoByEntity(BasePage $page, $entity) |
||
67 | { |
||
68 | //if no entity is provided |
||
69 | if ($entity === null) { |
||
70 | //we look for the entity of the page |
||
71 | if ($page->getBusinessEntity() !== null) { |
||
|
|||
72 | $entity = $page->getBusinessEntity(); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | //only if we have an entity instance |
||
77 | if ($entity !== null) { |
||
78 | //the page seo |
||
79 | $pageSeo = $page->getSeo(); |
||
80 | |||
81 | //the page seo might not exist yet |
||
82 | if ($pageSeo !== null) { |
||
83 | $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
||
84 | |||
85 | if ($businessEntity !== null) { |
||
86 | $businessProperties = $businessEntity->getBusinessPropertiesByType('seoable'); |
||
87 | |||
88 | //parse the business properties |
||
89 | foreach ($businessProperties as $businessProperty) { |
||
90 | //parse of seo attributes |
||
91 | foreach ($this->pageSeoAttributes as $seoAttribute) { |
||
92 | $accessor = new PropertyAccessor(); |
||
93 | $value = $accessor->getValue($pageSeo, $seoAttribute); |
||
94 | // we only update value if its a string and (if its a VBP or its a BP where value is not defined) |
||
95 | if (is_string($value) && ($page instanceof VirtualBusinessPage || ($page instanceof BusinessPage && $value == null))) { |
||
96 | $value = $this->parameterConverter->convertFromEntity( |
||
97 | $value, |
||
98 | $businessProperty, |
||
99 | $entity |
||
100 | ); |
||
101 | } |
||
102 | $this->setEntityAttributeValue($pageSeo, $seoAttribute, $value); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
126 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: