| Conditions | 2 |
| Paths | 2 |
| Total Lines | 93 |
| Code Lines | 58 |
| Lines | 3 |
| Ratio | 3.23 % |
| Changes | 1 | ||
| 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 |
||
| 26 | public function testExistingConfigurationFile() |
||
| 27 | { |
||
| 28 | |||
| 29 | $this->file = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME; |
||
|
|
|||
| 30 | |||
| 31 | // Dump the configuration set by the enduser |
||
| 32 | $configuration = [ |
||
| 33 | 'server' => [ |
||
| 34 | 'hostname' => null, |
||
| 35 | 'port' => null, |
||
| 36 | 'use_ssl' => null, |
||
| 37 | 'auth' => [ |
||
| 38 | 'username' => null, |
||
| 39 | 'password' => null, |
||
| 40 | ], |
||
| 41 | ], |
||
| 42 | 'urls' => [ |
||
| 43 | 'example.com' => [ |
||
| 44 | 'url' => 'https://www.example.com', |
||
| 45 | 'timeout' => 1, |
||
| 46 | 'status_code' => 200, |
||
| 47 | 'service_uid' => '', |
||
| 48 | ], |
||
| 49 | ], |
||
| 50 | ]; |
||
| 51 | |||
| 52 | $content = ''; |
||
| 53 | View Code Duplication | foreach ($configuration as $name => $section) { |
|
| 54 | $content .= Yaml::dump([$name => $section], 4).PHP_EOL; |
||
| 55 | } |
||
| 56 | |||
| 57 | file_put_contents($this->file, $content); |
||
| 58 | |||
| 59 | //Test if the enduser configuration is kept by the ConfigurationLoader |
||
| 60 | $configurationLoader = new ConfigurationLoader( |
||
| 61 | sys_get_temp_dir(), |
||
| 62 | Monitor::CONFIG_FILENAME |
||
| 63 | ); |
||
| 64 | |||
| 65 | $configurationDumper = new ConfigurationDumper(); |
||
| 66 | $filesystem = new Filesystem(); |
||
| 67 | |||
| 68 | $argsMock = $this->prophesize(Args::class); |
||
| 69 | $ioMock = $this->prophesize(IO::class); |
||
| 70 | $ioMock |
||
| 71 | ->writeLine(Argument::type('string')) |
||
| 72 | ->shouldBeCalled(); |
||
| 73 | |||
| 74 | $commandMock = $this->prophesize(Command::class); |
||
| 75 | |||
| 76 | $initHandler = new InitHandler( |
||
| 77 | $configurationLoader, |
||
| 78 | $configurationDumper, |
||
| 79 | $filesystem |
||
| 80 | ); |
||
| 81 | |||
| 82 | $initHandler->handle( |
||
| 83 | $argsMock->reveal(), |
||
| 84 | $ioMock->reveal(), |
||
| 85 | $commandMock->reveal() |
||
| 86 | ); |
||
| 87 | |||
| 88 | // Test the configuration of the enduser file |
||
| 89 | $this->assertEquals( |
||
| 90 | $this->file, |
||
| 91 | $configurationLoader->getConfigurationFilepath() |
||
| 92 | ); |
||
| 93 | |||
| 94 | $this->assertEquals( |
||
| 95 | [ |
||
| 96 | 'server' => [ |
||
| 97 | 'hostname' => null, |
||
| 98 | 'port' => null, |
||
| 99 | 'use_ssl' => null, |
||
| 100 | 'auth' => [ |
||
| 101 | 'username' => null, |
||
| 102 | 'password' => null, |
||
| 103 | ], |
||
| 104 | ], |
||
| 105 | 'urls' => [ |
||
| 106 | 'example.com' => [ |
||
| 107 | 'url' => 'https://www.example.com', |
||
| 108 | 'timeout' => 1, |
||
| 109 | 'status_code' => 200, |
||
| 110 | 'service_uid' => '', |
||
| 111 | ], |
||
| 112 | ], |
||
| 113 | ], |
||
| 114 | Yaml::parse(file_get_contents($this->file)) |
||
| 115 | ); |
||
| 116 | |||
| 117 | unlink($this->file); |
||
| 118 | } |
||
| 119 | |||
| 187 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: