Conditions | 2 |
Paths | 2 |
Total Lines | 73 |
Code Lines | 43 |
Lines | 3 |
Ratio | 4.11 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
25 | public function testExistingConfigurationFile() |
||
26 | { |
||
27 | $this->file = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME; |
||
|
|||
28 | |||
29 | // Dump the configuration set by the enduser |
||
30 | $configuration = [ |
||
31 | 'urls' => [ |
||
32 | 'example.com' => [ |
||
33 | 'url' => 'https://www.example.com', |
||
34 | 'timeout' => 1, |
||
35 | 'status_code' => 200, |
||
36 | ], |
||
37 | ], |
||
38 | ]; |
||
39 | |||
40 | $content = ''; |
||
41 | View Code Duplication | foreach ($configuration as $name => $section) { |
|
42 | $content .= Yaml::dump([$name => $section], 4).PHP_EOL; |
||
43 | } |
||
44 | |||
45 | file_put_contents($this->file, $content); |
||
46 | |||
47 | //Test if the enduser configuration is kept by the ConfigurationLoader |
||
48 | $configurationLoader = new ConfigurationLoader(); |
||
49 | |||
50 | $configurationDumper = new ConfigurationDumper(); |
||
51 | $filesystem = new Filesystem(); |
||
52 | |||
53 | $argsMock = $this->prophesize(Args::class); |
||
54 | $argsMock |
||
55 | ->getOption(Argument::type('string')) |
||
56 | ->willReturn(sys_get_temp_dir()); |
||
57 | |||
58 | $ioMock = $this->prophesize(IO::class); |
||
59 | $ioMock |
||
60 | ->writeLine(Argument::type('string')) |
||
61 | ->shouldBeCalled(); |
||
62 | |||
63 | $commandMock = $this->prophesize(Command::class); |
||
64 | |||
65 | $initHandler = new InitHandler( |
||
66 | $configurationLoader, |
||
67 | $configurationDumper, |
||
68 | $filesystem |
||
69 | ); |
||
70 | |||
71 | $initHandler->handle( |
||
72 | $argsMock->reveal(), |
||
73 | $ioMock->reveal(), |
||
74 | $commandMock->reveal() |
||
75 | ); |
||
76 | |||
77 | // Test the configuration of the enduser file |
||
78 | $this->assertEquals( |
||
79 | $this->file, |
||
80 | $configurationLoader->getConfigurationFilepath() |
||
81 | ); |
||
82 | |||
83 | $this->assertEquals( |
||
84 | [ |
||
85 | 'urls' => [ |
||
86 | 'example.com' => [ |
||
87 | 'url' => 'https://www.example.com', |
||
88 | 'timeout' => 1, |
||
89 | 'status_code' => 200, |
||
90 | ], |
||
91 | ], |
||
92 | ], |
||
93 | Yaml::parse(file_get_contents($this->file)) |
||
94 | ); |
||
95 | |||
96 | unlink($this->file); |
||
97 | } |
||
98 | |||
155 |
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: