Conditions | 7 |
Paths | 34 |
Total Lines | 100 |
Code Lines | 66 |
Lines | 17 |
Ratio | 17 % |
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 |
||
189 | public function testIntegration() |
||
190 | { |
||
191 | if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) |
||
192 | $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set'); |
||
193 | |||
194 | $credentials = $this->loadCredentialsFromFile(); |
||
195 | |||
196 | $optimizelyClient = new OptimizelyApiClient($credentials, 'v2'); |
||
197 | $this->assertTrue($optimizelyClient!=null); |
||
198 | |||
199 | // Create new project |
||
200 | $curDate = date('Y-m-d H:i:s'); |
||
201 | $newProject = new Project(array( |
||
202 | "name" => "Test Project $curDate", |
||
203 | "account_id" => 12345, |
||
204 | "confidence_threshold" => 0.9, |
||
205 | "platform" => "web", |
||
206 | "status" => "active", |
||
207 | "web_snippet" => array( |
||
208 | "enable_force_variation" => false, |
||
209 | "exclude_disabled_experiments" => false, |
||
210 | "exclude_names" => true, |
||
211 | "include_jquery" => true, |
||
212 | "ip_anonymization" => false, |
||
213 | "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$", |
||
214 | "library" => "jquery-1.11.3-trim", |
||
215 | "project_javascript" => "alert(\"Active Experiment\")" |
||
216 | ) |
||
217 | )); |
||
218 | |||
219 | $result = $optimizelyClient->projects()->create($newProject); |
||
220 | $createdProject = $result->getPayload(); |
||
221 | |||
222 | // Create new attribute in the project |
||
223 | $attribute = new Attribute(array( |
||
224 | "key" => "subscriber_status", |
||
225 | "project_id" => 0, |
||
226 | "archived" => false, |
||
227 | "description" => "string", |
||
228 | "name" => "Subscriber Status", |
||
229 | "condition_type" => "custom_attribute", |
||
230 | "id" => 0, |
||
231 | "last_modified" => "2017-05-08T03:34:01.035Z" |
||
232 | )); |
||
233 | |||
234 | $result = $optimizelyClient->attributes()->create($attribute); |
||
235 | $createdAttribute = $result->getPayload(); |
||
236 | |||
237 | $this->assertTrue($createdAttribute instanceOf Attribute); |
||
238 | $this->assertTrue($createdAttribute->getName()=='Subscriber Status'); |
||
239 | |||
240 | // List all existing attributes and try to find the created attribute |
||
241 | $attributeFound = false; |
||
242 | try { |
||
243 | $page = 1; |
||
244 | View Code Duplication | for (;;) { |
|
245 | $result = $optimizelyClient->attributes()->listAll($createdAttribute->getId(), $page); |
||
246 | |||
247 | $attributes = $result->getPayload(); |
||
248 | |||
249 | foreach ($attributes as $attribute) { |
||
250 | if ($audience->getName()=="Subscriber Status") { |
||
251 | $attributeFound = true; |
||
252 | break; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | if ($result->getNextPage()==null) |
||
257 | break; |
||
258 | |||
259 | $page ++; |
||
260 | } |
||
261 | } |
||
262 | catch (Exception $e) { |
||
263 | // Handle error. |
||
264 | $code = $e->getCode(); |
||
265 | $httpCode = $e->getHttpCode(); |
||
266 | $message = $e->getMessage(); |
||
267 | $uuid = $e->getUuid(); |
||
268 | echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n"; |
||
269 | } |
||
270 | |||
271 | $this->assertTrue($attributeFound); |
||
272 | |||
273 | // Update attribute |
||
274 | $createdAttribute->setName('Some new attribute name'); |
||
275 | $result = $optimizelyClient->attributes()->update($createdAttribute->getId(), $createdAttribute); |
||
276 | $updatedAttribute = $result->getPayload(); |
||
277 | |||
278 | $this->assertTrue($updatedAttribute instanceOf Attribute); |
||
279 | $this->assertTrue($updatedAttribute->getName()=='Some new attribute name'); |
||
280 | |||
281 | // Make project archived |
||
282 | |||
283 | $createdProject->setStatus('archived'); |
||
284 | $result = $optimizelyClient->projects()->update($createdProject->getId(), $createdProject); |
||
285 | $updatedProject = $result->getPayload(); |
||
286 | |||
287 | $this->assertEquals('archived', $updatedProject->getStatus()); |
||
288 | } |
||
289 | } |
||
291 |
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.