| Conditions | 9 |
| Paths | 36 |
| Total Lines | 52 |
| Code Lines | 32 |
| Lines | 18 |
| Ratio | 34.62 % |
| 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 |
||
| 92 | public function vote_vendor(Request $request, $vendor) |
||
| 93 | { |
||
| 94 | if($request->get('like_type') == 'like') |
||
| 95 | { |
||
| 96 | View Code Duplication | if($vendor->wasLiked('like')) { |
|
| 97 | $vendor->unlike('like'); |
||
| 98 | } else { |
||
| 99 | if($vendor->wasLiked('dislike')) |
||
| 100 | $vendor->unlike('dislike'); |
||
| 101 | |||
| 102 | $vendor->like('like'); |
||
| 103 | } |
||
| 104 | View Code Duplication | } else { |
|
| 105 | if($vendor->wasLiked('dislike')) { |
||
| 106 | $vendor->unlike('dislike'); |
||
| 107 | } else { |
||
| 108 | if($vendor->wasLiked('like')) |
||
| 109 | $vendor->unlike('like'); |
||
| 110 | |||
| 111 | $vendor->like('dislike'); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | if($vendor->wasLiked('like')){ |
||
| 115 | |||
| 116 | $was_liked = 'like'; |
||
| 117 | } |
||
| 118 | elseif($vendor->wasLiked('dislike')) { |
||
| 119 | |||
| 120 | $was_liked = 'unlike'; |
||
| 121 | } |
||
| 122 | else{ |
||
| 123 | $was_liked = "not_liked"; |
||
| 124 | } |
||
| 125 | |||
| 126 | if($vendor->likes()->count()) |
||
| 127 | { |
||
| 128 | $positive_like_percent = |
||
| 129 | ($vendor->likes()->count() - $vendor->getLikes('dislike')->count()) / $vendor->likes()->count() * 100; |
||
| 130 | } |
||
| 131 | else { |
||
| 132 | $positive_like_percent = 0; |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | return json_encode([ |
||
| 137 | 'likes' => $vendor->getLikes('like')->count(), |
||
| 138 | 'dislikes' => $vendor->getLikes('dislike')->count(), |
||
| 139 | 'likes_count' => $vendor->likes()->count(), |
||
| 140 | 'likes_percent' => $positive_like_percent, |
||
| 141 | 'was_liked' => $was_liked |
||
| 142 | ]); |
||
| 143 | } |
||
| 144 | } |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.