22/**
33 * This file is part of the leanphp/phpspec-code-coverage package
44 *
5+ * @author ek9 <dev@ek9.co>
6+ * @license MIT
7+ *
58 * For the full copyright and license information, please see the LICENSE file
69 * that was distributed with this source code.
710 *
8- * @license MIT
911 */
1012namespace LeanPHP \PhpSpec \CodeCoverage \Listener ;
1113
@@ -26,6 +28,7 @@ class CodeCoverageListener implements EventSubscriberInterface
2628 private $ io ;
2729 private $ options ;
2830 private $ enabled ;
31+ private $ skipCoverage ;
2932
3033 /**
3134 * @param ConsoleIO $io
@@ -47,7 +50,8 @@ public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $report
4750 'format ' => array ('html ' ),
4851 );
4952
50- $ this ->enabled = !$ skipCoverage && (extension_loaded ('xdebug ' ) || (PHP_SAPI === 'phpdbg ' ));
53+ $ this ->enabled = extension_loaded ('xdebug ' ) || (PHP_SAPI === 'phpdbg ' );
54+ $ this ->skipCoverage = $ skipCoverage ;
5155 }
5256
5357 /**
@@ -58,7 +62,7 @@ public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $report
5862 */
5963 public function beforeSuite (SuiteEvent $ event )
6064 {
61- if (!$ this ->enabled ) {
65+ if (!$ this ->enabled || $ this -> skipCoverage ) {
6266 return ;
6367 }
6468
@@ -87,7 +91,7 @@ public function beforeSuite(SuiteEvent $event)
8791 */
8892 public function beforeExample (ExampleEvent $ event )
8993 {
90- if (!$ this ->enabled ) {
94+ if (!$ this ->enabled || $ this -> skipCoverage ) {
9195 return ;
9296 }
9397
@@ -106,7 +110,7 @@ public function beforeExample(ExampleEvent $event)
106110 */
107111 public function afterExample (ExampleEvent $ event )
108112 {
109- if (!$ this ->enabled ) {
113+ if (!$ this ->enabled || $ this -> skipCoverage ) {
110114 return ;
111115 }
112116
@@ -118,9 +122,13 @@ public function afterExample(ExampleEvent $event)
118122 */
119123 public function afterSuite (SuiteEvent $ event )
120124 {
121- if (!$ this ->enabled ) {
125+ if (!$ this ->enabled || $ this -> skipCoverage ) {
122126 if ($ this ->io && $ this ->io ->isVerbose ()) {
123- $ this ->io ->writeln ('Did not detect Xdebug extension or phpdbg. No code coverage will be generated. ' );
127+ if (!$ this ->enabled ) {
128+ $ this ->io ->writeln ('No code coverage will be generated as neither Xdebug nor phpdbg was detected. ' );
129+ } elseif ($ this ->skipCoverage ) {
130+ $ this ->io ->writeln ('Skipping code coverage generation ' );
131+ }
124132 }
125133
126134 return ;
@@ -155,13 +163,13 @@ public function setOptions(array $options)
155163 /**
156164 * {@inheritDoc}
157165 */
158- public static function getSubscribedEvents ()
166+ public static function getSubscribedEvents (): array
159167 {
160- return array (
161- 'beforeExample ' => array ( 'beforeExample ' , -10 ) ,
162- 'afterExample ' => array ( 'afterExample ' , -10 ) ,
163- 'beforeSuite ' => array ( 'beforeSuite ' , -10 ) ,
164- 'afterSuite ' => array ( 'afterSuite ' , -10 ) ,
165- ) ;
168+ return [
169+ 'beforeExample ' => [ 'beforeExample ' , -10 ] ,
170+ 'afterExample ' => [ 'afterExample ' , -10 ] ,
171+ 'beforeSuite ' => [ 'beforeSuite ' , -10 ] ,
172+ 'afterSuite ' => [ 'afterSuite ' , -10 ] ,
173+ ] ;
166174 }
167175}
0 commit comments