Conditions | 15 |
Paths | 84 |
Total Lines | 50 |
Lines | 50 |
Ratio | 100 % |
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 |
||
78 | View Code Duplication | public function set($user = NULL, $profile = NULL, $name = NULL, $value = NULL) { |
|
79 | |||
80 | // Check if the type of the variables is valid. |
||
81 | if (!is_string($user) && ($user != NULL)) { |
||
82 | throw new InvalidArgumentException("The type of the user variable is not valid."); |
||
83 | } |
||
84 | if (!is_string($profile) && ($profile != NULL)) { |
||
85 | throw new InvalidArgumentException("The type of the profile variable is not valid."); |
||
86 | } |
||
87 | if (!is_string($name) && ($name != NULL)) { |
||
88 | throw new InvalidArgumentException("The type of the name variable is not valid."); |
||
89 | } |
||
90 | if (!is_string($value) && ($value != NULL)) { |
||
91 | throw new InvalidArgumentException("The type of the value variable is not valid."); |
||
92 | } |
||
93 | |||
94 | // Set the arguments of the request |
||
95 | $arguments = array(); |
||
96 | |||
97 | if ($user != NULL) { |
||
98 | $arguments["user"] = $user; |
||
99 | } |
||
100 | if ($profile != NULL) { |
||
101 | $arguments["profile"] = $profile; |
||
102 | } |
||
103 | if ($name != NULL) { |
||
104 | $arguments["name"] = $name; |
||
105 | } |
||
106 | if ($value != NULL) { |
||
107 | $arguments["value"] = $value; |
||
108 | } |
||
109 | |||
110 | $this->setUrl("users.profile.set", $arguments); |
||
111 | |||
112 | // Send the request |
||
113 | try { |
||
114 | $client = new \GuzzleHttp\Client(); |
||
115 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
116 | $response = json_decode( $json_response->getBody() ); |
||
117 | } |
||
118 | catch (RequestException $e) { |
||
119 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
120 | } |
||
121 | |||
122 | if($response->{'ok'} === FALSE) { |
||
123 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
124 | } |
||
125 | |||
126 | return $json_response->getBody(); |
||
127 | } |
||
128 | } |
||
129 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.