Version 1
[yaffs-website] / vendor / phpunit / php-code-coverage / src / CodeCoverage / Report / Text.php
1 <?php
2 /*
3  * This file is part of the PHP_CodeCoverage package.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Generates human readable output from an PHP_CodeCoverage object.
13  *
14  * The output gets put into a text file our written to the CLI.
15  *
16  * @since Class available since Release 1.1.0
17  */
18 class PHP_CodeCoverage_Report_Text
19 {
20     protected $lowUpperBound;
21     protected $highLowerBound;
22     protected $showUncoveredFiles;
23     protected $showOnlySummary;
24
25     protected $colors = array(
26         'green'  => "\x1b[30;42m",
27         'yellow' => "\x1b[30;43m",
28         'red'    => "\x1b[37;41m",
29         'header' => "\x1b[1;37;40m",
30         'reset'  => "\x1b[0m",
31         'eol'    => "\x1b[2K",
32     );
33
34     public function __construct($lowUpperBound, $highLowerBound, $showUncoveredFiles, $showOnlySummary)
35     {
36         $this->lowUpperBound      = $lowUpperBound;
37         $this->highLowerBound     = $highLowerBound;
38         $this->showUncoveredFiles = $showUncoveredFiles;
39         $this->showOnlySummary    = $showOnlySummary;
40     }
41
42     /**
43      * @param  PHP_CodeCoverage $coverage
44      * @param  bool             $showColors
45      * @return string
46      */
47     public function process(PHP_CodeCoverage $coverage, $showColors = false)
48     {
49         $output = PHP_EOL . PHP_EOL;
50         $report = $coverage->getReport();
51         unset($coverage);
52
53         $colors = array(
54             'header'  => '',
55             'classes' => '',
56             'methods' => '',
57             'lines'   => '',
58             'reset'   => '',
59             'eol'     => ''
60         );
61
62         if ($showColors) {
63             $colors['classes'] = $this->getCoverageColor(
64                 $report->getNumTestedClassesAndTraits(),
65                 $report->getNumClassesAndTraits()
66             );
67             $colors['methods'] = $this->getCoverageColor(
68                 $report->getNumTestedMethods(),
69                 $report->getNumMethods()
70             );
71             $colors['lines']   = $this->getCoverageColor(
72                 $report->getNumExecutedLines(),
73                 $report->getNumExecutableLines()
74             );
75             $colors['reset']   = $this->colors['reset'];
76             $colors['header']  = $this->colors['header'];
77             $colors['eol']     = $this->colors['eol'];
78         }
79
80         $classes = sprintf(
81             '  Classes: %6s (%d/%d)',
82             PHP_CodeCoverage_Util::percent(
83                 $report->getNumTestedClassesAndTraits(),
84                 $report->getNumClassesAndTraits(),
85                 true
86             ),
87             $report->getNumTestedClassesAndTraits(),
88             $report->getNumClassesAndTraits()
89         );
90
91         $methods = sprintf(
92             '  Methods: %6s (%d/%d)',
93             PHP_CodeCoverage_Util::percent(
94                 $report->getNumTestedMethods(),
95                 $report->getNumMethods(),
96                 true
97             ),
98             $report->getNumTestedMethods(),
99             $report->getNumMethods()
100         );
101
102         $lines = sprintf(
103             '  Lines:   %6s (%d/%d)',
104             PHP_CodeCoverage_Util::percent(
105                 $report->getNumExecutedLines(),
106                 $report->getNumExecutableLines(),
107                 true
108             ),
109             $report->getNumExecutedLines(),
110             $report->getNumExecutableLines()
111         );
112
113         $padding = max(array_map('strlen', array($classes, $methods, $lines)));
114
115         if ($this->showOnlySummary) {
116             $title   = 'Code Coverage Report Summary:';
117             $padding = max($padding, strlen($title));
118
119             $output .= $this->format($colors['header'], $padding, $title);
120         } else {
121             $date  = date('  Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
122             $title = 'Code Coverage Report:';
123
124             $output .= $this->format($colors['header'], $padding, $title);
125             $output .= $this->format($colors['header'], $padding, $date);
126             $output .= $this->format($colors['header'], $padding, '');
127             $output .= $this->format($colors['header'], $padding, ' Summary:');
128         }
129
130         $output .= $this->format($colors['classes'], $padding, $classes);
131         $output .= $this->format($colors['methods'], $padding, $methods);
132         $output .= $this->format($colors['lines'], $padding, $lines);
133
134         if ($this->showOnlySummary) {
135             return $output . PHP_EOL;
136         }
137
138         $classCoverage = array();
139
140         foreach ($report as $item) {
141             if (!$item instanceof PHP_CodeCoverage_Report_Node_File) {
142                 continue;
143             }
144
145             $classes  = $item->getClassesAndTraits();
146
147             foreach ($classes as $className => $class) {
148                 $classStatements        = 0;
149                 $coveredClassStatements = 0;
150                 $coveredMethods         = 0;
151                 $classMethods           = 0;
152
153                 foreach ($class['methods'] as $method) {
154                     if ($method['executableLines'] == 0) {
155                         continue;
156                     }
157
158                     $classMethods++;
159                     $classStatements        += $method['executableLines'];
160                     $coveredClassStatements += $method['executedLines'];
161                     if ($method['coverage'] == 100) {
162                         $coveredMethods++;
163                     }
164                 }
165
166                 if (!empty($class['package']['namespace'])) {
167                     $namespace = '\\' . $class['package']['namespace'] . '::';
168                 } elseif (!empty($class['package']['fullPackage'])) {
169                     $namespace = '@' . $class['package']['fullPackage'] . '::';
170                 } else {
171                     $namespace = '';
172                 }
173
174                 $classCoverage[$namespace . $className] = array(
175                     'namespace'         => $namespace,
176                     'className '        => $className,
177                     'methodsCovered'    => $coveredMethods,
178                     'methodCount'       => $classMethods,
179                     'statementsCovered' => $coveredClassStatements,
180                     'statementCount'    => $classStatements,
181                 );
182             }
183         }
184
185         ksort($classCoverage);
186
187         $methodColor = '';
188         $linesColor  = '';
189         $resetColor  = '';
190
191         foreach ($classCoverage as $fullQualifiedPath => $classInfo) {
192             if ($classInfo['statementsCovered'] != 0 ||
193                 $this->showUncoveredFiles) {
194                 if ($showColors) {
195                     $methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']);
196                     $linesColor  = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']);
197                     $resetColor  = $colors['reset'];
198                 }
199
200                 $output .= PHP_EOL . $fullQualifiedPath . PHP_EOL
201                     . '  ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], 2) . $resetColor . ' '
202                     . '  ' . $linesColor  . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], 3) . $resetColor
203                 ;
204             }
205         }
206
207         return $output . PHP_EOL;
208     }
209
210     protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElements)
211     {
212         $coverage = PHP_CodeCoverage_Util::percent(
213             $numberOfCoveredElements,
214             $totalNumberOfElements
215         );
216
217         if ($coverage >= $this->highLowerBound) {
218             return $this->colors['green'];
219         } elseif ($coverage > $this->lowUpperBound) {
220             return $this->colors['yellow'];
221         }
222
223         return $this->colors['red'];
224     }
225
226     protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $presicion)
227     {
228         $format = '%' . $presicion . 's';
229
230         return PHP_CodeCoverage_Util::percent(
231             $numberOfCoveredElements,
232             $totalNumberOfElements,
233             true,
234             true
235         ) .
236         ' (' . sprintf($format, $numberOfCoveredElements) . '/' .
237         sprintf($format, $totalNumberOfElements) . ')';
238     }
239
240     private function format($color, $padding, $string)
241     {
242         $reset = $color ? $this->colors['reset'] : '';
243
244         return $color . str_pad($string, $padding) . $reset . PHP_EOL;
245     }
246 }