| Conditions | 3 | 
| Paths | 4 | 
| Total Lines | 77 | 
| Code Lines | 42 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 4 | ||
| Bugs | 0 | Features | 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 | ||
| 42 | public function testSettingsAction() | ||
| 43 |     { | ||
| 44 | $this->getManager(); | ||
| 45 | |||
| 46 | /** @var Client $client */ | ||
| 47 |         $client = $this->client->loginAction('test', 'test'); | ||
| 48 | |||
| 49 | // Visit settings page. | ||
| 50 |         $crawler = $client->request('GET', '/settings/settings'); | ||
| 51 | |||
| 52 | // Assert categories are rendered. | ||
| 53 | /** @var array $categories */ | ||
| 54 |         $categories = $client->getContainer()->getParameter('ongr_settings.settings.categories'); | ||
| 55 | $content = $client->getResponse()->getContent(); | ||
| 56 | |||
| 57 | // Print $content. | ||
| 58 |         foreach ($categories as $category) { | ||
| 59 | $this->assertContains($category['name'], $content); | ||
| 60 | } | ||
| 61 | |||
| 62 | // Submit settings form. | ||
| 63 |         $buttonNode = $crawler->selectButton('settings_submit'); | ||
| 64 | $form = $buttonNode->form(); | ||
| 65 | |||
| 66 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 67 | $form['settings[foo_setting_1]']->tick(); | ||
| 68 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 69 | $form['settings[foo_setting_2]']->untick(); | ||
| 70 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 71 | $form['settings[foo_setting_3]']->tick(); | ||
| 72 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 73 | $form['settings[ongr_settings_profile_Acme2]']->tick(); | ||
| 74 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 75 | $form['settings[ongr_settings_profile_Acme1]']->tick(); | ||
| 76 | /** @noinspection PhpUndefinedMethodInspection */ | ||
| 77 | $form['settings[ongr_settings_live_settings]']->untick(); | ||
| 78 | $client->submit($form); | ||
| 79 | |||
| 80 | // Assert successful redirect. | ||
| 81 | $this->assertStringEndsWith( | ||
| 82 | 'settings', | ||
| 83 | $client->getRequest()->getUri(), | ||
| 84 | 'response must be a correct redirect' | ||
| 85 | ); | ||
| 86 | |||
| 87 | // Assert cookie values updated. | ||
| 88 | $cookieValue = $client | ||
| 89 | ->getCookieJar() | ||
| 90 |             ->get($client->getContainer()->getParameter('ongr_settings.settings.settings_cookie.name')) | ||
| 91 | ->getValue(); | ||
| 92 | |||
| 93 | $expectedValue = [ | ||
| 94 | 'foo_setting_1' => true, | ||
| 95 | 'foo_setting_2' => false, | ||
| 96 | 'foo_setting_3' => true, | ||
| 97 | 'ongr_settings_profile_Acme2' => true, | ||
| 98 | 'ongr_settings_profile_Acme1' => true, | ||
| 99 | 'ongr_settings_live_settings' => false, | ||
| 100 | ]; | ||
| 101 | $this->assertJsonStringEqualsJsonString(json_encode($expectedValue), $cookieValue); | ||
| 102 | |||
| 103 | // Try to change value through change setting action. | ||
| 104 | $data[0] = ['foo_setting_1', false]; | ||
|  | |||
| 105 | $data[1] = ['foo_setting_non_existent', false ]; | ||
| 106 | |||
| 107 |         foreach ($data as $case) { | ||
| 108 |             $client->request('get', '/admin/setting/change/' . base64_encode($case[0])); | ||
| 109 | |||
| 110 | // Assert cookie values updated. | ||
| 111 | $cookieValue = $client | ||
| 112 | ->getCookieJar() | ||
| 113 |                 ->get($client->getContainer()->getParameter('ongr_settings.settings.settings_cookie.name')) | ||
| 114 | ->getValue(); | ||
| 115 | |||
| 116 | $this->assertJsonStringEqualsJsonString(json_encode($expectedValue), $cookieValue); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 182 | 
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.