1515 */
1616class Collector
1717{
18+ public const
19+ ENGINE_PHPDBG = 'PHPDBG ' ,
20+ ENGINE_XDEBUG = 'Xdebug ' ;
21+
1822 /** @var resource */
1923 private static $ file ;
2024
2125 /** @var string */
2226 private static $ collector ;
2327
2428
29+ public static function detectEngines (): array
30+ {
31+ return array_filter ([
32+ defined ('PHPDBG_VERSION ' ) ? self ::ENGINE_PHPDBG : null ,
33+ extension_loaded ('xdebug ' ) ? self ::ENGINE_XDEBUG : null ,
34+ ]);
35+ }
36+
37+
2538 public static function isStarted (): bool
2639 {
2740 return self ::$ file !== null ;
@@ -32,23 +45,26 @@ public static function isStarted(): bool
3245 * Starts gathering the information for code coverage.
3346 * @throws \LogicException
3447 */
35- public static function start (string $ file ): void
48+ public static function start (string $ file, string $ engine ): void
3649 {
3750 if (self ::isStarted ()) {
3851 throw new \LogicException ('Code coverage collector has been already started. ' );
3952 }
4053 self ::$ file = fopen ($ file , 'c+ ' );
4154
42- if (defined ('PHPDBG_VERSION ' )) {
43- phpdbg_start_oplog ();
44- self ::$ collector = 'collectPhpDbg ' ;
55+ switch ($ engine ) {
56+ case self ::ENGINE_PHPDBG :
57+ phpdbg_start_oplog ();
58+ self ::$ collector = 'collectPhpDbg ' ;
59+ break ;
4560
46- } elseif (extension_loaded ('xdebug ' )) {
47- xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
48- self ::$ collector = 'collectXdebug ' ;
61+ case self ::ENGINE_XDEBUG :
62+ xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
63+ self ::$ collector = 'collectXdebug ' ;
64+ break ;
4965
50- } else {
51- throw new \LogicException (' Code coverage functionality requires Xdebug extension or phpdbg SAPI. ' );
66+ default :
67+ throw new \LogicException (" Code coverage engine ' $ engine ' is not supported. " );
5268 }
5369
5470 register_shutdown_function (function (): void {
0 commit comments