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+ $ engines = [];
32+ if (defined ('PHPDBG_VERSION ' )) {
33+ $ engines [] = self ::ENGINE_PHPDBG ;
34+ }
35+
36+ if (extension_loaded ('xdebug ' )) {
37+ $ engines [] = self ::ENGINE_XDEBUG ;
38+ }
39+
40+ return $ engines ;
41+ }
42+
43+
2544 public static function isStarted (): bool
2645 {
2746 return self ::$ file !== null ;
@@ -32,23 +51,26 @@ public static function isStarted(): bool
3251 * Starts gathering the information for code coverage.
3352 * @throws \LogicException
3453 */
35- public static function start (string $ file ): void
54+ public static function start (string $ file, string $ engine ): void
3655 {
3756 if (self ::isStarted ()) {
3857 throw new \LogicException ('Code coverage collector has been already started. ' );
3958 }
4059 self ::$ file = fopen ($ file , 'c+ ' );
4160
42- if (defined ('PHPDBG_VERSION ' )) {
43- phpdbg_start_oplog ();
44- self ::$ collector = 'collectPhpDbg ' ;
61+ switch ($ engine ) {
62+ case self ::ENGINE_PHPDBG :
63+ phpdbg_start_oplog ();
64+ self ::$ collector = 'collectPhpDbg ' ;
65+ break ;
4566
46- } elseif (extension_loaded ('xdebug ' )) {
47- xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
48- self ::$ collector = 'collectXdebug ' ;
67+ case self ::ENGINE_XDEBUG :
68+ xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE );
69+ self ::$ collector = 'collectXdebug ' ;
70+ break ;
4971
50- } else {
51- throw new \LogicException (' Code coverage functionality requires Xdebug extension or phpdbg SAPI. ' );
72+ default :
73+ throw new \LogicException (" Code coverage engine ' $ engine ' is not supported. " );
5274 }
5375
5476 register_shutdown_function (function (): void {
0 commit comments