Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class SelfTestCliArguments extends AbstractSelfTestCli |
||
29 | { |
||
30 | /** |
||
31 | * The output buffer to keep track of the detection. |
||
32 | * |
||
33 | * @var BufferedOutput |
||
34 | */ |
||
35 | private $log; |
||
36 | |||
37 | /** |
||
38 | * Check that we have a correct CLI executable of PHP. |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function doTest() |
||
43 | { |
||
44 | $this->setMessage('Check which arguments to pass to the PHP CLI executable.'); |
||
45 | |||
46 | if (!$this->hasInterpreter()) { |
||
47 | $this->markFailed('No PHP interpreter detected, can not test.'); |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | $this->log = new BufferedOutput(); |
||
52 | |||
53 | if ($this->check()) { |
||
54 | $data = $this->log->fetch(); |
||
55 | if (empty($data)) { |
||
56 | $data = 'No special arguments needed.'; |
||
57 | } |
||
58 | $this->markSuccess($data); |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | $this->markWarning( |
||
63 | 'Could not determine command line arguments, leaving unconfigured and hope the best.' |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Check the needed parameters. |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | private function check() |
||
78 | |||
79 | /** |
||
80 | * Test if raising the memory limit is needed. |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | private function testMemoryLimit() |
||
85 | { |
||
86 | $output = $this->testCliRuntime('echo ini_get(\'memory_limit\');'); |
||
87 | if ('-1' !== $output && ($this->memoryInBytes($output) < 2 * 1024 * 1024 * 1024)) { |
||
88 | View Code Duplication | if ($this->testOverride('echo ini_get(\'memory_limit\');', '-d memory_limit=2G', '2G')) { |
|
89 | $this->getAutoConfig()->addCommandLineArgument('-d memory_limit=2G'); |
||
90 | $this->log->writeln('Will override memory_limit of ' . $output . ' with 2G.'); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return true; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Test if raising the memory limit is needed. |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | private function testMaxExecutionTime() |
||
103 | { |
||
104 | $output = $this->testCliRuntime('echo ini_get(\'max_execution_time\');'); |
||
105 | if ((0 !== intval($output)) && (900 > intval($output))) { |
||
106 | View Code Duplication | if ($this->testOverride('echo ini_get(\'max_execution_time\');', '-d max_execution_time=900', '900')) { |
|
107 | $this->getAutoConfig()->addCommandLineArgument('-d max_execution_time=900'); |
||
108 | $this->log->writeln('Will override max_execution_time of ' . $output . ' with 900 seconds.'); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Test if overriding a parameter works. |
||
117 | * |
||
118 | * @param string $script The script to run. |
||
119 | * |
||
120 | * @param string $definition The argument to pass. |
||
121 | * |
||
122 | * @param string $expectedValue The expected output value. |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | private function testOverride($script, $definition, $expectedValue) |
||
136 | |||
137 | /** |
||
138 | * Convert a human readable memory amount to the exact byte count. |
||
139 | * |
||
140 | * @param string $value The human readable memory string. |
||
141 | * |
||
142 | * @return int |
||
143 | */ |
||
144 | private function memoryInBytes($value) |
||
163 | } |
||
164 |