Total Complexity | 49 |
Total Lines | 330 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TestHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TestHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class TestHelper |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $testName; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $exitCode = 0; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $timestamp; |
||
41 | |||
42 | /** |
||
43 | * TestHelper constructor. |
||
44 | * |
||
45 | * @param string $testName |
||
46 | * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
||
47 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
48 | */ |
||
49 | public function __construct(string $testName) |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
||
66 | * @throws \Phpfastcache\Exceptions\PhpfastcacheLogicException |
||
67 | */ |
||
68 | public function printHeaders() |
||
69 | { |
||
70 | if(!$this->isCli() && !\headers_sent()){ |
||
71 | \header('Content-Type: text/plain, true'); |
||
72 | } |
||
73 | |||
74 | $this->printText('[PhpFastCache CORE v' . Api::getPhpFastCacheVersion() . Api::getPhpFastCacheGitHeadHash() . ']', true); |
||
75 | $this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true); |
||
76 | $this->printText('[PHP v' . PHP_VERSION . ']', true); |
||
77 | $this->printText("[Begin Test: '{$this->testName}']"); |
||
78 | $this->printText('---'); |
||
79 | |||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return int |
||
84 | */ |
||
85 | public function getExitCode(): int |
||
86 | { |
||
87 | return $this->exitCode; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function resetExitCode(): self |
||
94 | { |
||
95 | $this->exitCode = 0; |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $string |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function printSkipText(string $string): self |
||
105 | { |
||
106 | $this->printText($string, false, 'SKIP'); |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $string |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function printPassText(string $string): self |
||
116 | { |
||
117 | $this->printText($string, false, 'PASS'); |
||
118 | |||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string printFailText |
||
|
|||
125 | * @return $this |
||
126 | */ |
||
127 | public function printInfoText(string $string): self |
||
128 | { |
||
129 | $this->printText($string, false, 'INFO'); |
||
130 | |||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $string |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function printDebugText(string $string): self |
||
140 | { |
||
141 | $this->printText($string, false, 'DEBUG'); |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string $string |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function printNoteText(string $string): self |
||
151 | { |
||
152 | $this->printText($string, false, 'NOTE'); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param string $string |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function printFailText(string $string): self |
||
162 | { |
||
163 | $this->printText($string, false, 'FAIL'); |
||
164 | $this->exitCode = 1; |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param int $count |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function printNewLine(int $count = 1): self |
||
174 | { |
||
175 | print \str_repeat(PHP_EOL, $count); |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $string |
||
181 | * @param bool $strtoupper |
||
182 | * @param string $prefix |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function printText(string $string, bool $strtoupper = false, string $prefix = ''): self |
||
186 | { |
||
187 | if ($prefix) { |
||
188 | $string = "[{$prefix}] {$string}"; |
||
189 | } |
||
190 | if (!$strtoupper) { |
||
191 | print \trim($string) . PHP_EOL; |
||
192 | } else { |
||
193 | print \strtoupper(\trim($string) . PHP_EOL); |
||
194 | } |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $cmd |
||
201 | */ |
||
202 | public function runAsyncProcess(string $cmd) |
||
203 | { |
||
204 | if (\substr(\php_uname(), 0, 7) === 'Windows') { |
||
205 | \pclose(\popen('start /B ' . $cmd, 'r')); |
||
206 | } else { |
||
207 | \exec($cmd . ' > /dev/null &'); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $file |
||
213 | * @param string $ext |
||
214 | */ |
||
215 | public function runSubProcess(string $file, string $ext = '.php') |
||
216 | { |
||
217 | $this->runAsyncProcess(($this->isHHVM() ? 'hhvm ' : 'php ') . \getcwd() . \DIRECTORY_SEPARATOR . 'subprocess' . \DIRECTORY_SEPARATOR . $file . '.subprocess' . $ext); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return void |
||
222 | */ |
||
223 | public function terminateTest() |
||
224 | { |
||
225 | $execTime = \round(\microtime(true) - $this->timestamp, 3); |
||
226 | |||
227 | $this->printText('Test finished in ' . $execTime . 's'); |
||
228 | exit($this->exitCode); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function isHHVM(): bool |
||
235 | { |
||
236 | return \defined('HHVM_VERSION'); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param $obj |
||
241 | * @param $prop |
||
242 | * @return mixed |
||
243 | * @throws \ReflectionException |
||
244 | */ |
||
245 | public function accessInaccessibleMember($obj, $prop) |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param \Throwable $exception |
||
255 | */ |
||
256 | public function exceptionHandler(\Throwable $exception) |
||
257 | { |
||
258 | if ($exception instanceof PhpfastcacheDriverCheckException) { |
||
259 | $this->printSkipText('A driver could not be initialized due to missing requirement: ' . $exception->getMessage()); |
||
260 | } else { |
||
261 | $this->printFailText(\sprintf( |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @param int $errno |
||
274 | * @param string $errstr |
||
275 | * @param string $errfile |
||
276 | * @param int $errline |
||
277 | */ |
||
278 | public function errorHandler(int $errno, string $errstr, string $errfile, int $errline) |
||
324 | )); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @see https://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function isCli(): bool |
||
355 | } |
||
356 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths