Conditions | 10 |
Paths | 10 |
Total Lines | 32 |
Code Lines | 29 |
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 |
||
11 | public function getElementXpath($type, $count) |
||
12 | { |
||
13 | switch ($type) { |
||
14 | case ProductSummary::DESCRIPTION: |
||
15 | return $this->theme->getProductGridDescriptionXpath($count); |
||
|
|||
16 | break; |
||
17 | case ProductSummary::TITLE: |
||
18 | return $this->theme->getProductGridTitleXpath($count); |
||
19 | break; |
||
20 | case ProductSummary::COMPARE_LINK: |
||
21 | return $this->theme->getProductGridCompareLinkXpath($count); |
||
22 | break; |
||
23 | case ProductSummary::IMAGE: |
||
24 | return $this->theme->getProductGridImageXpath($count); |
||
25 | break; |
||
26 | case ProductSummary::LINK: |
||
27 | return $this->theme->getProductGridLinkXpath($count); |
||
28 | break; |
||
29 | case ProductSummary::ORIGINAL_PRICE: |
||
30 | return $this->theme->getProductGridOriginalPriceXpath($count); |
||
31 | break; |
||
32 | case ProductSummary::PRICE: |
||
33 | return $this->theme->getProductGridPriceXpath($count); |
||
34 | break; |
||
35 | case ProductSummary::WISHLIST_LINK: |
||
36 | return $this->theme->getProductGridWishlistLinkXpath($count); |
||
37 | break; |
||
38 | case ProductSummary::ADD_TO_CART_LINK: |
||
39 | return $this->theme->getProductGridAddToCartLinkXpath($count); |
||
40 | break; |
||
41 | } |
||
42 | } |
||
43 | |||
45 | } |
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 implementation 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 interface: