@@ -70,12 +70,18 @@ final class Text
70
70
*/
71
71
private $ showOnlySummary ;
72
72
73
- public function __construct (int $ lowUpperBound = 50 , int $ highLowerBound = 90 , bool $ showUncoveredFiles = false , bool $ showOnlySummary = false )
73
+ /**
74
+ * @var bool
75
+ */
76
+ private $ determineBranchCoverage ;
77
+
78
+ public function __construct (int $ lowUpperBound = 50 , int $ highLowerBound = 90 , bool $ showUncoveredFiles = false , bool $ showOnlySummary = false , bool $ determineBranchCoverage = false )
74
79
{
75
- $ this ->lowUpperBound = $ lowUpperBound ;
76
- $ this ->highLowerBound = $ highLowerBound ;
77
- $ this ->showUncoveredFiles = $ showUncoveredFiles ;
78
- $ this ->showOnlySummary = $ showOnlySummary ;
80
+ $ this ->lowUpperBound = $ lowUpperBound ;
81
+ $ this ->highLowerBound = $ highLowerBound ;
82
+ $ this ->showUncoveredFiles = $ showUncoveredFiles ;
83
+ $ this ->showOnlySummary = $ showOnlySummary ;
84
+ $ this ->determineBranchCoverage = $ determineBranchCoverage ;
79
85
}
80
86
81
87
public function process (CodeCoverage $ coverage , bool $ showColors = false ): string
@@ -84,12 +90,14 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
84
90
$ report = $ coverage ->getReport ();
85
91
86
92
$ colors = [
87
- 'header ' => '' ,
88
- 'classes ' => '' ,
89
- 'methods ' => '' ,
90
- 'lines ' => '' ,
91
- 'reset ' => '' ,
92
- 'eol ' => '' ,
93
+ 'header ' => '' ,
94
+ 'classes ' => '' ,
95
+ 'methods ' => '' ,
96
+ 'lines ' => '' ,
97
+ 'branches ' => '' ,
98
+ 'paths ' => '' ,
99
+ 'reset ' => '' ,
100
+ 'eol ' => '' ,
93
101
];
94
102
95
103
if ($ showColors ) {
@@ -108,13 +116,25 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
108
116
$ report ->getNumExecutableLines ()
109
117
);
110
118
119
+ if ($ this ->determineBranchCoverage ) {
120
+ $ colors ['branches ' ] = $ this ->getCoverageColor (
121
+ $ report ->getNumTestedBranches (),
122
+ $ report ->getNumBranches ()
123
+ );
124
+
125
+ $ colors ['paths ' ] = $ this ->getCoverageColor (
126
+ $ report ->getNumTestedPaths (),
127
+ $ report ->getNumPaths ()
128
+ );
129
+ }
130
+
111
131
$ colors ['reset ' ] = self ::COLOR_RESET ;
112
132
$ colors ['header ' ] = self ::COLOR_HEADER ;
113
133
$ colors ['eol ' ] = self ::COLOR_EOL ;
114
134
}
115
135
116
136
$ classes = \sprintf (
117
- ' Classes: %6s (%d/%d) ' ,
137
+ ' Classes: %6s (%d/%d) ' ,
118
138
Util::percent (
119
139
$ report ->getNumTestedClassesAndTraits (),
120
140
$ report ->getNumClassesAndTraits (),
@@ -125,7 +145,7 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
125
145
);
126
146
127
147
$ methods = \sprintf (
128
- ' Methods: %6s (%d/%d) ' ,
148
+ ' Methods: %6s (%d/%d) ' ,
129
149
Util::percent (
130
150
$ report ->getNumTestedMethods (),
131
151
$ report ->getNumMethods (),
@@ -136,7 +156,7 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
136
156
);
137
157
138
158
$ lines = \sprintf (
139
- ' Lines: %6s (%d/%d) ' ,
159
+ ' Lines: %6s (%d/%d) ' ,
140
160
Util::percent (
141
161
$ report ->getNumExecutedLines (),
142
162
$ report ->getNumExecutableLines (),
@@ -146,6 +166,33 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
146
166
$ report ->getNumExecutableLines ()
147
167
);
148
168
169
+ $ paths = '' ;
170
+ $ branches = '' ;
171
+
172
+ if ($ this ->determineBranchCoverage ) {
173
+ $ branches = \sprintf (
174
+ ' Branches: %6s (%d/%d) ' ,
175
+ Util::percent (
176
+ $ report ->getNumTestedBranches (),
177
+ $ report ->getNumBranches (),
178
+ true
179
+ ),
180
+ $ report ->getNumTestedBranches (),
181
+ $ report ->getNumBranches ()
182
+ );
183
+
184
+ $ paths = \sprintf (
185
+ ' Paths: %6s (%d/%d) ' ,
186
+ Util::percent (
187
+ $ report ->getNumTestedPaths (),
188
+ $ report ->getNumPaths (),
189
+ true
190
+ ),
191
+ $ report ->getNumTestedPaths (),
192
+ $ report ->getNumPaths ()
193
+ );
194
+ }
195
+
149
196
$ padding = \max (\array_map ('strlen ' , [$ classes , $ methods , $ lines ]));
150
197
151
198
if ($ this ->showOnlySummary ) {
@@ -167,12 +214,22 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
167
214
$ output .= $ this ->format ($ colors ['methods ' ], $ padding , $ methods );
168
215
$ output .= $ this ->format ($ colors ['lines ' ], $ padding , $ lines );
169
216
217
+ if ($ this ->determineBranchCoverage ) {
218
+ $ output .= $ this ->format ($ colors ['branches ' ], $ padding , $ branches );
219
+ $ output .= $ this ->format ($ colors ['paths ' ], $ padding , $ paths );
220
+ }
221
+
170
222
if ($ this ->showOnlySummary ) {
171
223
return $ output . \PHP_EOL ;
172
224
}
173
225
174
226
$ classCoverage = [];
175
227
228
+ $ maxMethods = 0 ;
229
+ $ maxLines = 0 ;
230
+ $ maxBranches = 0 ;
231
+ $ maxPaths = 0 ;
232
+
176
233
foreach ($ report as $ item ) {
177
234
if (!$ item instanceof File) {
178
235
continue ;
@@ -185,21 +242,56 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
185
242
$ coveredClassStatements = 0 ;
186
243
$ coveredMethods = 0 ;
187
244
$ classMethods = 0 ;
245
+ $ classPaths = 0 ;
246
+ $ coveredClassPaths = 0 ;
247
+ $ classBranches = 0 ;
248
+ $ coveredClassBranches = 0 ;
188
249
189
250
foreach ($ class ['methods ' ] as $ method ) {
190
- if ($ method ['executableLines ' ] == 0 ) {
251
+ if ($ method ['executableLines ' ] === 0 ) {
191
252
continue ;
192
253
}
193
254
194
255
$ classMethods ++;
195
256
$ classStatements += $ method ['executableLines ' ];
196
257
$ coveredClassStatements += $ method ['executedLines ' ];
197
258
198
- if ($ method ['coverage ' ] == 100 ) {
259
+ if ($ this ->determineBranchCoverage ) {
260
+ $ classPaths += $ method ['executablePaths ' ];
261
+ $ coveredClassPaths += $ method ['executedPaths ' ];
262
+ $ classBranches += $ method ['executableBranches ' ];
263
+ $ coveredClassBranches += $ method ['executedBranches ' ];
264
+ }
265
+
266
+ if ($ method ['coverage ' ] === 100 ) {
199
267
$ coveredMethods ++;
200
268
}
201
269
}
202
270
271
+ $ maxMethods = \max (
272
+ $ maxMethods ,
273
+ \strlen ((string ) $ classMethods ),
274
+ \strlen ((string ) $ coveredMethods )
275
+ );
276
+ $ maxLines = \max (
277
+ $ maxLines ,
278
+ \strlen ((string ) $ classStatements ),
279
+ \strlen ((string ) $ coveredClassStatements )
280
+ );
281
+
282
+ if ($ this ->determineBranchCoverage ) {
283
+ $ maxBranches = \max (
284
+ $ maxBranches ,
285
+ \strlen ((string ) $ classBranches ),
286
+ \strlen ((string ) $ coveredClassBranches )
287
+ );
288
+ $ maxPaths = \max (
289
+ $ maxPaths ,
290
+ \strlen ((string ) $ classPaths ),
291
+ \strlen ((string ) $ coveredClassPaths )
292
+ );
293
+ }
294
+
203
295
$ namespace = '' ;
204
296
205
297
if (!empty ($ class ['package ' ]['namespace ' ])) {
@@ -215,27 +307,44 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
215
307
'methodCount ' => $ classMethods ,
216
308
'statementsCovered ' => $ coveredClassStatements ,
217
309
'statementCount ' => $ classStatements ,
310
+ 'pathsCovered ' => $ coveredClassPaths ,
311
+ 'pathCount ' => $ classPaths ,
312
+ 'branchesCovered ' => $ coveredClassBranches ,
313
+ 'branchCount ' => $ classBranches ,
218
314
];
219
315
}
220
316
}
221
317
222
318
\ksort ($ classCoverage );
223
319
224
- $ methodColor = '' ;
225
- $ linesColor = '' ;
226
- $ resetColor = '' ;
320
+ $ methodColor = '' ;
321
+ $ linesColor = '' ;
322
+ $ resetColor = '' ;
323
+ $ pathsColor = '' ;
324
+ $ branchesColor = '' ;
227
325
228
326
foreach ($ classCoverage as $ fullQualifiedPath => $ classInfo ) {
229
- if ($ this ->showUncoveredFiles || $ classInfo ['statementsCovered ' ] != 0 ) {
327
+ if ($ this ->showUncoveredFiles || $ classInfo ['statementsCovered ' ] !== 0 ) {
230
328
if ($ showColors ) {
231
- $ methodColor = $ this ->getCoverageColor ($ classInfo ['methodsCovered ' ], $ classInfo ['methodCount ' ]);
232
- $ linesColor = $ this ->getCoverageColor ($ classInfo ['statementsCovered ' ], $ classInfo ['statementCount ' ]);
233
- $ resetColor = $ colors ['reset ' ];
329
+ $ methodColor = $ this ->getCoverageColor ($ classInfo ['methodsCovered ' ], $ classInfo ['methodCount ' ]);
330
+ $ linesColor = $ this ->getCoverageColor ($ classInfo ['statementsCovered ' ], $ classInfo ['statementCount ' ]);
331
+
332
+ if ($ this ->determineBranchCoverage ) {
333
+ $ branchesColor = $ this ->getCoverageColor ($ classInfo ['branchesCovered ' ], $ classInfo ['branchCount ' ]);
334
+ $ pathsColor = $ this ->getCoverageColor ($ classInfo ['pathsCovered ' ], $ classInfo ['pathCount ' ]);
335
+ }
336
+ $ resetColor = $ colors ['reset ' ];
234
337
}
235
338
236
339
$ output .= \PHP_EOL . $ fullQualifiedPath . \PHP_EOL
237
- . ' ' . $ methodColor . 'Methods: ' . $ this ->printCoverageCounts ($ classInfo ['methodsCovered ' ], $ classInfo ['methodCount ' ], 2 ) . $ resetColor . ' '
238
- . ' ' . $ linesColor . 'Lines: ' . $ this ->printCoverageCounts ($ classInfo ['statementsCovered ' ], $ classInfo ['statementCount ' ], 3 ) . $ resetColor ;
340
+ . ' ' . $ methodColor . 'Methods: ' . $ this ->printCoverageCounts ($ classInfo ['methodsCovered ' ], $ classInfo ['methodCount ' ], $ maxMethods ) . $ resetColor . ' '
341
+ . ' ' . $ linesColor . 'Lines: ' . $ this ->printCoverageCounts ($ classInfo ['statementsCovered ' ], $ classInfo ['statementCount ' ], $ maxLines ) . $ resetColor ;
342
+
343
+ if ($ this ->determineBranchCoverage ) {
344
+ $ output .= ''
345
+ . ' ' . $ branchesColor . 'Branches: ' . $ this ->printCoverageCounts ($ classInfo ['branchesCovered ' ], $ classInfo ['branchCount ' ], $ maxBranches ) . $ resetColor . ' '
346
+ . ' ' . $ pathsColor . 'Paths: ' . $ this ->printCoverageCounts ($ classInfo ['pathsCovered ' ], $ classInfo ['pathCount ' ], $ maxPaths ) . $ resetColor ;
347
+ }
239
348
}
240
349
}
241
350
0 commit comments