Conditions | 5 |
Paths | 46 |
Total Lines | 72 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
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 |
||
20 | public function testValidationScenario($fileContents, $expectedFileContents = null, $expectedException = null) |
||
21 | { |
||
22 | $createdFiles = []; |
||
23 | |||
24 | try { |
||
25 | $createdFiles = $this->createFiles($fileContents); |
||
26 | |||
27 | $fileList = new Change\FileList( |
||
28 | 'cache' . mt_rand(), |
||
29 | function () use ($createdFiles) { |
||
30 | return $createdFiles; |
||
31 | } |
||
32 | ); |
||
33 | |||
34 | $binFile = realpath(__DIR__ . '/../../../../vendor/friendsofphp/php-cs-fixer/php-cs-fixer'); |
||
35 | $this->assertNotFalse($binFile, 'PHP-CS-Fixer executable could not be located (cwd: ' . getcwd() . ').'); |
||
36 | |||
37 | $configFile = tempnam(sys_get_temp_dir(), 'pcc'); |
||
38 | $this->assertNotFalse( |
||
39 | file_put_contents( |
||
40 | $configFile, |
||
41 | '<?php return Symfony\CS\Config\Config::create()' |
||
42 | . '->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)' |
||
43 | . '->fixers(["linefeed"]);' |
||
44 | ), |
||
45 | 'PHP-CS-Fixer config file could not be saved: ' . $configFile |
||
46 | ); |
||
47 | |||
48 | $action = new PhpCsFixer($fileList, $binFile, $configFile, false); |
||
49 | |||
50 | try { |
||
51 | $action->execute($this->getInputMock(), $this->getOutputMock()); |
||
52 | |||
53 | if (!is_null($expectedFileContents)) { |
||
54 | $this->assertEquals( |
||
55 | $expectedFileContents, |
||
56 | array_combine( |
||
57 | array_keys($fileContents), |
||
58 | array_map( |
||
59 | function ($file) { |
||
60 | return file($file, FILE_IGNORE_NEW_LINES); |
||
61 | }, |
||
62 | $createdFiles |
||
63 | ) |
||
64 | ) |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | $this->assertNull($expectedException, 'No exception should be thrown.'); |
||
69 | } catch (\Exception $ex) { |
||
70 | if (!$expectedException) { |
||
71 | throw $ex; |
||
72 | } |
||
73 | |||
74 | // replace virtual filenames with fake filenames |
||
75 | $message = str_replace( |
||
76 | $createdFiles, |
||
77 | array_keys($fileContents), |
||
78 | $ex->getMessage() |
||
79 | ); |
||
80 | |||
81 | // do some asserting :) |
||
82 | $this->assertSame(get_class($expectedException), get_class($ex)); |
||
83 | $this->assertSame($expectedException->getMessage(), $message); |
||
84 | } |
||
85 | |||
86 | $this->removeFiles($createdFiles); |
||
87 | } catch (\Exception $ex) { |
||
88 | $this->removeFiles($createdFiles); |
||
89 | throw $ex; |
||
90 | } |
||
91 | } |
||
92 | |||
183 |