@@ -19,172 +19,172 @@ |
||
19 | 19 | */ |
20 | 20 | class Facade |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $templatePath; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $generator; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var int |
|
34 | - */ |
|
35 | - private $lowUpperBound; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var int |
|
39 | - */ |
|
40 | - private $highLowerBound; |
|
41 | - |
|
42 | - /** |
|
43 | - * Constructor. |
|
44 | - * |
|
45 | - * @param int $lowUpperBound |
|
46 | - * @param int $highLowerBound |
|
47 | - * @param string $generator |
|
48 | - */ |
|
49 | - public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '') |
|
50 | - { |
|
51 | - $this->generator = $generator; |
|
52 | - $this->highLowerBound = $highLowerBound; |
|
53 | - $this->lowUpperBound = $lowUpperBound; |
|
54 | - $this->templatePath = __DIR__ . '/Renderer/Template/'; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param CodeCoverage $coverage |
|
59 | - * @param string $target |
|
60 | - */ |
|
61 | - public function process(CodeCoverage $coverage, $target) |
|
62 | - { |
|
63 | - $target = $this->getDirectory($target); |
|
64 | - $report = $coverage->getReport(); |
|
65 | - unset($coverage); |
|
66 | - |
|
67 | - if (!isset($_SERVER['REQUEST_TIME'])) { |
|
68 | - $_SERVER['REQUEST_TIME'] = time(); |
|
69 | - } |
|
70 | - |
|
71 | - $date = date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']); |
|
72 | - |
|
73 | - $dashboard = new Dashboard( |
|
74 | - $this->templatePath, |
|
75 | - $this->generator, |
|
76 | - $date, |
|
77 | - $this->lowUpperBound, |
|
78 | - $this->highLowerBound |
|
79 | - ); |
|
80 | - |
|
81 | - $directory = new Directory( |
|
82 | - $this->templatePath, |
|
83 | - $this->generator, |
|
84 | - $date, |
|
85 | - $this->lowUpperBound, |
|
86 | - $this->highLowerBound |
|
87 | - ); |
|
88 | - |
|
89 | - $file = new File( |
|
90 | - $this->templatePath, |
|
91 | - $this->generator, |
|
92 | - $date, |
|
93 | - $this->lowUpperBound, |
|
94 | - $this->highLowerBound |
|
95 | - ); |
|
96 | - |
|
97 | - $directory->render($report, $target . 'index.html'); |
|
98 | - $dashboard->render($report, $target . 'dashboard.html'); |
|
99 | - |
|
100 | - foreach ($report as $node) { |
|
101 | - $id = $node->getId(); |
|
102 | - |
|
103 | - if ($node instanceof DirectoryNode) { |
|
104 | - if (!file_exists($target . $id)) { |
|
105 | - mkdir($target . $id, 0777, true); |
|
106 | - } |
|
107 | - |
|
108 | - $directory->render($node, $target . $id . '/index.html'); |
|
109 | - $dashboard->render($node, $target . $id . '/dashboard.html'); |
|
110 | - } else { |
|
111 | - $dir = dirname($target . $id); |
|
112 | - |
|
113 | - if (!file_exists($dir)) { |
|
114 | - mkdir($dir, 0777, true); |
|
115 | - } |
|
116 | - |
|
117 | - $file->render($node, $target . $id . '.html'); |
|
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - $this->copyFiles($target); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string $target |
|
126 | - */ |
|
127 | - private function copyFiles($target) |
|
128 | - { |
|
129 | - $dir = $this->getDirectory($target . '.css'); |
|
130 | - |
|
131 | - file_put_contents( |
|
132 | - $dir . 'bootstrap.min.css', |
|
133 | - str_replace( |
|
134 | - 'url(../fonts/', |
|
135 | - 'url(../.fonts/', |
|
136 | - file_get_contents($this->templatePath . 'css/bootstrap.min.css') |
|
137 | - ) |
|
138 | - |
|
139 | - ); |
|
140 | - |
|
141 | - copy($this->templatePath . 'css/nv.d3.min.css', $dir . 'nv.d3.min.css'); |
|
142 | - copy($this->templatePath . 'css/style.css', $dir . 'style.css'); |
|
143 | - |
|
144 | - $dir = $this->getDirectory($target . '.fonts'); |
|
145 | - copy($this->templatePath . 'fonts/glyphicons-halflings-regular.eot', $dir . 'glyphicons-halflings-regular.eot'); |
|
146 | - copy($this->templatePath . 'fonts/glyphicons-halflings-regular.svg', $dir . 'glyphicons-halflings-regular.svg'); |
|
147 | - copy($this->templatePath . 'fonts/glyphicons-halflings-regular.ttf', $dir . 'glyphicons-halflings-regular.ttf'); |
|
148 | - copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff', $dir . 'glyphicons-halflings-regular.woff'); |
|
149 | - copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff2', $dir . 'glyphicons-halflings-regular.woff2'); |
|
150 | - |
|
151 | - $dir = $this->getDirectory($target . '.js'); |
|
152 | - copy($this->templatePath . 'js/bootstrap.min.js', $dir . 'bootstrap.min.js'); |
|
153 | - copy($this->templatePath . 'js/d3.min.js', $dir . 'd3.min.js'); |
|
154 | - copy($this->templatePath . 'js/holder.min.js', $dir . 'holder.min.js'); |
|
155 | - copy($this->templatePath . 'js/html5shiv.min.js', $dir . 'html5shiv.min.js'); |
|
156 | - copy($this->templatePath . 'js/jquery.min.js', $dir . 'jquery.min.js'); |
|
157 | - copy($this->templatePath . 'js/nv.d3.min.js', $dir . 'nv.d3.min.js'); |
|
158 | - copy($this->templatePath . 'js/respond.min.js', $dir . 'respond.min.js'); |
|
159 | - copy($this->templatePath . 'js/file.js', $dir . 'file.js'); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param string $directory |
|
164 | - * |
|
165 | - * @return string |
|
166 | - * |
|
167 | - * @throws RuntimeException |
|
168 | - */ |
|
169 | - private function getDirectory($directory) |
|
170 | - { |
|
171 | - if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) { |
|
172 | - $directory .= DIRECTORY_SEPARATOR; |
|
173 | - } |
|
174 | - |
|
175 | - if (is_dir($directory)) { |
|
176 | - return $directory; |
|
177 | - } |
|
178 | - |
|
179 | - if (@mkdir($directory, 0777, true)) { |
|
180 | - return $directory; |
|
181 | - } |
|
182 | - |
|
183 | - throw new RuntimeException( |
|
184 | - sprintf( |
|
185 | - 'Directory "%s" does not exist.', |
|
186 | - $directory |
|
187 | - ) |
|
188 | - ); |
|
189 | - } |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $templatePath; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $generator; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var int |
|
34 | + */ |
|
35 | + private $lowUpperBound; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var int |
|
39 | + */ |
|
40 | + private $highLowerBound; |
|
41 | + |
|
42 | + /** |
|
43 | + * Constructor. |
|
44 | + * |
|
45 | + * @param int $lowUpperBound |
|
46 | + * @param int $highLowerBound |
|
47 | + * @param string $generator |
|
48 | + */ |
|
49 | + public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '') |
|
50 | + { |
|
51 | + $this->generator = $generator; |
|
52 | + $this->highLowerBound = $highLowerBound; |
|
53 | + $this->lowUpperBound = $lowUpperBound; |
|
54 | + $this->templatePath = __DIR__ . '/Renderer/Template/'; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param CodeCoverage $coverage |
|
59 | + * @param string $target |
|
60 | + */ |
|
61 | + public function process(CodeCoverage $coverage, $target) |
|
62 | + { |
|
63 | + $target = $this->getDirectory($target); |
|
64 | + $report = $coverage->getReport(); |
|
65 | + unset($coverage); |
|
66 | + |
|
67 | + if (!isset($_SERVER['REQUEST_TIME'])) { |
|
68 | + $_SERVER['REQUEST_TIME'] = time(); |
|
69 | + } |
|
70 | + |
|
71 | + $date = date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']); |
|
72 | + |
|
73 | + $dashboard = new Dashboard( |
|
74 | + $this->templatePath, |
|
75 | + $this->generator, |
|
76 | + $date, |
|
77 | + $this->lowUpperBound, |
|
78 | + $this->highLowerBound |
|
79 | + ); |
|
80 | + |
|
81 | + $directory = new Directory( |
|
82 | + $this->templatePath, |
|
83 | + $this->generator, |
|
84 | + $date, |
|
85 | + $this->lowUpperBound, |
|
86 | + $this->highLowerBound |
|
87 | + ); |
|
88 | + |
|
89 | + $file = new File( |
|
90 | + $this->templatePath, |
|
91 | + $this->generator, |
|
92 | + $date, |
|
93 | + $this->lowUpperBound, |
|
94 | + $this->highLowerBound |
|
95 | + ); |
|
96 | + |
|
97 | + $directory->render($report, $target . 'index.html'); |
|
98 | + $dashboard->render($report, $target . 'dashboard.html'); |
|
99 | + |
|
100 | + foreach ($report as $node) { |
|
101 | + $id = $node->getId(); |
|
102 | + |
|
103 | + if ($node instanceof DirectoryNode) { |
|
104 | + if (!file_exists($target . $id)) { |
|
105 | + mkdir($target . $id, 0777, true); |
|
106 | + } |
|
107 | + |
|
108 | + $directory->render($node, $target . $id . '/index.html'); |
|
109 | + $dashboard->render($node, $target . $id . '/dashboard.html'); |
|
110 | + } else { |
|
111 | + $dir = dirname($target . $id); |
|
112 | + |
|
113 | + if (!file_exists($dir)) { |
|
114 | + mkdir($dir, 0777, true); |
|
115 | + } |
|
116 | + |
|
117 | + $file->render($node, $target . $id . '.html'); |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + $this->copyFiles($target); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string $target |
|
126 | + */ |
|
127 | + private function copyFiles($target) |
|
128 | + { |
|
129 | + $dir = $this->getDirectory($target . '.css'); |
|
130 | + |
|
131 | + file_put_contents( |
|
132 | + $dir . 'bootstrap.min.css', |
|
133 | + str_replace( |
|
134 | + 'url(../fonts/', |
|
135 | + 'url(../.fonts/', |
|
136 | + file_get_contents($this->templatePath . 'css/bootstrap.min.css') |
|
137 | + ) |
|
138 | + |
|
139 | + ); |
|
140 | + |
|
141 | + copy($this->templatePath . 'css/nv.d3.min.css', $dir . 'nv.d3.min.css'); |
|
142 | + copy($this->templatePath . 'css/style.css', $dir . 'style.css'); |
|
143 | + |
|
144 | + $dir = $this->getDirectory($target . '.fonts'); |
|
145 | + copy($this->templatePath . 'fonts/glyphicons-halflings-regular.eot', $dir . 'glyphicons-halflings-regular.eot'); |
|
146 | + copy($this->templatePath . 'fonts/glyphicons-halflings-regular.svg', $dir . 'glyphicons-halflings-regular.svg'); |
|
147 | + copy($this->templatePath . 'fonts/glyphicons-halflings-regular.ttf', $dir . 'glyphicons-halflings-regular.ttf'); |
|
148 | + copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff', $dir . 'glyphicons-halflings-regular.woff'); |
|
149 | + copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff2', $dir . 'glyphicons-halflings-regular.woff2'); |
|
150 | + |
|
151 | + $dir = $this->getDirectory($target . '.js'); |
|
152 | + copy($this->templatePath . 'js/bootstrap.min.js', $dir . 'bootstrap.min.js'); |
|
153 | + copy($this->templatePath . 'js/d3.min.js', $dir . 'd3.min.js'); |
|
154 | + copy($this->templatePath . 'js/holder.min.js', $dir . 'holder.min.js'); |
|
155 | + copy($this->templatePath . 'js/html5shiv.min.js', $dir . 'html5shiv.min.js'); |
|
156 | + copy($this->templatePath . 'js/jquery.min.js', $dir . 'jquery.min.js'); |
|
157 | + copy($this->templatePath . 'js/nv.d3.min.js', $dir . 'nv.d3.min.js'); |
|
158 | + copy($this->templatePath . 'js/respond.min.js', $dir . 'respond.min.js'); |
|
159 | + copy($this->templatePath . 'js/file.js', $dir . 'file.js'); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param string $directory |
|
164 | + * |
|
165 | + * @return string |
|
166 | + * |
|
167 | + * @throws RuntimeException |
|
168 | + */ |
|
169 | + private function getDirectory($directory) |
|
170 | + { |
|
171 | + if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) { |
|
172 | + $directory .= DIRECTORY_SEPARATOR; |
|
173 | + } |
|
174 | + |
|
175 | + if (is_dir($directory)) { |
|
176 | + return $directory; |
|
177 | + } |
|
178 | + |
|
179 | + if (@mkdir($directory, 0777, true)) { |
|
180 | + return $directory; |
|
181 | + } |
|
182 | + |
|
183 | + throw new RuntimeException( |
|
184 | + sprintf( |
|
185 | + 'Directory "%s" does not exist.', |
|
186 | + $directory |
|
187 | + ) |
|
188 | + ); |
|
189 | + } |
|
190 | 190 | } |
@@ -21,276 +21,276 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class Renderer |
23 | 23 | { |
24 | - /** |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $templatePath; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $generator; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $date; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var int |
|
41 | - */ |
|
42 | - protected $lowUpperBound; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var int |
|
46 | - */ |
|
47 | - protected $highLowerBound; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $version; |
|
53 | - |
|
54 | - /** |
|
55 | - * Constructor. |
|
56 | - * |
|
57 | - * @param string $templatePath |
|
58 | - * @param string $generator |
|
59 | - * @param string $date |
|
60 | - * @param int $lowUpperBound |
|
61 | - * @param int $highLowerBound |
|
62 | - */ |
|
63 | - public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound) |
|
64 | - { |
|
65 | - $this->templatePath = $templatePath; |
|
66 | - $this->generator = $generator; |
|
67 | - $this->date = $date; |
|
68 | - $this->lowUpperBound = $lowUpperBound; |
|
69 | - $this->highLowerBound = $highLowerBound; |
|
70 | - $this->version = Version::id(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param \Text_Template $template |
|
75 | - * @param array $data |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - protected function renderItemTemplate(\Text_Template $template, array $data) |
|
80 | - { |
|
81 | - $numSeparator = ' / '; |
|
82 | - |
|
83 | - if (isset($data['numClasses']) && $data['numClasses'] > 0) { |
|
84 | - $classesLevel = $this->getColorLevel($data['testedClassesPercent']); |
|
85 | - |
|
86 | - $classesNumber = $data['numTestedClasses'] . $numSeparator . |
|
87 | - $data['numClasses']; |
|
88 | - |
|
89 | - $classesBar = $this->getCoverageBar( |
|
90 | - $data['testedClassesPercent'] |
|
91 | - ); |
|
92 | - } else { |
|
93 | - $classesLevel = ''; |
|
94 | - $classesNumber = '0' . $numSeparator . '0'; |
|
95 | - $classesBar = ''; |
|
96 | - $data['testedClassesPercentAsString'] = 'n/a'; |
|
97 | - } |
|
98 | - |
|
99 | - if ($data['numMethods'] > 0) { |
|
100 | - $methodsLevel = $this->getColorLevel($data['testedMethodsPercent']); |
|
101 | - |
|
102 | - $methodsNumber = $data['numTestedMethods'] . $numSeparator . |
|
103 | - $data['numMethods']; |
|
104 | - |
|
105 | - $methodsBar = $this->getCoverageBar( |
|
106 | - $data['testedMethodsPercent'] |
|
107 | - ); |
|
108 | - } else { |
|
109 | - $methodsLevel = ''; |
|
110 | - $methodsNumber = '0' . $numSeparator . '0'; |
|
111 | - $methodsBar = ''; |
|
112 | - $data['testedMethodsPercentAsString'] = 'n/a'; |
|
113 | - } |
|
114 | - |
|
115 | - if ($data['numExecutableLines'] > 0) { |
|
116 | - $linesLevel = $this->getColorLevel($data['linesExecutedPercent']); |
|
117 | - |
|
118 | - $linesNumber = $data['numExecutedLines'] . $numSeparator . |
|
119 | - $data['numExecutableLines']; |
|
120 | - |
|
121 | - $linesBar = $this->getCoverageBar( |
|
122 | - $data['linesExecutedPercent'] |
|
123 | - ); |
|
124 | - } else { |
|
125 | - $linesLevel = ''; |
|
126 | - $linesNumber = '0' . $numSeparator . '0'; |
|
127 | - $linesBar = ''; |
|
128 | - $data['linesExecutedPercentAsString'] = 'n/a'; |
|
129 | - } |
|
130 | - |
|
131 | - $template->setVar( |
|
132 | - [ |
|
133 | - 'icon' => isset($data['icon']) ? $data['icon'] : '', |
|
134 | - 'crap' => isset($data['crap']) ? $data['crap'] : '', |
|
135 | - 'name' => $data['name'], |
|
136 | - 'lines_bar' => $linesBar, |
|
137 | - 'lines_executed_percent' => $data['linesExecutedPercentAsString'], |
|
138 | - 'lines_level' => $linesLevel, |
|
139 | - 'lines_number' => $linesNumber, |
|
140 | - 'methods_bar' => $methodsBar, |
|
141 | - 'methods_tested_percent' => $data['testedMethodsPercentAsString'], |
|
142 | - 'methods_level' => $methodsLevel, |
|
143 | - 'methods_number' => $methodsNumber, |
|
144 | - 'classes_bar' => $classesBar, |
|
145 | - 'classes_tested_percent' => isset($data['testedClassesPercentAsString']) ? $data['testedClassesPercentAsString'] : '', |
|
146 | - 'classes_level' => $classesLevel, |
|
147 | - 'classes_number' => $classesNumber |
|
148 | - ] |
|
149 | - ); |
|
150 | - |
|
151 | - return $template->render(); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @param \Text_Template $template |
|
156 | - * @param AbstractNode $node |
|
157 | - */ |
|
158 | - protected function setCommonTemplateVariables(\Text_Template $template, AbstractNode $node) |
|
159 | - { |
|
160 | - $template->setVar( |
|
161 | - [ |
|
162 | - 'id' => $node->getId(), |
|
163 | - 'full_path' => $node->getPath(), |
|
164 | - 'path_to_root' => $this->getPathToRoot($node), |
|
165 | - 'breadcrumbs' => $this->getBreadcrumbs($node), |
|
166 | - 'date' => $this->date, |
|
167 | - 'version' => $this->version, |
|
168 | - 'runtime' => $this->getRuntimeString(), |
|
169 | - 'generator' => $this->generator, |
|
170 | - 'low_upper_bound' => $this->lowUpperBound, |
|
171 | - 'high_lower_bound' => $this->highLowerBound |
|
172 | - ] |
|
173 | - ); |
|
174 | - } |
|
175 | - |
|
176 | - protected function getBreadcrumbs(AbstractNode $node) |
|
177 | - { |
|
178 | - $breadcrumbs = ''; |
|
179 | - $path = $node->getPathAsArray(); |
|
180 | - $pathToRoot = []; |
|
181 | - $max = count($path); |
|
182 | - |
|
183 | - if ($node instanceof FileNode) { |
|
184 | - $max--; |
|
185 | - } |
|
186 | - |
|
187 | - for ($i = 0; $i < $max; $i++) { |
|
188 | - $pathToRoot[] = str_repeat('../', $i); |
|
189 | - } |
|
190 | - |
|
191 | - foreach ($path as $step) { |
|
192 | - if ($step !== $node) { |
|
193 | - $breadcrumbs .= $this->getInactiveBreadcrumb( |
|
194 | - $step, |
|
195 | - array_pop($pathToRoot) |
|
196 | - ); |
|
197 | - } else { |
|
198 | - $breadcrumbs .= $this->getActiveBreadcrumb($step); |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - return $breadcrumbs; |
|
203 | - } |
|
204 | - |
|
205 | - protected function getActiveBreadcrumb(AbstractNode $node) |
|
206 | - { |
|
207 | - $buffer = sprintf( |
|
208 | - ' <li class="active">%s</li>' . "\n", |
|
209 | - $node->getName() |
|
210 | - ); |
|
211 | - |
|
212 | - if ($node instanceof DirectoryNode) { |
|
213 | - $buffer .= ' <li>(<a href="dashboard.html">Dashboard</a>)</li>' . "\n"; |
|
214 | - } |
|
215 | - |
|
216 | - return $buffer; |
|
217 | - } |
|
218 | - |
|
219 | - protected function getInactiveBreadcrumb(AbstractNode $node, $pathToRoot) |
|
220 | - { |
|
221 | - return sprintf( |
|
222 | - ' <li><a href="%sindex.html">%s</a></li>' . "\n", |
|
223 | - $pathToRoot, |
|
224 | - $node->getName() |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - protected function getPathToRoot(AbstractNode $node) |
|
229 | - { |
|
230 | - $id = $node->getId(); |
|
231 | - $depth = substr_count($id, '/'); |
|
232 | - |
|
233 | - if ($id != 'index' && |
|
234 | - $node instanceof DirectoryNode) { |
|
235 | - $depth++; |
|
236 | - } |
|
237 | - |
|
238 | - return str_repeat('../', $depth); |
|
239 | - } |
|
240 | - |
|
241 | - protected function getCoverageBar($percent) |
|
242 | - { |
|
243 | - $level = $this->getColorLevel($percent); |
|
244 | - |
|
245 | - $template = new \Text_Template( |
|
246 | - $this->templatePath . 'coverage_bar.html', |
|
247 | - '{{', |
|
248 | - '}}' |
|
249 | - ); |
|
250 | - |
|
251 | - $template->setVar(['level' => $level, 'percent' => sprintf('%.2F', $percent)]); |
|
252 | - |
|
253 | - return $template->render(); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @param int $percent |
|
258 | - * |
|
259 | - * @return string |
|
260 | - */ |
|
261 | - protected function getColorLevel($percent) |
|
262 | - { |
|
263 | - if ($percent <= $this->lowUpperBound) { |
|
264 | - return 'danger'; |
|
265 | - } elseif ($percent > $this->lowUpperBound && |
|
266 | - $percent < $this->highLowerBound) { |
|
267 | - return 'warning'; |
|
268 | - } else { |
|
269 | - return 'success'; |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * @return string |
|
275 | - */ |
|
276 | - private function getRuntimeString() |
|
277 | - { |
|
278 | - $runtime = new Runtime; |
|
279 | - |
|
280 | - $buffer = sprintf( |
|
281 | - '<a href="%s" target="_top">%s %s</a>', |
|
282 | - $runtime->getVendorUrl(), |
|
283 | - $runtime->getName(), |
|
284 | - $runtime->getVersion() |
|
285 | - ); |
|
286 | - |
|
287 | - if ($runtime->hasXdebug() && !$runtime->hasPHPDBGCodeCoverage()) { |
|
288 | - $buffer .= sprintf( |
|
289 | - ' with <a href="https://xdebug.org/">Xdebug %s</a>', |
|
290 | - phpversion('xdebug') |
|
291 | - ); |
|
292 | - } |
|
293 | - |
|
294 | - return $buffer; |
|
295 | - } |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $templatePath; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $generator; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $date; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var int |
|
41 | + */ |
|
42 | + protected $lowUpperBound; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var int |
|
46 | + */ |
|
47 | + protected $highLowerBound; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $version; |
|
53 | + |
|
54 | + /** |
|
55 | + * Constructor. |
|
56 | + * |
|
57 | + * @param string $templatePath |
|
58 | + * @param string $generator |
|
59 | + * @param string $date |
|
60 | + * @param int $lowUpperBound |
|
61 | + * @param int $highLowerBound |
|
62 | + */ |
|
63 | + public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound) |
|
64 | + { |
|
65 | + $this->templatePath = $templatePath; |
|
66 | + $this->generator = $generator; |
|
67 | + $this->date = $date; |
|
68 | + $this->lowUpperBound = $lowUpperBound; |
|
69 | + $this->highLowerBound = $highLowerBound; |
|
70 | + $this->version = Version::id(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param \Text_Template $template |
|
75 | + * @param array $data |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + protected function renderItemTemplate(\Text_Template $template, array $data) |
|
80 | + { |
|
81 | + $numSeparator = ' / '; |
|
82 | + |
|
83 | + if (isset($data['numClasses']) && $data['numClasses'] > 0) { |
|
84 | + $classesLevel = $this->getColorLevel($data['testedClassesPercent']); |
|
85 | + |
|
86 | + $classesNumber = $data['numTestedClasses'] . $numSeparator . |
|
87 | + $data['numClasses']; |
|
88 | + |
|
89 | + $classesBar = $this->getCoverageBar( |
|
90 | + $data['testedClassesPercent'] |
|
91 | + ); |
|
92 | + } else { |
|
93 | + $classesLevel = ''; |
|
94 | + $classesNumber = '0' . $numSeparator . '0'; |
|
95 | + $classesBar = ''; |
|
96 | + $data['testedClassesPercentAsString'] = 'n/a'; |
|
97 | + } |
|
98 | + |
|
99 | + if ($data['numMethods'] > 0) { |
|
100 | + $methodsLevel = $this->getColorLevel($data['testedMethodsPercent']); |
|
101 | + |
|
102 | + $methodsNumber = $data['numTestedMethods'] . $numSeparator . |
|
103 | + $data['numMethods']; |
|
104 | + |
|
105 | + $methodsBar = $this->getCoverageBar( |
|
106 | + $data['testedMethodsPercent'] |
|
107 | + ); |
|
108 | + } else { |
|
109 | + $methodsLevel = ''; |
|
110 | + $methodsNumber = '0' . $numSeparator . '0'; |
|
111 | + $methodsBar = ''; |
|
112 | + $data['testedMethodsPercentAsString'] = 'n/a'; |
|
113 | + } |
|
114 | + |
|
115 | + if ($data['numExecutableLines'] > 0) { |
|
116 | + $linesLevel = $this->getColorLevel($data['linesExecutedPercent']); |
|
117 | + |
|
118 | + $linesNumber = $data['numExecutedLines'] . $numSeparator . |
|
119 | + $data['numExecutableLines']; |
|
120 | + |
|
121 | + $linesBar = $this->getCoverageBar( |
|
122 | + $data['linesExecutedPercent'] |
|
123 | + ); |
|
124 | + } else { |
|
125 | + $linesLevel = ''; |
|
126 | + $linesNumber = '0' . $numSeparator . '0'; |
|
127 | + $linesBar = ''; |
|
128 | + $data['linesExecutedPercentAsString'] = 'n/a'; |
|
129 | + } |
|
130 | + |
|
131 | + $template->setVar( |
|
132 | + [ |
|
133 | + 'icon' => isset($data['icon']) ? $data['icon'] : '', |
|
134 | + 'crap' => isset($data['crap']) ? $data['crap'] : '', |
|
135 | + 'name' => $data['name'], |
|
136 | + 'lines_bar' => $linesBar, |
|
137 | + 'lines_executed_percent' => $data['linesExecutedPercentAsString'], |
|
138 | + 'lines_level' => $linesLevel, |
|
139 | + 'lines_number' => $linesNumber, |
|
140 | + 'methods_bar' => $methodsBar, |
|
141 | + 'methods_tested_percent' => $data['testedMethodsPercentAsString'], |
|
142 | + 'methods_level' => $methodsLevel, |
|
143 | + 'methods_number' => $methodsNumber, |
|
144 | + 'classes_bar' => $classesBar, |
|
145 | + 'classes_tested_percent' => isset($data['testedClassesPercentAsString']) ? $data['testedClassesPercentAsString'] : '', |
|
146 | + 'classes_level' => $classesLevel, |
|
147 | + 'classes_number' => $classesNumber |
|
148 | + ] |
|
149 | + ); |
|
150 | + |
|
151 | + return $template->render(); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @param \Text_Template $template |
|
156 | + * @param AbstractNode $node |
|
157 | + */ |
|
158 | + protected function setCommonTemplateVariables(\Text_Template $template, AbstractNode $node) |
|
159 | + { |
|
160 | + $template->setVar( |
|
161 | + [ |
|
162 | + 'id' => $node->getId(), |
|
163 | + 'full_path' => $node->getPath(), |
|
164 | + 'path_to_root' => $this->getPathToRoot($node), |
|
165 | + 'breadcrumbs' => $this->getBreadcrumbs($node), |
|
166 | + 'date' => $this->date, |
|
167 | + 'version' => $this->version, |
|
168 | + 'runtime' => $this->getRuntimeString(), |
|
169 | + 'generator' => $this->generator, |
|
170 | + 'low_upper_bound' => $this->lowUpperBound, |
|
171 | + 'high_lower_bound' => $this->highLowerBound |
|
172 | + ] |
|
173 | + ); |
|
174 | + } |
|
175 | + |
|
176 | + protected function getBreadcrumbs(AbstractNode $node) |
|
177 | + { |
|
178 | + $breadcrumbs = ''; |
|
179 | + $path = $node->getPathAsArray(); |
|
180 | + $pathToRoot = []; |
|
181 | + $max = count($path); |
|
182 | + |
|
183 | + if ($node instanceof FileNode) { |
|
184 | + $max--; |
|
185 | + } |
|
186 | + |
|
187 | + for ($i = 0; $i < $max; $i++) { |
|
188 | + $pathToRoot[] = str_repeat('../', $i); |
|
189 | + } |
|
190 | + |
|
191 | + foreach ($path as $step) { |
|
192 | + if ($step !== $node) { |
|
193 | + $breadcrumbs .= $this->getInactiveBreadcrumb( |
|
194 | + $step, |
|
195 | + array_pop($pathToRoot) |
|
196 | + ); |
|
197 | + } else { |
|
198 | + $breadcrumbs .= $this->getActiveBreadcrumb($step); |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + return $breadcrumbs; |
|
203 | + } |
|
204 | + |
|
205 | + protected function getActiveBreadcrumb(AbstractNode $node) |
|
206 | + { |
|
207 | + $buffer = sprintf( |
|
208 | + ' <li class="active">%s</li>' . "\n", |
|
209 | + $node->getName() |
|
210 | + ); |
|
211 | + |
|
212 | + if ($node instanceof DirectoryNode) { |
|
213 | + $buffer .= ' <li>(<a href="dashboard.html">Dashboard</a>)</li>' . "\n"; |
|
214 | + } |
|
215 | + |
|
216 | + return $buffer; |
|
217 | + } |
|
218 | + |
|
219 | + protected function getInactiveBreadcrumb(AbstractNode $node, $pathToRoot) |
|
220 | + { |
|
221 | + return sprintf( |
|
222 | + ' <li><a href="%sindex.html">%s</a></li>' . "\n", |
|
223 | + $pathToRoot, |
|
224 | + $node->getName() |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + protected function getPathToRoot(AbstractNode $node) |
|
229 | + { |
|
230 | + $id = $node->getId(); |
|
231 | + $depth = substr_count($id, '/'); |
|
232 | + |
|
233 | + if ($id != 'index' && |
|
234 | + $node instanceof DirectoryNode) { |
|
235 | + $depth++; |
|
236 | + } |
|
237 | + |
|
238 | + return str_repeat('../', $depth); |
|
239 | + } |
|
240 | + |
|
241 | + protected function getCoverageBar($percent) |
|
242 | + { |
|
243 | + $level = $this->getColorLevel($percent); |
|
244 | + |
|
245 | + $template = new \Text_Template( |
|
246 | + $this->templatePath . 'coverage_bar.html', |
|
247 | + '{{', |
|
248 | + '}}' |
|
249 | + ); |
|
250 | + |
|
251 | + $template->setVar(['level' => $level, 'percent' => sprintf('%.2F', $percent)]); |
|
252 | + |
|
253 | + return $template->render(); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @param int $percent |
|
258 | + * |
|
259 | + * @return string |
|
260 | + */ |
|
261 | + protected function getColorLevel($percent) |
|
262 | + { |
|
263 | + if ($percent <= $this->lowUpperBound) { |
|
264 | + return 'danger'; |
|
265 | + } elseif ($percent > $this->lowUpperBound && |
|
266 | + $percent < $this->highLowerBound) { |
|
267 | + return 'warning'; |
|
268 | + } else { |
|
269 | + return 'success'; |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * @return string |
|
275 | + */ |
|
276 | + private function getRuntimeString() |
|
277 | + { |
|
278 | + $runtime = new Runtime; |
|
279 | + |
|
280 | + $buffer = sprintf( |
|
281 | + '<a href="%s" target="_top">%s %s</a>', |
|
282 | + $runtime->getVendorUrl(), |
|
283 | + $runtime->getName(), |
|
284 | + $runtime->getVersion() |
|
285 | + ); |
|
286 | + |
|
287 | + if ($runtime->hasXdebug() && !$runtime->hasPHPDBGCodeCoverage()) { |
|
288 | + $buffer .= sprintf( |
|
289 | + ' with <a href="https://xdebug.org/">Xdebug %s</a>', |
|
290 | + phpversion('xdebug') |
|
291 | + ); |
|
292 | + } |
|
293 | + |
|
294 | + return $buffer; |
|
295 | + } |
|
296 | 296 | } |
@@ -18,534 +18,534 @@ |
||
18 | 18 | */ |
19 | 19 | class File extends Renderer |
20 | 20 | { |
21 | - /** |
|
22 | - * @var int |
|
23 | - */ |
|
24 | - private $htmlspecialcharsFlags; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param string $templatePath |
|
30 | - * @param string $generator |
|
31 | - * @param string $date |
|
32 | - * @param int $lowUpperBound |
|
33 | - * @param int $highLowerBound |
|
34 | - */ |
|
35 | - public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound) |
|
36 | - { |
|
37 | - parent::__construct( |
|
38 | - $templatePath, |
|
39 | - $generator, |
|
40 | - $date, |
|
41 | - $lowUpperBound, |
|
42 | - $highLowerBound |
|
43 | - ); |
|
44 | - |
|
45 | - $this->htmlspecialcharsFlags = ENT_COMPAT; |
|
46 | - |
|
47 | - $this->htmlspecialcharsFlags = $this->htmlspecialcharsFlags | ENT_HTML401 | ENT_SUBSTITUTE; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @param FileNode $node |
|
52 | - * @param string $file |
|
53 | - */ |
|
54 | - public function render(FileNode $node, $file) |
|
55 | - { |
|
56 | - $template = new \Text_Template($this->templatePath . 'file.html', '{{', '}}'); |
|
57 | - |
|
58 | - $template->setVar( |
|
59 | - [ |
|
60 | - 'items' => $this->renderItems($node), |
|
61 | - 'lines' => $this->renderSource($node) |
|
62 | - ] |
|
63 | - ); |
|
64 | - |
|
65 | - $this->setCommonTemplateVariables($template, $node); |
|
66 | - |
|
67 | - $template->renderTo($file); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param FileNode $node |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - protected function renderItems(FileNode $node) |
|
76 | - { |
|
77 | - $template = new \Text_Template($this->templatePath . 'file_item.html', '{{', '}}'); |
|
78 | - |
|
79 | - $methodItemTemplate = new \Text_Template( |
|
80 | - $this->templatePath . 'method_item.html', |
|
81 | - '{{', |
|
82 | - '}}' |
|
83 | - ); |
|
84 | - |
|
85 | - $items = $this->renderItemTemplate( |
|
86 | - $template, |
|
87 | - [ |
|
88 | - 'name' => 'Total', |
|
89 | - 'numClasses' => $node->getNumClassesAndTraits(), |
|
90 | - 'numTestedClasses' => $node->getNumTestedClassesAndTraits(), |
|
91 | - 'numMethods' => $node->getNumMethods(), |
|
92 | - 'numTestedMethods' => $node->getNumTestedMethods(), |
|
93 | - 'linesExecutedPercent' => $node->getLineExecutedPercent(false), |
|
94 | - 'linesExecutedPercentAsString' => $node->getLineExecutedPercent(), |
|
95 | - 'numExecutedLines' => $node->getNumExecutedLines(), |
|
96 | - 'numExecutableLines' => $node->getNumExecutableLines(), |
|
97 | - 'testedMethodsPercent' => $node->getTestedMethodsPercent(false), |
|
98 | - 'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(), |
|
99 | - 'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false), |
|
100 | - 'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent(), |
|
101 | - 'crap' => '<abbr title="Change Risk Anti-Patterns (CRAP) Index">CRAP</abbr>' |
|
102 | - ] |
|
103 | - ); |
|
104 | - |
|
105 | - $items .= $this->renderFunctionItems( |
|
106 | - $node->getFunctions(), |
|
107 | - $methodItemTemplate |
|
108 | - ); |
|
109 | - |
|
110 | - $items .= $this->renderTraitOrClassItems( |
|
111 | - $node->getTraits(), |
|
112 | - $template, |
|
113 | - $methodItemTemplate |
|
114 | - ); |
|
115 | - |
|
116 | - $items .= $this->renderTraitOrClassItems( |
|
117 | - $node->getClasses(), |
|
118 | - $template, |
|
119 | - $methodItemTemplate |
|
120 | - ); |
|
121 | - |
|
122 | - return $items; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param array $items |
|
127 | - * @param \Text_Template $template |
|
128 | - * @param \Text_Template $methodItemTemplate |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - protected function renderTraitOrClassItems(array $items, \Text_Template $template, \Text_Template $methodItemTemplate) |
|
133 | - { |
|
134 | - if (empty($items)) { |
|
135 | - return ''; |
|
136 | - } |
|
137 | - |
|
138 | - $buffer = ''; |
|
139 | - |
|
140 | - foreach ($items as $name => $item) { |
|
141 | - $numMethods = count($item['methods']); |
|
142 | - $numTestedMethods = 0; |
|
143 | - |
|
144 | - foreach ($item['methods'] as $method) { |
|
145 | - if ($method['executedLines'] == $method['executableLines']) { |
|
146 | - $numTestedMethods++; |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - if ($item['executableLines'] > 0) { |
|
151 | - $numClasses = 1; |
|
152 | - $numTestedClasses = $numTestedMethods == $numMethods ? 1 : 0; |
|
153 | - $linesExecutedPercentAsString = Util::percent( |
|
154 | - $item['executedLines'], |
|
155 | - $item['executableLines'], |
|
156 | - true |
|
157 | - ); |
|
158 | - } else { |
|
159 | - $numClasses = 'n/a'; |
|
160 | - $numTestedClasses = 'n/a'; |
|
161 | - $linesExecutedPercentAsString = 'n/a'; |
|
162 | - } |
|
163 | - |
|
164 | - $buffer .= $this->renderItemTemplate( |
|
165 | - $template, |
|
166 | - [ |
|
167 | - 'name' => $name, |
|
168 | - 'numClasses' => $numClasses, |
|
169 | - 'numTestedClasses' => $numTestedClasses, |
|
170 | - 'numMethods' => $numMethods, |
|
171 | - 'numTestedMethods' => $numTestedMethods, |
|
172 | - 'linesExecutedPercent' => Util::percent( |
|
173 | - $item['executedLines'], |
|
174 | - $item['executableLines'], |
|
175 | - false |
|
176 | - ), |
|
177 | - 'linesExecutedPercentAsString' => $linesExecutedPercentAsString, |
|
178 | - 'numExecutedLines' => $item['executedLines'], |
|
179 | - 'numExecutableLines' => $item['executableLines'], |
|
180 | - 'testedMethodsPercent' => Util::percent( |
|
181 | - $numTestedMethods, |
|
182 | - $numMethods, |
|
183 | - false |
|
184 | - ), |
|
185 | - 'testedMethodsPercentAsString' => Util::percent( |
|
186 | - $numTestedMethods, |
|
187 | - $numMethods, |
|
188 | - true |
|
189 | - ), |
|
190 | - 'testedClassesPercent' => Util::percent( |
|
191 | - $numTestedMethods == $numMethods ? 1 : 0, |
|
192 | - 1, |
|
193 | - false |
|
194 | - ), |
|
195 | - 'testedClassesPercentAsString' => Util::percent( |
|
196 | - $numTestedMethods == $numMethods ? 1 : 0, |
|
197 | - 1, |
|
198 | - true |
|
199 | - ), |
|
200 | - 'crap' => $item['crap'] |
|
201 | - ] |
|
202 | - ); |
|
203 | - |
|
204 | - foreach ($item['methods'] as $method) { |
|
205 | - $buffer .= $this->renderFunctionOrMethodItem( |
|
206 | - $methodItemTemplate, |
|
207 | - $method, |
|
208 | - ' ' |
|
209 | - ); |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - return $buffer; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @param array $functions |
|
218 | - * @param \Text_Template $template |
|
219 | - * |
|
220 | - * @return string |
|
221 | - */ |
|
222 | - protected function renderFunctionItems(array $functions, \Text_Template $template) |
|
223 | - { |
|
224 | - if (empty($functions)) { |
|
225 | - return ''; |
|
226 | - } |
|
227 | - |
|
228 | - $buffer = ''; |
|
229 | - |
|
230 | - foreach ($functions as $function) { |
|
231 | - $buffer .= $this->renderFunctionOrMethodItem( |
|
232 | - $template, |
|
233 | - $function |
|
234 | - ); |
|
235 | - } |
|
236 | - |
|
237 | - return $buffer; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param \Text_Template $template |
|
242 | - * |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - protected function renderFunctionOrMethodItem(\Text_Template $template, array $item, $indent = '') |
|
246 | - { |
|
247 | - $numTestedItems = $item['executedLines'] == $item['executableLines'] ? 1 : 0; |
|
248 | - |
|
249 | - return $this->renderItemTemplate( |
|
250 | - $template, |
|
251 | - [ |
|
252 | - 'name' => sprintf( |
|
253 | - '%s<a href="#%d"><abbr title="%s">%s</abbr></a>', |
|
254 | - $indent, |
|
255 | - $item['startLine'], |
|
256 | - htmlspecialchars($item['signature']), |
|
257 | - isset($item['functionName']) ? $item['functionName'] : $item['methodName'] |
|
258 | - ), |
|
259 | - 'numMethods' => 1, |
|
260 | - 'numTestedMethods' => $numTestedItems, |
|
261 | - 'linesExecutedPercent' => Util::percent( |
|
262 | - $item['executedLines'], |
|
263 | - $item['executableLines'], |
|
264 | - false |
|
265 | - ), |
|
266 | - 'linesExecutedPercentAsString' => Util::percent( |
|
267 | - $item['executedLines'], |
|
268 | - $item['executableLines'], |
|
269 | - true |
|
270 | - ), |
|
271 | - 'numExecutedLines' => $item['executedLines'], |
|
272 | - 'numExecutableLines' => $item['executableLines'], |
|
273 | - 'testedMethodsPercent' => Util::percent( |
|
274 | - $numTestedItems, |
|
275 | - 1, |
|
276 | - false |
|
277 | - ), |
|
278 | - 'testedMethodsPercentAsString' => Util::percent( |
|
279 | - $numTestedItems, |
|
280 | - 1, |
|
281 | - true |
|
282 | - ), |
|
283 | - 'crap' => $item['crap'] |
|
284 | - ] |
|
285 | - ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * @param FileNode $node |
|
290 | - * |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - protected function renderSource(FileNode $node) |
|
294 | - { |
|
295 | - $coverageData = $node->getCoverageData(); |
|
296 | - $testData = $node->getTestData(); |
|
297 | - $codeLines = $this->loadFile($node->getPath()); |
|
298 | - $lines = ''; |
|
299 | - $i = 1; |
|
300 | - |
|
301 | - foreach ($codeLines as $line) { |
|
302 | - $trClass = ''; |
|
303 | - $popoverContent = ''; |
|
304 | - $popoverTitle = ''; |
|
305 | - |
|
306 | - if (array_key_exists($i, $coverageData)) { |
|
307 | - $numTests = ($coverageData[$i] ? count($coverageData[$i]) : 0); |
|
308 | - |
|
309 | - if ($coverageData[$i] === null) { |
|
310 | - $trClass = ' class="warning"'; |
|
311 | - } elseif ($numTests == 0) { |
|
312 | - $trClass = ' class="danger"'; |
|
313 | - } else { |
|
314 | - $lineCss = 'covered-by-large-tests'; |
|
315 | - $popoverContent = '<ul>'; |
|
316 | - |
|
317 | - if ($numTests > 1) { |
|
318 | - $popoverTitle = $numTests . ' tests cover line ' . $i; |
|
319 | - } else { |
|
320 | - $popoverTitle = '1 test covers line ' . $i; |
|
321 | - } |
|
322 | - |
|
323 | - foreach ($coverageData[$i] as $test) { |
|
324 | - if ($lineCss == 'covered-by-large-tests' && $testData[$test]['size'] == 'medium') { |
|
325 | - $lineCss = 'covered-by-medium-tests'; |
|
326 | - } elseif ($testData[$test]['size'] == 'small') { |
|
327 | - $lineCss = 'covered-by-small-tests'; |
|
328 | - } |
|
329 | - |
|
330 | - switch ($testData[$test]['status']) { |
|
331 | - case 0: |
|
332 | - switch ($testData[$test]['size']) { |
|
333 | - case 'small': |
|
334 | - $testCSS = ' class="covered-by-small-tests"'; |
|
335 | - break; |
|
336 | - |
|
337 | - case 'medium': |
|
338 | - $testCSS = ' class="covered-by-medium-tests"'; |
|
339 | - break; |
|
340 | - |
|
341 | - default: |
|
342 | - $testCSS = ' class="covered-by-large-tests"'; |
|
343 | - break; |
|
344 | - } |
|
345 | - break; |
|
346 | - |
|
347 | - case 1: |
|
348 | - case 2: |
|
349 | - $testCSS = ' class="warning"'; |
|
350 | - break; |
|
351 | - |
|
352 | - case 3: |
|
353 | - $testCSS = ' class="danger"'; |
|
354 | - break; |
|
355 | - |
|
356 | - case 4: |
|
357 | - $testCSS = ' class="danger"'; |
|
358 | - break; |
|
359 | - |
|
360 | - default: |
|
361 | - $testCSS = ''; |
|
362 | - } |
|
363 | - |
|
364 | - $popoverContent .= sprintf( |
|
365 | - '<li%s>%s</li>', |
|
366 | - $testCSS, |
|
367 | - htmlspecialchars($test) |
|
368 | - ); |
|
369 | - } |
|
370 | - |
|
371 | - $popoverContent .= '</ul>'; |
|
372 | - $trClass = ' class="' . $lineCss . ' popin"'; |
|
373 | - } |
|
374 | - } |
|
375 | - |
|
376 | - if (!empty($popoverTitle)) { |
|
377 | - $popover = sprintf( |
|
378 | - ' data-title="%s" data-content="%s" data-placement="bottom" data-html="true"', |
|
379 | - $popoverTitle, |
|
380 | - htmlspecialchars($popoverContent) |
|
381 | - ); |
|
382 | - } else { |
|
383 | - $popover = ''; |
|
384 | - } |
|
385 | - |
|
386 | - $lines .= sprintf( |
|
387 | - ' <tr%s%s><td><div align="right"><a name="%d"></a><a href="#%d">%d</a></div></td><td class="codeLine">%s</td></tr>' . "\n", |
|
388 | - $trClass, |
|
389 | - $popover, |
|
390 | - $i, |
|
391 | - $i, |
|
392 | - $i, |
|
393 | - $line |
|
394 | - ); |
|
395 | - |
|
396 | - $i++; |
|
397 | - } |
|
398 | - |
|
399 | - return $lines; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * @param string $file |
|
404 | - * |
|
405 | - * @return array |
|
406 | - */ |
|
407 | - protected function loadFile($file) |
|
408 | - { |
|
409 | - $buffer = file_get_contents($file); |
|
410 | - $tokens = token_get_all($buffer); |
|
411 | - $result = ['']; |
|
412 | - $i = 0; |
|
413 | - $stringFlag = false; |
|
414 | - $fileEndsWithNewLine = substr($buffer, -1) == "\n"; |
|
415 | - |
|
416 | - unset($buffer); |
|
417 | - |
|
418 | - foreach ($tokens as $j => $token) { |
|
419 | - if (is_string($token)) { |
|
420 | - if ($token === '"' && $tokens[$j - 1] !== '\\') { |
|
421 | - $result[$i] .= sprintf( |
|
422 | - '<span class="string">%s</span>', |
|
423 | - htmlspecialchars($token) |
|
424 | - ); |
|
425 | - |
|
426 | - $stringFlag = !$stringFlag; |
|
427 | - } else { |
|
428 | - $result[$i] .= sprintf( |
|
429 | - '<span class="keyword">%s</span>', |
|
430 | - htmlspecialchars($token) |
|
431 | - ); |
|
432 | - } |
|
433 | - |
|
434 | - continue; |
|
435 | - } |
|
436 | - |
|
437 | - list($token, $value) = $token; |
|
438 | - |
|
439 | - $value = str_replace( |
|
440 | - ["\t", ' '], |
|
441 | - [' ', ' '], |
|
442 | - htmlspecialchars($value, $this->htmlspecialcharsFlags) |
|
443 | - ); |
|
444 | - |
|
445 | - if ($value === "\n") { |
|
446 | - $result[++$i] = ''; |
|
447 | - } else { |
|
448 | - $lines = explode("\n", $value); |
|
449 | - |
|
450 | - foreach ($lines as $jj => $line) { |
|
451 | - $line = trim($line); |
|
452 | - |
|
453 | - if ($line !== '') { |
|
454 | - if ($stringFlag) { |
|
455 | - $colour = 'string'; |
|
456 | - } else { |
|
457 | - switch ($token) { |
|
458 | - case T_INLINE_HTML: |
|
459 | - $colour = 'html'; |
|
460 | - break; |
|
461 | - |
|
462 | - case T_COMMENT: |
|
463 | - case T_DOC_COMMENT: |
|
464 | - $colour = 'comment'; |
|
465 | - break; |
|
466 | - |
|
467 | - case T_ABSTRACT: |
|
468 | - case T_ARRAY: |
|
469 | - case T_AS: |
|
470 | - case T_BREAK: |
|
471 | - case T_CALLABLE: |
|
472 | - case T_CASE: |
|
473 | - case T_CATCH: |
|
474 | - case T_CLASS: |
|
475 | - case T_CLONE: |
|
476 | - case T_CONTINUE: |
|
477 | - case T_DEFAULT: |
|
478 | - case T_ECHO: |
|
479 | - case T_ELSE: |
|
480 | - case T_ELSEIF: |
|
481 | - case T_EMPTY: |
|
482 | - case T_ENDDECLARE: |
|
483 | - case T_ENDFOR: |
|
484 | - case T_ENDFOREACH: |
|
485 | - case T_ENDIF: |
|
486 | - case T_ENDSWITCH: |
|
487 | - case T_ENDWHILE: |
|
488 | - case T_EXIT: |
|
489 | - case T_EXTENDS: |
|
490 | - case T_FINAL: |
|
491 | - case T_FINALLY: |
|
492 | - case T_FOREACH: |
|
493 | - case T_FUNCTION: |
|
494 | - case T_GLOBAL: |
|
495 | - case T_IF: |
|
496 | - case T_IMPLEMENTS: |
|
497 | - case T_INCLUDE: |
|
498 | - case T_INCLUDE_ONCE: |
|
499 | - case T_INSTANCEOF: |
|
500 | - case T_INSTEADOF: |
|
501 | - case T_INTERFACE: |
|
502 | - case T_ISSET: |
|
503 | - case T_LOGICAL_AND: |
|
504 | - case T_LOGICAL_OR: |
|
505 | - case T_LOGICAL_XOR: |
|
506 | - case T_NAMESPACE: |
|
507 | - case T_NEW: |
|
508 | - case T_PRIVATE: |
|
509 | - case T_PROTECTED: |
|
510 | - case T_PUBLIC: |
|
511 | - case T_REQUIRE: |
|
512 | - case T_REQUIRE_ONCE: |
|
513 | - case T_RETURN: |
|
514 | - case T_STATIC: |
|
515 | - case T_THROW: |
|
516 | - case T_TRAIT: |
|
517 | - case T_TRY: |
|
518 | - case T_UNSET: |
|
519 | - case T_USE: |
|
520 | - case T_VAR: |
|
521 | - case T_WHILE: |
|
522 | - case T_YIELD: |
|
523 | - $colour = 'keyword'; |
|
524 | - break; |
|
525 | - |
|
526 | - default: |
|
527 | - $colour = 'default'; |
|
528 | - } |
|
529 | - } |
|
530 | - |
|
531 | - $result[$i] .= sprintf( |
|
532 | - '<span class="%s">%s</span>', |
|
533 | - $colour, |
|
534 | - $line |
|
535 | - ); |
|
536 | - } |
|
537 | - |
|
538 | - if (isset($lines[$jj + 1])) { |
|
539 | - $result[++$i] = ''; |
|
540 | - } |
|
541 | - } |
|
542 | - } |
|
543 | - } |
|
544 | - |
|
545 | - if ($fileEndsWithNewLine) { |
|
546 | - unset($result[count($result) - 1]); |
|
547 | - } |
|
548 | - |
|
549 | - return $result; |
|
550 | - } |
|
21 | + /** |
|
22 | + * @var int |
|
23 | + */ |
|
24 | + private $htmlspecialcharsFlags; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param string $templatePath |
|
30 | + * @param string $generator |
|
31 | + * @param string $date |
|
32 | + * @param int $lowUpperBound |
|
33 | + * @param int $highLowerBound |
|
34 | + */ |
|
35 | + public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound) |
|
36 | + { |
|
37 | + parent::__construct( |
|
38 | + $templatePath, |
|
39 | + $generator, |
|
40 | + $date, |
|
41 | + $lowUpperBound, |
|
42 | + $highLowerBound |
|
43 | + ); |
|
44 | + |
|
45 | + $this->htmlspecialcharsFlags = ENT_COMPAT; |
|
46 | + |
|
47 | + $this->htmlspecialcharsFlags = $this->htmlspecialcharsFlags | ENT_HTML401 | ENT_SUBSTITUTE; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @param FileNode $node |
|
52 | + * @param string $file |
|
53 | + */ |
|
54 | + public function render(FileNode $node, $file) |
|
55 | + { |
|
56 | + $template = new \Text_Template($this->templatePath . 'file.html', '{{', '}}'); |
|
57 | + |
|
58 | + $template->setVar( |
|
59 | + [ |
|
60 | + 'items' => $this->renderItems($node), |
|
61 | + 'lines' => $this->renderSource($node) |
|
62 | + ] |
|
63 | + ); |
|
64 | + |
|
65 | + $this->setCommonTemplateVariables($template, $node); |
|
66 | + |
|
67 | + $template->renderTo($file); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param FileNode $node |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + protected function renderItems(FileNode $node) |
|
76 | + { |
|
77 | + $template = new \Text_Template($this->templatePath . 'file_item.html', '{{', '}}'); |
|
78 | + |
|
79 | + $methodItemTemplate = new \Text_Template( |
|
80 | + $this->templatePath . 'method_item.html', |
|
81 | + '{{', |
|
82 | + '}}' |
|
83 | + ); |
|
84 | + |
|
85 | + $items = $this->renderItemTemplate( |
|
86 | + $template, |
|
87 | + [ |
|
88 | + 'name' => 'Total', |
|
89 | + 'numClasses' => $node->getNumClassesAndTraits(), |
|
90 | + 'numTestedClasses' => $node->getNumTestedClassesAndTraits(), |
|
91 | + 'numMethods' => $node->getNumMethods(), |
|
92 | + 'numTestedMethods' => $node->getNumTestedMethods(), |
|
93 | + 'linesExecutedPercent' => $node->getLineExecutedPercent(false), |
|
94 | + 'linesExecutedPercentAsString' => $node->getLineExecutedPercent(), |
|
95 | + 'numExecutedLines' => $node->getNumExecutedLines(), |
|
96 | + 'numExecutableLines' => $node->getNumExecutableLines(), |
|
97 | + 'testedMethodsPercent' => $node->getTestedMethodsPercent(false), |
|
98 | + 'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(), |
|
99 | + 'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false), |
|
100 | + 'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent(), |
|
101 | + 'crap' => '<abbr title="Change Risk Anti-Patterns (CRAP) Index">CRAP</abbr>' |
|
102 | + ] |
|
103 | + ); |
|
104 | + |
|
105 | + $items .= $this->renderFunctionItems( |
|
106 | + $node->getFunctions(), |
|
107 | + $methodItemTemplate |
|
108 | + ); |
|
109 | + |
|
110 | + $items .= $this->renderTraitOrClassItems( |
|
111 | + $node->getTraits(), |
|
112 | + $template, |
|
113 | + $methodItemTemplate |
|
114 | + ); |
|
115 | + |
|
116 | + $items .= $this->renderTraitOrClassItems( |
|
117 | + $node->getClasses(), |
|
118 | + $template, |
|
119 | + $methodItemTemplate |
|
120 | + ); |
|
121 | + |
|
122 | + return $items; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param array $items |
|
127 | + * @param \Text_Template $template |
|
128 | + * @param \Text_Template $methodItemTemplate |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + protected function renderTraitOrClassItems(array $items, \Text_Template $template, \Text_Template $methodItemTemplate) |
|
133 | + { |
|
134 | + if (empty($items)) { |
|
135 | + return ''; |
|
136 | + } |
|
137 | + |
|
138 | + $buffer = ''; |
|
139 | + |
|
140 | + foreach ($items as $name => $item) { |
|
141 | + $numMethods = count($item['methods']); |
|
142 | + $numTestedMethods = 0; |
|
143 | + |
|
144 | + foreach ($item['methods'] as $method) { |
|
145 | + if ($method['executedLines'] == $method['executableLines']) { |
|
146 | + $numTestedMethods++; |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + if ($item['executableLines'] > 0) { |
|
151 | + $numClasses = 1; |
|
152 | + $numTestedClasses = $numTestedMethods == $numMethods ? 1 : 0; |
|
153 | + $linesExecutedPercentAsString = Util::percent( |
|
154 | + $item['executedLines'], |
|
155 | + $item['executableLines'], |
|
156 | + true |
|
157 | + ); |
|
158 | + } else { |
|
159 | + $numClasses = 'n/a'; |
|
160 | + $numTestedClasses = 'n/a'; |
|
161 | + $linesExecutedPercentAsString = 'n/a'; |
|
162 | + } |
|
163 | + |
|
164 | + $buffer .= $this->renderItemTemplate( |
|
165 | + $template, |
|
166 | + [ |
|
167 | + 'name' => $name, |
|
168 | + 'numClasses' => $numClasses, |
|
169 | + 'numTestedClasses' => $numTestedClasses, |
|
170 | + 'numMethods' => $numMethods, |
|
171 | + 'numTestedMethods' => $numTestedMethods, |
|
172 | + 'linesExecutedPercent' => Util::percent( |
|
173 | + $item['executedLines'], |
|
174 | + $item['executableLines'], |
|
175 | + false |
|
176 | + ), |
|
177 | + 'linesExecutedPercentAsString' => $linesExecutedPercentAsString, |
|
178 | + 'numExecutedLines' => $item['executedLines'], |
|
179 | + 'numExecutableLines' => $item['executableLines'], |
|
180 | + 'testedMethodsPercent' => Util::percent( |
|
181 | + $numTestedMethods, |
|
182 | + $numMethods, |
|
183 | + false |
|
184 | + ), |
|
185 | + 'testedMethodsPercentAsString' => Util::percent( |
|
186 | + $numTestedMethods, |
|
187 | + $numMethods, |
|
188 | + true |
|
189 | + ), |
|
190 | + 'testedClassesPercent' => Util::percent( |
|
191 | + $numTestedMethods == $numMethods ? 1 : 0, |
|
192 | + 1, |
|
193 | + false |
|
194 | + ), |
|
195 | + 'testedClassesPercentAsString' => Util::percent( |
|
196 | + $numTestedMethods == $numMethods ? 1 : 0, |
|
197 | + 1, |
|
198 | + true |
|
199 | + ), |
|
200 | + 'crap' => $item['crap'] |
|
201 | + ] |
|
202 | + ); |
|
203 | + |
|
204 | + foreach ($item['methods'] as $method) { |
|
205 | + $buffer .= $this->renderFunctionOrMethodItem( |
|
206 | + $methodItemTemplate, |
|
207 | + $method, |
|
208 | + ' ' |
|
209 | + ); |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + return $buffer; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @param array $functions |
|
218 | + * @param \Text_Template $template |
|
219 | + * |
|
220 | + * @return string |
|
221 | + */ |
|
222 | + protected function renderFunctionItems(array $functions, \Text_Template $template) |
|
223 | + { |
|
224 | + if (empty($functions)) { |
|
225 | + return ''; |
|
226 | + } |
|
227 | + |
|
228 | + $buffer = ''; |
|
229 | + |
|
230 | + foreach ($functions as $function) { |
|
231 | + $buffer .= $this->renderFunctionOrMethodItem( |
|
232 | + $template, |
|
233 | + $function |
|
234 | + ); |
|
235 | + } |
|
236 | + |
|
237 | + return $buffer; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param \Text_Template $template |
|
242 | + * |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + protected function renderFunctionOrMethodItem(\Text_Template $template, array $item, $indent = '') |
|
246 | + { |
|
247 | + $numTestedItems = $item['executedLines'] == $item['executableLines'] ? 1 : 0; |
|
248 | + |
|
249 | + return $this->renderItemTemplate( |
|
250 | + $template, |
|
251 | + [ |
|
252 | + 'name' => sprintf( |
|
253 | + '%s<a href="#%d"><abbr title="%s">%s</abbr></a>', |
|
254 | + $indent, |
|
255 | + $item['startLine'], |
|
256 | + htmlspecialchars($item['signature']), |
|
257 | + isset($item['functionName']) ? $item['functionName'] : $item['methodName'] |
|
258 | + ), |
|
259 | + 'numMethods' => 1, |
|
260 | + 'numTestedMethods' => $numTestedItems, |
|
261 | + 'linesExecutedPercent' => Util::percent( |
|
262 | + $item['executedLines'], |
|
263 | + $item['executableLines'], |
|
264 | + false |
|
265 | + ), |
|
266 | + 'linesExecutedPercentAsString' => Util::percent( |
|
267 | + $item['executedLines'], |
|
268 | + $item['executableLines'], |
|
269 | + true |
|
270 | + ), |
|
271 | + 'numExecutedLines' => $item['executedLines'], |
|
272 | + 'numExecutableLines' => $item['executableLines'], |
|
273 | + 'testedMethodsPercent' => Util::percent( |
|
274 | + $numTestedItems, |
|
275 | + 1, |
|
276 | + false |
|
277 | + ), |
|
278 | + 'testedMethodsPercentAsString' => Util::percent( |
|
279 | + $numTestedItems, |
|
280 | + 1, |
|
281 | + true |
|
282 | + ), |
|
283 | + 'crap' => $item['crap'] |
|
284 | + ] |
|
285 | + ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * @param FileNode $node |
|
290 | + * |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + protected function renderSource(FileNode $node) |
|
294 | + { |
|
295 | + $coverageData = $node->getCoverageData(); |
|
296 | + $testData = $node->getTestData(); |
|
297 | + $codeLines = $this->loadFile($node->getPath()); |
|
298 | + $lines = ''; |
|
299 | + $i = 1; |
|
300 | + |
|
301 | + foreach ($codeLines as $line) { |
|
302 | + $trClass = ''; |
|
303 | + $popoverContent = ''; |
|
304 | + $popoverTitle = ''; |
|
305 | + |
|
306 | + if (array_key_exists($i, $coverageData)) { |
|
307 | + $numTests = ($coverageData[$i] ? count($coverageData[$i]) : 0); |
|
308 | + |
|
309 | + if ($coverageData[$i] === null) { |
|
310 | + $trClass = ' class="warning"'; |
|
311 | + } elseif ($numTests == 0) { |
|
312 | + $trClass = ' class="danger"'; |
|
313 | + } else { |
|
314 | + $lineCss = 'covered-by-large-tests'; |
|
315 | + $popoverContent = '<ul>'; |
|
316 | + |
|
317 | + if ($numTests > 1) { |
|
318 | + $popoverTitle = $numTests . ' tests cover line ' . $i; |
|
319 | + } else { |
|
320 | + $popoverTitle = '1 test covers line ' . $i; |
|
321 | + } |
|
322 | + |
|
323 | + foreach ($coverageData[$i] as $test) { |
|
324 | + if ($lineCss == 'covered-by-large-tests' && $testData[$test]['size'] == 'medium') { |
|
325 | + $lineCss = 'covered-by-medium-tests'; |
|
326 | + } elseif ($testData[$test]['size'] == 'small') { |
|
327 | + $lineCss = 'covered-by-small-tests'; |
|
328 | + } |
|
329 | + |
|
330 | + switch ($testData[$test]['status']) { |
|
331 | + case 0: |
|
332 | + switch ($testData[$test]['size']) { |
|
333 | + case 'small': |
|
334 | + $testCSS = ' class="covered-by-small-tests"'; |
|
335 | + break; |
|
336 | + |
|
337 | + case 'medium': |
|
338 | + $testCSS = ' class="covered-by-medium-tests"'; |
|
339 | + break; |
|
340 | + |
|
341 | + default: |
|
342 | + $testCSS = ' class="covered-by-large-tests"'; |
|
343 | + break; |
|
344 | + } |
|
345 | + break; |
|
346 | + |
|
347 | + case 1: |
|
348 | + case 2: |
|
349 | + $testCSS = ' class="warning"'; |
|
350 | + break; |
|
351 | + |
|
352 | + case 3: |
|
353 | + $testCSS = ' class="danger"'; |
|
354 | + break; |
|
355 | + |
|
356 | + case 4: |
|
357 | + $testCSS = ' class="danger"'; |
|
358 | + break; |
|
359 | + |
|
360 | + default: |
|
361 | + $testCSS = ''; |
|
362 | + } |
|
363 | + |
|
364 | + $popoverContent .= sprintf( |
|
365 | + '<li%s>%s</li>', |
|
366 | + $testCSS, |
|
367 | + htmlspecialchars($test) |
|
368 | + ); |
|
369 | + } |
|
370 | + |
|
371 | + $popoverContent .= '</ul>'; |
|
372 | + $trClass = ' class="' . $lineCss . ' popin"'; |
|
373 | + } |
|
374 | + } |
|
375 | + |
|
376 | + if (!empty($popoverTitle)) { |
|
377 | + $popover = sprintf( |
|
378 | + ' data-title="%s" data-content="%s" data-placement="bottom" data-html="true"', |
|
379 | + $popoverTitle, |
|
380 | + htmlspecialchars($popoverContent) |
|
381 | + ); |
|
382 | + } else { |
|
383 | + $popover = ''; |
|
384 | + } |
|
385 | + |
|
386 | + $lines .= sprintf( |
|
387 | + ' <tr%s%s><td><div align="right"><a name="%d"></a><a href="#%d">%d</a></div></td><td class="codeLine">%s</td></tr>' . "\n", |
|
388 | + $trClass, |
|
389 | + $popover, |
|
390 | + $i, |
|
391 | + $i, |
|
392 | + $i, |
|
393 | + $line |
|
394 | + ); |
|
395 | + |
|
396 | + $i++; |
|
397 | + } |
|
398 | + |
|
399 | + return $lines; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * @param string $file |
|
404 | + * |
|
405 | + * @return array |
|
406 | + */ |
|
407 | + protected function loadFile($file) |
|
408 | + { |
|
409 | + $buffer = file_get_contents($file); |
|
410 | + $tokens = token_get_all($buffer); |
|
411 | + $result = ['']; |
|
412 | + $i = 0; |
|
413 | + $stringFlag = false; |
|
414 | + $fileEndsWithNewLine = substr($buffer, -1) == "\n"; |
|
415 | + |
|
416 | + unset($buffer); |
|
417 | + |
|
418 | + foreach ($tokens as $j => $token) { |
|
419 | + if (is_string($token)) { |
|
420 | + if ($token === '"' && $tokens[$j - 1] !== '\\') { |
|
421 | + $result[$i] .= sprintf( |
|
422 | + '<span class="string">%s</span>', |
|
423 | + htmlspecialchars($token) |
|
424 | + ); |
|
425 | + |
|
426 | + $stringFlag = !$stringFlag; |
|
427 | + } else { |
|
428 | + $result[$i] .= sprintf( |
|
429 | + '<span class="keyword">%s</span>', |
|
430 | + htmlspecialchars($token) |
|
431 | + ); |
|
432 | + } |
|
433 | + |
|
434 | + continue; |
|
435 | + } |
|
436 | + |
|
437 | + list($token, $value) = $token; |
|
438 | + |
|
439 | + $value = str_replace( |
|
440 | + ["\t", ' '], |
|
441 | + [' ', ' '], |
|
442 | + htmlspecialchars($value, $this->htmlspecialcharsFlags) |
|
443 | + ); |
|
444 | + |
|
445 | + if ($value === "\n") { |
|
446 | + $result[++$i] = ''; |
|
447 | + } else { |
|
448 | + $lines = explode("\n", $value); |
|
449 | + |
|
450 | + foreach ($lines as $jj => $line) { |
|
451 | + $line = trim($line); |
|
452 | + |
|
453 | + if ($line !== '') { |
|
454 | + if ($stringFlag) { |
|
455 | + $colour = 'string'; |
|
456 | + } else { |
|
457 | + switch ($token) { |
|
458 | + case T_INLINE_HTML: |
|
459 | + $colour = 'html'; |
|
460 | + break; |
|
461 | + |
|
462 | + case T_COMMENT: |
|
463 | + case T_DOC_COMMENT: |
|
464 | + $colour = 'comment'; |
|
465 | + break; |
|
466 | + |
|
467 | + case T_ABSTRACT: |
|
468 | + case T_ARRAY: |
|
469 | + case T_AS: |
|
470 | + case T_BREAK: |
|
471 | + case T_CALLABLE: |
|
472 | + case T_CASE: |
|
473 | + case T_CATCH: |
|
474 | + case T_CLASS: |
|
475 | + case T_CLONE: |
|
476 | + case T_CONTINUE: |
|
477 | + case T_DEFAULT: |
|
478 | + case T_ECHO: |
|
479 | + case T_ELSE: |
|
480 | + case T_ELSEIF: |
|
481 | + case T_EMPTY: |
|
482 | + case T_ENDDECLARE: |
|
483 | + case T_ENDFOR: |
|
484 | + case T_ENDFOREACH: |
|
485 | + case T_ENDIF: |
|
486 | + case T_ENDSWITCH: |
|
487 | + case T_ENDWHILE: |
|
488 | + case T_EXIT: |
|
489 | + case T_EXTENDS: |
|
490 | + case T_FINAL: |
|
491 | + case T_FINALLY: |
|
492 | + case T_FOREACH: |
|
493 | + case T_FUNCTION: |
|
494 | + case T_GLOBAL: |
|
495 | + case T_IF: |
|
496 | + case T_IMPLEMENTS: |
|
497 | + case T_INCLUDE: |
|
498 | + case T_INCLUDE_ONCE: |
|
499 | + case T_INSTANCEOF: |
|
500 | + case T_INSTEADOF: |
|
501 | + case T_INTERFACE: |
|
502 | + case T_ISSET: |
|
503 | + case T_LOGICAL_AND: |
|
504 | + case T_LOGICAL_OR: |
|
505 | + case T_LOGICAL_XOR: |
|
506 | + case T_NAMESPACE: |
|
507 | + case T_NEW: |
|
508 | + case T_PRIVATE: |
|
509 | + case T_PROTECTED: |
|
510 | + case T_PUBLIC: |
|
511 | + case T_REQUIRE: |
|
512 | + case T_REQUIRE_ONCE: |
|
513 | + case T_RETURN: |
|
514 | + case T_STATIC: |
|
515 | + case T_THROW: |
|
516 | + case T_TRAIT: |
|
517 | + case T_TRY: |
|
518 | + case T_UNSET: |
|
519 | + case T_USE: |
|
520 | + case T_VAR: |
|
521 | + case T_WHILE: |
|
522 | + case T_YIELD: |
|
523 | + $colour = 'keyword'; |
|
524 | + break; |
|
525 | + |
|
526 | + default: |
|
527 | + $colour = 'default'; |
|
528 | + } |
|
529 | + } |
|
530 | + |
|
531 | + $result[$i] .= sprintf( |
|
532 | + '<span class="%s">%s</span>', |
|
533 | + $colour, |
|
534 | + $line |
|
535 | + ); |
|
536 | + } |
|
537 | + |
|
538 | + if (isset($lines[$jj + 1])) { |
|
539 | + $result[++$i] = ''; |
|
540 | + } |
|
541 | + } |
|
542 | + } |
|
543 | + } |
|
544 | + |
|
545 | + if ($fileEndsWithNewLine) { |
|
546 | + unset($result[count($result) - 1]); |
|
547 | + } |
|
548 | + |
|
549 | + return $result; |
|
550 | + } |
|
551 | 551 | } |
@@ -18,285 +18,285 @@ |
||
18 | 18 | */ |
19 | 19 | class Dashboard extends Renderer |
20 | 20 | { |
21 | - /** |
|
22 | - * @param DirectoryNode $node |
|
23 | - * @param string $file |
|
24 | - */ |
|
25 | - public function render(DirectoryNode $node, $file) |
|
26 | - { |
|
27 | - $classes = $node->getClassesAndTraits(); |
|
28 | - $template = new \Text_Template( |
|
29 | - $this->templatePath . 'dashboard.html', |
|
30 | - '{{', |
|
31 | - '}}' |
|
32 | - ); |
|
33 | - |
|
34 | - $this->setCommonTemplateVariables($template, $node); |
|
35 | - |
|
36 | - $baseLink = $node->getId() . '/'; |
|
37 | - $complexity = $this->complexity($classes, $baseLink); |
|
38 | - $coverageDistribution = $this->coverageDistribution($classes); |
|
39 | - $insufficientCoverage = $this->insufficientCoverage($classes, $baseLink); |
|
40 | - $projectRisks = $this->projectRisks($classes, $baseLink); |
|
41 | - |
|
42 | - $template->setVar( |
|
43 | - [ |
|
44 | - 'insufficient_coverage_classes' => $insufficientCoverage['class'], |
|
45 | - 'insufficient_coverage_methods' => $insufficientCoverage['method'], |
|
46 | - 'project_risks_classes' => $projectRisks['class'], |
|
47 | - 'project_risks_methods' => $projectRisks['method'], |
|
48 | - 'complexity_class' => $complexity['class'], |
|
49 | - 'complexity_method' => $complexity['method'], |
|
50 | - 'class_coverage_distribution' => $coverageDistribution['class'], |
|
51 | - 'method_coverage_distribution' => $coverageDistribution['method'] |
|
52 | - ] |
|
53 | - ); |
|
54 | - |
|
55 | - $template->renderTo($file); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns the data for the Class/Method Complexity charts. |
|
60 | - * |
|
61 | - * @param array $classes |
|
62 | - * @param string $baseLink |
|
63 | - * |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - protected function complexity(array $classes, $baseLink) |
|
67 | - { |
|
68 | - $result = ['class' => [], 'method' => []]; |
|
69 | - |
|
70 | - foreach ($classes as $className => $class) { |
|
71 | - foreach ($class['methods'] as $methodName => $method) { |
|
72 | - if ($className != '*') { |
|
73 | - $methodName = $className . '::' . $methodName; |
|
74 | - } |
|
75 | - |
|
76 | - $result['method'][] = [ |
|
77 | - $method['coverage'], |
|
78 | - $method['ccn'], |
|
79 | - sprintf( |
|
80 | - '<a href="%s">%s</a>', |
|
81 | - str_replace($baseLink, '', $method['link']), |
|
82 | - $methodName |
|
83 | - ) |
|
84 | - ]; |
|
85 | - } |
|
86 | - |
|
87 | - $result['class'][] = [ |
|
88 | - $class['coverage'], |
|
89 | - $class['ccn'], |
|
90 | - sprintf( |
|
91 | - '<a href="%s">%s</a>', |
|
92 | - str_replace($baseLink, '', $class['link']), |
|
93 | - $className |
|
94 | - ) |
|
95 | - ]; |
|
96 | - } |
|
97 | - |
|
98 | - return [ |
|
99 | - 'class' => json_encode($result['class']), |
|
100 | - 'method' => json_encode($result['method']) |
|
101 | - ]; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Returns the data for the Class / Method Coverage Distribution chart. |
|
106 | - * |
|
107 | - * @param array $classes |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - protected function coverageDistribution(array $classes) |
|
112 | - { |
|
113 | - $result = [ |
|
114 | - 'class' => [ |
|
115 | - '0%' => 0, |
|
116 | - '0-10%' => 0, |
|
117 | - '10-20%' => 0, |
|
118 | - '20-30%' => 0, |
|
119 | - '30-40%' => 0, |
|
120 | - '40-50%' => 0, |
|
121 | - '50-60%' => 0, |
|
122 | - '60-70%' => 0, |
|
123 | - '70-80%' => 0, |
|
124 | - '80-90%' => 0, |
|
125 | - '90-100%' => 0, |
|
126 | - '100%' => 0 |
|
127 | - ], |
|
128 | - 'method' => [ |
|
129 | - '0%' => 0, |
|
130 | - '0-10%' => 0, |
|
131 | - '10-20%' => 0, |
|
132 | - '20-30%' => 0, |
|
133 | - '30-40%' => 0, |
|
134 | - '40-50%' => 0, |
|
135 | - '50-60%' => 0, |
|
136 | - '60-70%' => 0, |
|
137 | - '70-80%' => 0, |
|
138 | - '80-90%' => 0, |
|
139 | - '90-100%' => 0, |
|
140 | - '100%' => 0 |
|
141 | - ] |
|
142 | - ]; |
|
143 | - |
|
144 | - foreach ($classes as $class) { |
|
145 | - foreach ($class['methods'] as $methodName => $method) { |
|
146 | - if ($method['coverage'] == 0) { |
|
147 | - $result['method']['0%']++; |
|
148 | - } elseif ($method['coverage'] == 100) { |
|
149 | - $result['method']['100%']++; |
|
150 | - } else { |
|
151 | - $key = floor($method['coverage'] / 10) * 10; |
|
152 | - $key = $key . '-' . ($key + 10) . '%'; |
|
153 | - $result['method'][$key]++; |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - if ($class['coverage'] == 0) { |
|
158 | - $result['class']['0%']++; |
|
159 | - } elseif ($class['coverage'] == 100) { |
|
160 | - $result['class']['100%']++; |
|
161 | - } else { |
|
162 | - $key = floor($class['coverage'] / 10) * 10; |
|
163 | - $key = $key . '-' . ($key + 10) . '%'; |
|
164 | - $result['class'][$key]++; |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - return [ |
|
169 | - 'class' => json_encode(array_values($result['class'])), |
|
170 | - 'method' => json_encode(array_values($result['method'])) |
|
171 | - ]; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Returns the classes / methods with insufficient coverage. |
|
176 | - * |
|
177 | - * @param array $classes |
|
178 | - * @param string $baseLink |
|
179 | - * |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function insufficientCoverage(array $classes, $baseLink) |
|
183 | - { |
|
184 | - $leastTestedClasses = []; |
|
185 | - $leastTestedMethods = []; |
|
186 | - $result = ['class' => '', 'method' => '']; |
|
187 | - |
|
188 | - foreach ($classes as $className => $class) { |
|
189 | - foreach ($class['methods'] as $methodName => $method) { |
|
190 | - if ($method['coverage'] < $this->highLowerBound) { |
|
191 | - if ($className != '*') { |
|
192 | - $key = $className . '::' . $methodName; |
|
193 | - } else { |
|
194 | - $key = $methodName; |
|
195 | - } |
|
196 | - |
|
197 | - $leastTestedMethods[$key] = $method['coverage']; |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - if ($class['coverage'] < $this->highLowerBound) { |
|
202 | - $leastTestedClasses[$className] = $class['coverage']; |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - asort($leastTestedClasses); |
|
207 | - asort($leastTestedMethods); |
|
208 | - |
|
209 | - foreach ($leastTestedClasses as $className => $coverage) { |
|
210 | - $result['class'] .= sprintf( |
|
211 | - ' <tr><td><a href="%s">%s</a></td><td class="text-right">%d%%</td></tr>' . "\n", |
|
212 | - str_replace($baseLink, '', $classes[$className]['link']), |
|
213 | - $className, |
|
214 | - $coverage |
|
215 | - ); |
|
216 | - } |
|
217 | - |
|
218 | - foreach ($leastTestedMethods as $methodName => $coverage) { |
|
219 | - list($class, $method) = explode('::', $methodName); |
|
220 | - |
|
221 | - $result['method'] .= sprintf( |
|
222 | - ' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%d%%</td></tr>' . "\n", |
|
223 | - str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']), |
|
224 | - $methodName, |
|
225 | - $method, |
|
226 | - $coverage |
|
227 | - ); |
|
228 | - } |
|
229 | - |
|
230 | - return $result; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Returns the project risks according to the CRAP index. |
|
235 | - * |
|
236 | - * @param array $classes |
|
237 | - * @param string $baseLink |
|
238 | - * |
|
239 | - * @return array |
|
240 | - */ |
|
241 | - protected function projectRisks(array $classes, $baseLink) |
|
242 | - { |
|
243 | - $classRisks = []; |
|
244 | - $methodRisks = []; |
|
245 | - $result = ['class' => '', 'method' => '']; |
|
246 | - |
|
247 | - foreach ($classes as $className => $class) { |
|
248 | - foreach ($class['methods'] as $methodName => $method) { |
|
249 | - if ($method['coverage'] < $this->highLowerBound && |
|
250 | - $method['ccn'] > 1) { |
|
251 | - if ($className != '*') { |
|
252 | - $key = $className . '::' . $methodName; |
|
253 | - } else { |
|
254 | - $key = $methodName; |
|
255 | - } |
|
256 | - |
|
257 | - $methodRisks[$key] = $method['crap']; |
|
258 | - } |
|
259 | - } |
|
260 | - |
|
261 | - if ($class['coverage'] < $this->highLowerBound && |
|
262 | - $class['ccn'] > count($class['methods'])) { |
|
263 | - $classRisks[$className] = $class['crap']; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - arsort($classRisks); |
|
268 | - arsort($methodRisks); |
|
269 | - |
|
270 | - foreach ($classRisks as $className => $crap) { |
|
271 | - $result['class'] .= sprintf( |
|
272 | - ' <tr><td><a href="%s">%s</a></td><td class="text-right">%d</td></tr>' . "\n", |
|
273 | - str_replace($baseLink, '', $classes[$className]['link']), |
|
274 | - $className, |
|
275 | - $crap |
|
276 | - ); |
|
277 | - } |
|
278 | - |
|
279 | - foreach ($methodRisks as $methodName => $crap) { |
|
280 | - list($class, $method) = explode('::', $methodName); |
|
281 | - |
|
282 | - $result['method'] .= sprintf( |
|
283 | - ' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%d</td></tr>' . "\n", |
|
284 | - str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']), |
|
285 | - $methodName, |
|
286 | - $method, |
|
287 | - $crap |
|
288 | - ); |
|
289 | - } |
|
290 | - |
|
291 | - return $result; |
|
292 | - } |
|
293 | - |
|
294 | - protected function getActiveBreadcrumb(AbstractNode $node) |
|
295 | - { |
|
296 | - return sprintf( |
|
297 | - ' <li><a href="index.html">%s</a></li>' . "\n" . |
|
298 | - ' <li class="active">(Dashboard)</li>' . "\n", |
|
299 | - $node->getName() |
|
300 | - ); |
|
301 | - } |
|
21 | + /** |
|
22 | + * @param DirectoryNode $node |
|
23 | + * @param string $file |
|
24 | + */ |
|
25 | + public function render(DirectoryNode $node, $file) |
|
26 | + { |
|
27 | + $classes = $node->getClassesAndTraits(); |
|
28 | + $template = new \Text_Template( |
|
29 | + $this->templatePath . 'dashboard.html', |
|
30 | + '{{', |
|
31 | + '}}' |
|
32 | + ); |
|
33 | + |
|
34 | + $this->setCommonTemplateVariables($template, $node); |
|
35 | + |
|
36 | + $baseLink = $node->getId() . '/'; |
|
37 | + $complexity = $this->complexity($classes, $baseLink); |
|
38 | + $coverageDistribution = $this->coverageDistribution($classes); |
|
39 | + $insufficientCoverage = $this->insufficientCoverage($classes, $baseLink); |
|
40 | + $projectRisks = $this->projectRisks($classes, $baseLink); |
|
41 | + |
|
42 | + $template->setVar( |
|
43 | + [ |
|
44 | + 'insufficient_coverage_classes' => $insufficientCoverage['class'], |
|
45 | + 'insufficient_coverage_methods' => $insufficientCoverage['method'], |
|
46 | + 'project_risks_classes' => $projectRisks['class'], |
|
47 | + 'project_risks_methods' => $projectRisks['method'], |
|
48 | + 'complexity_class' => $complexity['class'], |
|
49 | + 'complexity_method' => $complexity['method'], |
|
50 | + 'class_coverage_distribution' => $coverageDistribution['class'], |
|
51 | + 'method_coverage_distribution' => $coverageDistribution['method'] |
|
52 | + ] |
|
53 | + ); |
|
54 | + |
|
55 | + $template->renderTo($file); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns the data for the Class/Method Complexity charts. |
|
60 | + * |
|
61 | + * @param array $classes |
|
62 | + * @param string $baseLink |
|
63 | + * |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + protected function complexity(array $classes, $baseLink) |
|
67 | + { |
|
68 | + $result = ['class' => [], 'method' => []]; |
|
69 | + |
|
70 | + foreach ($classes as $className => $class) { |
|
71 | + foreach ($class['methods'] as $methodName => $method) { |
|
72 | + if ($className != '*') { |
|
73 | + $methodName = $className . '::' . $methodName; |
|
74 | + } |
|
75 | + |
|
76 | + $result['method'][] = [ |
|
77 | + $method['coverage'], |
|
78 | + $method['ccn'], |
|
79 | + sprintf( |
|
80 | + '<a href="%s">%s</a>', |
|
81 | + str_replace($baseLink, '', $method['link']), |
|
82 | + $methodName |
|
83 | + ) |
|
84 | + ]; |
|
85 | + } |
|
86 | + |
|
87 | + $result['class'][] = [ |
|
88 | + $class['coverage'], |
|
89 | + $class['ccn'], |
|
90 | + sprintf( |
|
91 | + '<a href="%s">%s</a>', |
|
92 | + str_replace($baseLink, '', $class['link']), |
|
93 | + $className |
|
94 | + ) |
|
95 | + ]; |
|
96 | + } |
|
97 | + |
|
98 | + return [ |
|
99 | + 'class' => json_encode($result['class']), |
|
100 | + 'method' => json_encode($result['method']) |
|
101 | + ]; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Returns the data for the Class / Method Coverage Distribution chart. |
|
106 | + * |
|
107 | + * @param array $classes |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + protected function coverageDistribution(array $classes) |
|
112 | + { |
|
113 | + $result = [ |
|
114 | + 'class' => [ |
|
115 | + '0%' => 0, |
|
116 | + '0-10%' => 0, |
|
117 | + '10-20%' => 0, |
|
118 | + '20-30%' => 0, |
|
119 | + '30-40%' => 0, |
|
120 | + '40-50%' => 0, |
|
121 | + '50-60%' => 0, |
|
122 | + '60-70%' => 0, |
|
123 | + '70-80%' => 0, |
|
124 | + '80-90%' => 0, |
|
125 | + '90-100%' => 0, |
|
126 | + '100%' => 0 |
|
127 | + ], |
|
128 | + 'method' => [ |
|
129 | + '0%' => 0, |
|
130 | + '0-10%' => 0, |
|
131 | + '10-20%' => 0, |
|
132 | + '20-30%' => 0, |
|
133 | + '30-40%' => 0, |
|
134 | + '40-50%' => 0, |
|
135 | + '50-60%' => 0, |
|
136 | + '60-70%' => 0, |
|
137 | + '70-80%' => 0, |
|
138 | + '80-90%' => 0, |
|
139 | + '90-100%' => 0, |
|
140 | + '100%' => 0 |
|
141 | + ] |
|
142 | + ]; |
|
143 | + |
|
144 | + foreach ($classes as $class) { |
|
145 | + foreach ($class['methods'] as $methodName => $method) { |
|
146 | + if ($method['coverage'] == 0) { |
|
147 | + $result['method']['0%']++; |
|
148 | + } elseif ($method['coverage'] == 100) { |
|
149 | + $result['method']['100%']++; |
|
150 | + } else { |
|
151 | + $key = floor($method['coverage'] / 10) * 10; |
|
152 | + $key = $key . '-' . ($key + 10) . '%'; |
|
153 | + $result['method'][$key]++; |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + if ($class['coverage'] == 0) { |
|
158 | + $result['class']['0%']++; |
|
159 | + } elseif ($class['coverage'] == 100) { |
|
160 | + $result['class']['100%']++; |
|
161 | + } else { |
|
162 | + $key = floor($class['coverage'] / 10) * 10; |
|
163 | + $key = $key . '-' . ($key + 10) . '%'; |
|
164 | + $result['class'][$key]++; |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + return [ |
|
169 | + 'class' => json_encode(array_values($result['class'])), |
|
170 | + 'method' => json_encode(array_values($result['method'])) |
|
171 | + ]; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Returns the classes / methods with insufficient coverage. |
|
176 | + * |
|
177 | + * @param array $classes |
|
178 | + * @param string $baseLink |
|
179 | + * |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function insufficientCoverage(array $classes, $baseLink) |
|
183 | + { |
|
184 | + $leastTestedClasses = []; |
|
185 | + $leastTestedMethods = []; |
|
186 | + $result = ['class' => '', 'method' => '']; |
|
187 | + |
|
188 | + foreach ($classes as $className => $class) { |
|
189 | + foreach ($class['methods'] as $methodName => $method) { |
|
190 | + if ($method['coverage'] < $this->highLowerBound) { |
|
191 | + if ($className != '*') { |
|
192 | + $key = $className . '::' . $methodName; |
|
193 | + } else { |
|
194 | + $key = $methodName; |
|
195 | + } |
|
196 | + |
|
197 | + $leastTestedMethods[$key] = $method['coverage']; |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + if ($class['coverage'] < $this->highLowerBound) { |
|
202 | + $leastTestedClasses[$className] = $class['coverage']; |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + asort($leastTestedClasses); |
|
207 | + asort($leastTestedMethods); |
|
208 | + |
|
209 | + foreach ($leastTestedClasses as $className => $coverage) { |
|
210 | + $result['class'] .= sprintf( |
|
211 | + ' <tr><td><a href="%s">%s</a></td><td class="text-right">%d%%</td></tr>' . "\n", |
|
212 | + str_replace($baseLink, '', $classes[$className]['link']), |
|
213 | + $className, |
|
214 | + $coverage |
|
215 | + ); |
|
216 | + } |
|
217 | + |
|
218 | + foreach ($leastTestedMethods as $methodName => $coverage) { |
|
219 | + list($class, $method) = explode('::', $methodName); |
|
220 | + |
|
221 | + $result['method'] .= sprintf( |
|
222 | + ' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%d%%</td></tr>' . "\n", |
|
223 | + str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']), |
|
224 | + $methodName, |
|
225 | + $method, |
|
226 | + $coverage |
|
227 | + ); |
|
228 | + } |
|
229 | + |
|
230 | + return $result; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Returns the project risks according to the CRAP index. |
|
235 | + * |
|
236 | + * @param array $classes |
|
237 | + * @param string $baseLink |
|
238 | + * |
|
239 | + * @return array |
|
240 | + */ |
|
241 | + protected function projectRisks(array $classes, $baseLink) |
|
242 | + { |
|
243 | + $classRisks = []; |
|
244 | + $methodRisks = []; |
|
245 | + $result = ['class' => '', 'method' => '']; |
|
246 | + |
|
247 | + foreach ($classes as $className => $class) { |
|
248 | + foreach ($class['methods'] as $methodName => $method) { |
|
249 | + if ($method['coverage'] < $this->highLowerBound && |
|
250 | + $method['ccn'] > 1) { |
|
251 | + if ($className != '*') { |
|
252 | + $key = $className . '::' . $methodName; |
|
253 | + } else { |
|
254 | + $key = $methodName; |
|
255 | + } |
|
256 | + |
|
257 | + $methodRisks[$key] = $method['crap']; |
|
258 | + } |
|
259 | + } |
|
260 | + |
|
261 | + if ($class['coverage'] < $this->highLowerBound && |
|
262 | + $class['ccn'] > count($class['methods'])) { |
|
263 | + $classRisks[$className] = $class['crap']; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + arsort($classRisks); |
|
268 | + arsort($methodRisks); |
|
269 | + |
|
270 | + foreach ($classRisks as $className => $crap) { |
|
271 | + $result['class'] .= sprintf( |
|
272 | + ' <tr><td><a href="%s">%s</a></td><td class="text-right">%d</td></tr>' . "\n", |
|
273 | + str_replace($baseLink, '', $classes[$className]['link']), |
|
274 | + $className, |
|
275 | + $crap |
|
276 | + ); |
|
277 | + } |
|
278 | + |
|
279 | + foreach ($methodRisks as $methodName => $crap) { |
|
280 | + list($class, $method) = explode('::', $methodName); |
|
281 | + |
|
282 | + $result['method'] .= sprintf( |
|
283 | + ' <tr><td><a href="%s"><abbr title="%s">%s</abbr></a></td><td class="text-right">%d</td></tr>' . "\n", |
|
284 | + str_replace($baseLink, '', $classes[$class]['methods'][$method]['link']), |
|
285 | + $methodName, |
|
286 | + $method, |
|
287 | + $crap |
|
288 | + ); |
|
289 | + } |
|
290 | + |
|
291 | + return $result; |
|
292 | + } |
|
293 | + |
|
294 | + protected function getActiveBreadcrumb(AbstractNode $node) |
|
295 | + { |
|
296 | + return sprintf( |
|
297 | + ' <li><a href="index.html">%s</a></li>' . "\n" . |
|
298 | + ' <li class="active">(Dashboard)</li>' . "\n", |
|
299 | + $node->getName() |
|
300 | + ); |
|
301 | + } |
|
302 | 302 | } |
@@ -18,84 +18,84 @@ |
||
18 | 18 | */ |
19 | 19 | class Directory extends Renderer |
20 | 20 | { |
21 | - /** |
|
22 | - * @param DirectoryNode $node |
|
23 | - * @param string $file |
|
24 | - */ |
|
25 | - public function render(DirectoryNode $node, $file) |
|
26 | - { |
|
27 | - $template = new \Text_Template($this->templatePath . 'directory.html', '{{', '}}'); |
|
21 | + /** |
|
22 | + * @param DirectoryNode $node |
|
23 | + * @param string $file |
|
24 | + */ |
|
25 | + public function render(DirectoryNode $node, $file) |
|
26 | + { |
|
27 | + $template = new \Text_Template($this->templatePath . 'directory.html', '{{', '}}'); |
|
28 | 28 | |
29 | - $this->setCommonTemplateVariables($template, $node); |
|
29 | + $this->setCommonTemplateVariables($template, $node); |
|
30 | 30 | |
31 | - $items = $this->renderItem($node, true); |
|
31 | + $items = $this->renderItem($node, true); |
|
32 | 32 | |
33 | - foreach ($node->getDirectories() as $item) { |
|
34 | - $items .= $this->renderItem($item); |
|
35 | - } |
|
33 | + foreach ($node->getDirectories() as $item) { |
|
34 | + $items .= $this->renderItem($item); |
|
35 | + } |
|
36 | 36 | |
37 | - foreach ($node->getFiles() as $item) { |
|
38 | - $items .= $this->renderItem($item); |
|
39 | - } |
|
37 | + foreach ($node->getFiles() as $item) { |
|
38 | + $items .= $this->renderItem($item); |
|
39 | + } |
|
40 | 40 | |
41 | - $template->setVar( |
|
42 | - [ |
|
43 | - 'id' => $node->getId(), |
|
44 | - 'items' => $items |
|
45 | - ] |
|
46 | - ); |
|
41 | + $template->setVar( |
|
42 | + [ |
|
43 | + 'id' => $node->getId(), |
|
44 | + 'items' => $items |
|
45 | + ] |
|
46 | + ); |
|
47 | 47 | |
48 | - $template->renderTo($file); |
|
49 | - } |
|
48 | + $template->renderTo($file); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param Node $node |
|
53 | - * @param bool $total |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - protected function renderItem(Node $node, $total = false) |
|
58 | - { |
|
59 | - $data = [ |
|
60 | - 'numClasses' => $node->getNumClassesAndTraits(), |
|
61 | - 'numTestedClasses' => $node->getNumTestedClassesAndTraits(), |
|
62 | - 'numMethods' => $node->getNumMethods(), |
|
63 | - 'numTestedMethods' => $node->getNumTestedMethods(), |
|
64 | - 'linesExecutedPercent' => $node->getLineExecutedPercent(false), |
|
65 | - 'linesExecutedPercentAsString' => $node->getLineExecutedPercent(), |
|
66 | - 'numExecutedLines' => $node->getNumExecutedLines(), |
|
67 | - 'numExecutableLines' => $node->getNumExecutableLines(), |
|
68 | - 'testedMethodsPercent' => $node->getTestedMethodsPercent(false), |
|
69 | - 'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(), |
|
70 | - 'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false), |
|
71 | - 'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent() |
|
72 | - ]; |
|
51 | + /** |
|
52 | + * @param Node $node |
|
53 | + * @param bool $total |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + protected function renderItem(Node $node, $total = false) |
|
58 | + { |
|
59 | + $data = [ |
|
60 | + 'numClasses' => $node->getNumClassesAndTraits(), |
|
61 | + 'numTestedClasses' => $node->getNumTestedClassesAndTraits(), |
|
62 | + 'numMethods' => $node->getNumMethods(), |
|
63 | + 'numTestedMethods' => $node->getNumTestedMethods(), |
|
64 | + 'linesExecutedPercent' => $node->getLineExecutedPercent(false), |
|
65 | + 'linesExecutedPercentAsString' => $node->getLineExecutedPercent(), |
|
66 | + 'numExecutedLines' => $node->getNumExecutedLines(), |
|
67 | + 'numExecutableLines' => $node->getNumExecutableLines(), |
|
68 | + 'testedMethodsPercent' => $node->getTestedMethodsPercent(false), |
|
69 | + 'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(), |
|
70 | + 'testedClassesPercent' => $node->getTestedClassesAndTraitsPercent(false), |
|
71 | + 'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent() |
|
72 | + ]; |
|
73 | 73 | |
74 | - if ($total) { |
|
75 | - $data['name'] = 'Total'; |
|
76 | - } else { |
|
77 | - if ($node instanceof DirectoryNode) { |
|
78 | - $data['name'] = sprintf( |
|
79 | - '<a href="%s/index.html">%s</a>', |
|
80 | - $node->getName(), |
|
81 | - $node->getName() |
|
82 | - ); |
|
74 | + if ($total) { |
|
75 | + $data['name'] = 'Total'; |
|
76 | + } else { |
|
77 | + if ($node instanceof DirectoryNode) { |
|
78 | + $data['name'] = sprintf( |
|
79 | + '<a href="%s/index.html">%s</a>', |
|
80 | + $node->getName(), |
|
81 | + $node->getName() |
|
82 | + ); |
|
83 | 83 | |
84 | - $data['icon'] = '<span class="glyphicon glyphicon-folder-open"></span> '; |
|
85 | - } else { |
|
86 | - $data['name'] = sprintf( |
|
87 | - '<a href="%s.html">%s</a>', |
|
88 | - $node->getName(), |
|
89 | - $node->getName() |
|
90 | - ); |
|
84 | + $data['icon'] = '<span class="glyphicon glyphicon-folder-open"></span> '; |
|
85 | + } else { |
|
86 | + $data['name'] = sprintf( |
|
87 | + '<a href="%s.html">%s</a>', |
|
88 | + $node->getName(), |
|
89 | + $node->getName() |
|
90 | + ); |
|
91 | 91 | |
92 | - $data['icon'] = '<span class="glyphicon glyphicon-file"></span> '; |
|
93 | - } |
|
94 | - } |
|
92 | + $data['icon'] = '<span class="glyphicon glyphicon-file"></span> '; |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | - return $this->renderItemTemplate( |
|
97 | - new \Text_Template($this->templatePath . 'directory_item.html', '{{', '}}'), |
|
98 | - $data |
|
99 | - ); |
|
100 | - } |
|
96 | + return $this->renderItemTemplate( |
|
97 | + new \Text_Template($this->templatePath . 'directory_item.html', '{{', '}}'), |
|
98 | + $data |
|
99 | + ); |
|
100 | + } |
|
101 | 101 | } |
@@ -21,237 +21,237 @@ |
||
21 | 21 | */ |
22 | 22 | class Text |
23 | 23 | { |
24 | - private $lowUpperBound; |
|
25 | - private $highLowerBound; |
|
26 | - private $showUncoveredFiles; |
|
27 | - private $showOnlySummary; |
|
28 | - |
|
29 | - private $colors = [ |
|
30 | - 'green' => "\x1b[30;42m", |
|
31 | - 'yellow' => "\x1b[30;43m", |
|
32 | - 'red' => "\x1b[37;41m", |
|
33 | - 'header' => "\x1b[1;37;40m", |
|
34 | - 'reset' => "\x1b[0m", |
|
35 | - 'eol' => "\x1b[2K", |
|
36 | - ]; |
|
37 | - |
|
38 | - /** |
|
39 | - * @param int $lowUpperBound |
|
40 | - * @param int $highLowerBound |
|
41 | - * @param bool $showUncoveredFiles |
|
42 | - * @param bool $showOnlySummary |
|
43 | - */ |
|
44 | - public function __construct($lowUpperBound = 50, $highLowerBound = 90, $showUncoveredFiles = false, $showOnlySummary = false) |
|
45 | - { |
|
46 | - $this->lowUpperBound = $lowUpperBound; |
|
47 | - $this->highLowerBound = $highLowerBound; |
|
48 | - $this->showUncoveredFiles = $showUncoveredFiles; |
|
49 | - $this->showOnlySummary = $showOnlySummary; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param CodeCoverage $coverage |
|
54 | - * @param bool $showColors |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function process(CodeCoverage $coverage, $showColors = false) |
|
59 | - { |
|
60 | - $output = PHP_EOL . PHP_EOL; |
|
61 | - $report = $coverage->getReport(); |
|
62 | - unset($coverage); |
|
63 | - |
|
64 | - $colors = [ |
|
65 | - 'header' => '', |
|
66 | - 'classes' => '', |
|
67 | - 'methods' => '', |
|
68 | - 'lines' => '', |
|
69 | - 'reset' => '', |
|
70 | - 'eol' => '' |
|
71 | - ]; |
|
72 | - |
|
73 | - if ($showColors) { |
|
74 | - $colors['classes'] = $this->getCoverageColor( |
|
75 | - $report->getNumTestedClassesAndTraits(), |
|
76 | - $report->getNumClassesAndTraits() |
|
77 | - ); |
|
78 | - $colors['methods'] = $this->getCoverageColor( |
|
79 | - $report->getNumTestedMethods(), |
|
80 | - $report->getNumMethods() |
|
81 | - ); |
|
82 | - $colors['lines'] = $this->getCoverageColor( |
|
83 | - $report->getNumExecutedLines(), |
|
84 | - $report->getNumExecutableLines() |
|
85 | - ); |
|
86 | - $colors['reset'] = $this->colors['reset']; |
|
87 | - $colors['header'] = $this->colors['header']; |
|
88 | - $colors['eol'] = $this->colors['eol']; |
|
89 | - } |
|
90 | - |
|
91 | - $classes = sprintf( |
|
92 | - ' Classes: %6s (%d/%d)', |
|
93 | - Util::percent( |
|
94 | - $report->getNumTestedClassesAndTraits(), |
|
95 | - $report->getNumClassesAndTraits(), |
|
96 | - true |
|
97 | - ), |
|
98 | - $report->getNumTestedClassesAndTraits(), |
|
99 | - $report->getNumClassesAndTraits() |
|
100 | - ); |
|
101 | - |
|
102 | - $methods = sprintf( |
|
103 | - ' Methods: %6s (%d/%d)', |
|
104 | - Util::percent( |
|
105 | - $report->getNumTestedMethods(), |
|
106 | - $report->getNumMethods(), |
|
107 | - true |
|
108 | - ), |
|
109 | - $report->getNumTestedMethods(), |
|
110 | - $report->getNumMethods() |
|
111 | - ); |
|
112 | - |
|
113 | - $lines = sprintf( |
|
114 | - ' Lines: %6s (%d/%d)', |
|
115 | - Util::percent( |
|
116 | - $report->getNumExecutedLines(), |
|
117 | - $report->getNumExecutableLines(), |
|
118 | - true |
|
119 | - ), |
|
120 | - $report->getNumExecutedLines(), |
|
121 | - $report->getNumExecutableLines() |
|
122 | - ); |
|
123 | - |
|
124 | - $padding = max(array_map('strlen', [$classes, $methods, $lines])); |
|
125 | - |
|
126 | - if ($this->showOnlySummary) { |
|
127 | - $title = 'Code Coverage Report Summary:'; |
|
128 | - $padding = max($padding, strlen($title)); |
|
129 | - |
|
130 | - $output .= $this->format($colors['header'], $padding, $title); |
|
131 | - } else { |
|
132 | - $date = date(' Y-m-d H:i:s', $_SERVER['REQUEST_TIME']); |
|
133 | - $title = 'Code Coverage Report:'; |
|
134 | - |
|
135 | - $output .= $this->format($colors['header'], $padding, $title); |
|
136 | - $output .= $this->format($colors['header'], $padding, $date); |
|
137 | - $output .= $this->format($colors['header'], $padding, ''); |
|
138 | - $output .= $this->format($colors['header'], $padding, ' Summary:'); |
|
139 | - } |
|
140 | - |
|
141 | - $output .= $this->format($colors['classes'], $padding, $classes); |
|
142 | - $output .= $this->format($colors['methods'], $padding, $methods); |
|
143 | - $output .= $this->format($colors['lines'], $padding, $lines); |
|
144 | - |
|
145 | - if ($this->showOnlySummary) { |
|
146 | - return $output . PHP_EOL; |
|
147 | - } |
|
148 | - |
|
149 | - $classCoverage = []; |
|
150 | - |
|
151 | - foreach ($report as $item) { |
|
152 | - if (!$item instanceof File) { |
|
153 | - continue; |
|
154 | - } |
|
155 | - |
|
156 | - $classes = $item->getClassesAndTraits(); |
|
157 | - |
|
158 | - foreach ($classes as $className => $class) { |
|
159 | - $classStatements = 0; |
|
160 | - $coveredClassStatements = 0; |
|
161 | - $coveredMethods = 0; |
|
162 | - $classMethods = 0; |
|
163 | - |
|
164 | - foreach ($class['methods'] as $method) { |
|
165 | - if ($method['executableLines'] == 0) { |
|
166 | - continue; |
|
167 | - } |
|
168 | - |
|
169 | - $classMethods++; |
|
170 | - $classStatements += $method['executableLines']; |
|
171 | - $coveredClassStatements += $method['executedLines']; |
|
172 | - if ($method['coverage'] == 100) { |
|
173 | - $coveredMethods++; |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - if (!empty($class['package']['namespace'])) { |
|
178 | - $namespace = '\\' . $class['package']['namespace'] . '::'; |
|
179 | - } elseif (!empty($class['package']['fullPackage'])) { |
|
180 | - $namespace = '@' . $class['package']['fullPackage'] . '::'; |
|
181 | - } else { |
|
182 | - $namespace = ''; |
|
183 | - } |
|
184 | - |
|
185 | - $classCoverage[$namespace . $className] = [ |
|
186 | - 'namespace' => $namespace, |
|
187 | - 'className ' => $className, |
|
188 | - 'methodsCovered' => $coveredMethods, |
|
189 | - 'methodCount' => $classMethods, |
|
190 | - 'statementsCovered' => $coveredClassStatements, |
|
191 | - 'statementCount' => $classStatements, |
|
192 | - ]; |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - ksort($classCoverage); |
|
197 | - |
|
198 | - $methodColor = ''; |
|
199 | - $linesColor = ''; |
|
200 | - $resetColor = ''; |
|
201 | - |
|
202 | - foreach ($classCoverage as $fullQualifiedPath => $classInfo) { |
|
203 | - if ($classInfo['statementsCovered'] != 0 || |
|
204 | - $this->showUncoveredFiles) { |
|
205 | - if ($showColors) { |
|
206 | - $methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']); |
|
207 | - $linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']); |
|
208 | - $resetColor = $colors['reset']; |
|
209 | - } |
|
210 | - |
|
211 | - $output .= PHP_EOL . $fullQualifiedPath . PHP_EOL |
|
212 | - . ' ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], 2) . $resetColor . ' ' |
|
213 | - . ' ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], 3) . $resetColor |
|
214 | - ; |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - return $output . PHP_EOL; |
|
219 | - } |
|
220 | - |
|
221 | - protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElements) |
|
222 | - { |
|
223 | - $coverage = Util::percent( |
|
224 | - $numberOfCoveredElements, |
|
225 | - $totalNumberOfElements |
|
226 | - ); |
|
227 | - |
|
228 | - if ($coverage >= $this->highLowerBound) { |
|
229 | - return $this->colors['green']; |
|
230 | - } elseif ($coverage > $this->lowUpperBound) { |
|
231 | - return $this->colors['yellow']; |
|
232 | - } |
|
233 | - |
|
234 | - return $this->colors['red']; |
|
235 | - } |
|
236 | - |
|
237 | - protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $precision) |
|
238 | - { |
|
239 | - $format = '%' . $precision . 's'; |
|
240 | - |
|
241 | - return Util::percent( |
|
242 | - $numberOfCoveredElements, |
|
243 | - $totalNumberOfElements, |
|
244 | - true, |
|
245 | - true |
|
246 | - ) . |
|
247 | - ' (' . sprintf($format, $numberOfCoveredElements) . '/' . |
|
248 | - sprintf($format, $totalNumberOfElements) . ')'; |
|
249 | - } |
|
250 | - |
|
251 | - private function format($color, $padding, $string) |
|
252 | - { |
|
253 | - $reset = $color ? $this->colors['reset'] : ''; |
|
254 | - |
|
255 | - return $color . str_pad($string, $padding) . $reset . PHP_EOL; |
|
256 | - } |
|
24 | + private $lowUpperBound; |
|
25 | + private $highLowerBound; |
|
26 | + private $showUncoveredFiles; |
|
27 | + private $showOnlySummary; |
|
28 | + |
|
29 | + private $colors = [ |
|
30 | + 'green' => "\x1b[30;42m", |
|
31 | + 'yellow' => "\x1b[30;43m", |
|
32 | + 'red' => "\x1b[37;41m", |
|
33 | + 'header' => "\x1b[1;37;40m", |
|
34 | + 'reset' => "\x1b[0m", |
|
35 | + 'eol' => "\x1b[2K", |
|
36 | + ]; |
|
37 | + |
|
38 | + /** |
|
39 | + * @param int $lowUpperBound |
|
40 | + * @param int $highLowerBound |
|
41 | + * @param bool $showUncoveredFiles |
|
42 | + * @param bool $showOnlySummary |
|
43 | + */ |
|
44 | + public function __construct($lowUpperBound = 50, $highLowerBound = 90, $showUncoveredFiles = false, $showOnlySummary = false) |
|
45 | + { |
|
46 | + $this->lowUpperBound = $lowUpperBound; |
|
47 | + $this->highLowerBound = $highLowerBound; |
|
48 | + $this->showUncoveredFiles = $showUncoveredFiles; |
|
49 | + $this->showOnlySummary = $showOnlySummary; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param CodeCoverage $coverage |
|
54 | + * @param bool $showColors |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function process(CodeCoverage $coverage, $showColors = false) |
|
59 | + { |
|
60 | + $output = PHP_EOL . PHP_EOL; |
|
61 | + $report = $coverage->getReport(); |
|
62 | + unset($coverage); |
|
63 | + |
|
64 | + $colors = [ |
|
65 | + 'header' => '', |
|
66 | + 'classes' => '', |
|
67 | + 'methods' => '', |
|
68 | + 'lines' => '', |
|
69 | + 'reset' => '', |
|
70 | + 'eol' => '' |
|
71 | + ]; |
|
72 | + |
|
73 | + if ($showColors) { |
|
74 | + $colors['classes'] = $this->getCoverageColor( |
|
75 | + $report->getNumTestedClassesAndTraits(), |
|
76 | + $report->getNumClassesAndTraits() |
|
77 | + ); |
|
78 | + $colors['methods'] = $this->getCoverageColor( |
|
79 | + $report->getNumTestedMethods(), |
|
80 | + $report->getNumMethods() |
|
81 | + ); |
|
82 | + $colors['lines'] = $this->getCoverageColor( |
|
83 | + $report->getNumExecutedLines(), |
|
84 | + $report->getNumExecutableLines() |
|
85 | + ); |
|
86 | + $colors['reset'] = $this->colors['reset']; |
|
87 | + $colors['header'] = $this->colors['header']; |
|
88 | + $colors['eol'] = $this->colors['eol']; |
|
89 | + } |
|
90 | + |
|
91 | + $classes = sprintf( |
|
92 | + ' Classes: %6s (%d/%d)', |
|
93 | + Util::percent( |
|
94 | + $report->getNumTestedClassesAndTraits(), |
|
95 | + $report->getNumClassesAndTraits(), |
|
96 | + true |
|
97 | + ), |
|
98 | + $report->getNumTestedClassesAndTraits(), |
|
99 | + $report->getNumClassesAndTraits() |
|
100 | + ); |
|
101 | + |
|
102 | + $methods = sprintf( |
|
103 | + ' Methods: %6s (%d/%d)', |
|
104 | + Util::percent( |
|
105 | + $report->getNumTestedMethods(), |
|
106 | + $report->getNumMethods(), |
|
107 | + true |
|
108 | + ), |
|
109 | + $report->getNumTestedMethods(), |
|
110 | + $report->getNumMethods() |
|
111 | + ); |
|
112 | + |
|
113 | + $lines = sprintf( |
|
114 | + ' Lines: %6s (%d/%d)', |
|
115 | + Util::percent( |
|
116 | + $report->getNumExecutedLines(), |
|
117 | + $report->getNumExecutableLines(), |
|
118 | + true |
|
119 | + ), |
|
120 | + $report->getNumExecutedLines(), |
|
121 | + $report->getNumExecutableLines() |
|
122 | + ); |
|
123 | + |
|
124 | + $padding = max(array_map('strlen', [$classes, $methods, $lines])); |
|
125 | + |
|
126 | + if ($this->showOnlySummary) { |
|
127 | + $title = 'Code Coverage Report Summary:'; |
|
128 | + $padding = max($padding, strlen($title)); |
|
129 | + |
|
130 | + $output .= $this->format($colors['header'], $padding, $title); |
|
131 | + } else { |
|
132 | + $date = date(' Y-m-d H:i:s', $_SERVER['REQUEST_TIME']); |
|
133 | + $title = 'Code Coverage Report:'; |
|
134 | + |
|
135 | + $output .= $this->format($colors['header'], $padding, $title); |
|
136 | + $output .= $this->format($colors['header'], $padding, $date); |
|
137 | + $output .= $this->format($colors['header'], $padding, ''); |
|
138 | + $output .= $this->format($colors['header'], $padding, ' Summary:'); |
|
139 | + } |
|
140 | + |
|
141 | + $output .= $this->format($colors['classes'], $padding, $classes); |
|
142 | + $output .= $this->format($colors['methods'], $padding, $methods); |
|
143 | + $output .= $this->format($colors['lines'], $padding, $lines); |
|
144 | + |
|
145 | + if ($this->showOnlySummary) { |
|
146 | + return $output . PHP_EOL; |
|
147 | + } |
|
148 | + |
|
149 | + $classCoverage = []; |
|
150 | + |
|
151 | + foreach ($report as $item) { |
|
152 | + if (!$item instanceof File) { |
|
153 | + continue; |
|
154 | + } |
|
155 | + |
|
156 | + $classes = $item->getClassesAndTraits(); |
|
157 | + |
|
158 | + foreach ($classes as $className => $class) { |
|
159 | + $classStatements = 0; |
|
160 | + $coveredClassStatements = 0; |
|
161 | + $coveredMethods = 0; |
|
162 | + $classMethods = 0; |
|
163 | + |
|
164 | + foreach ($class['methods'] as $method) { |
|
165 | + if ($method['executableLines'] == 0) { |
|
166 | + continue; |
|
167 | + } |
|
168 | + |
|
169 | + $classMethods++; |
|
170 | + $classStatements += $method['executableLines']; |
|
171 | + $coveredClassStatements += $method['executedLines']; |
|
172 | + if ($method['coverage'] == 100) { |
|
173 | + $coveredMethods++; |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + if (!empty($class['package']['namespace'])) { |
|
178 | + $namespace = '\\' . $class['package']['namespace'] . '::'; |
|
179 | + } elseif (!empty($class['package']['fullPackage'])) { |
|
180 | + $namespace = '@' . $class['package']['fullPackage'] . '::'; |
|
181 | + } else { |
|
182 | + $namespace = ''; |
|
183 | + } |
|
184 | + |
|
185 | + $classCoverage[$namespace . $className] = [ |
|
186 | + 'namespace' => $namespace, |
|
187 | + 'className ' => $className, |
|
188 | + 'methodsCovered' => $coveredMethods, |
|
189 | + 'methodCount' => $classMethods, |
|
190 | + 'statementsCovered' => $coveredClassStatements, |
|
191 | + 'statementCount' => $classStatements, |
|
192 | + ]; |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + ksort($classCoverage); |
|
197 | + |
|
198 | + $methodColor = ''; |
|
199 | + $linesColor = ''; |
|
200 | + $resetColor = ''; |
|
201 | + |
|
202 | + foreach ($classCoverage as $fullQualifiedPath => $classInfo) { |
|
203 | + if ($classInfo['statementsCovered'] != 0 || |
|
204 | + $this->showUncoveredFiles) { |
|
205 | + if ($showColors) { |
|
206 | + $methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']); |
|
207 | + $linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']); |
|
208 | + $resetColor = $colors['reset']; |
|
209 | + } |
|
210 | + |
|
211 | + $output .= PHP_EOL . $fullQualifiedPath . PHP_EOL |
|
212 | + . ' ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], 2) . $resetColor . ' ' |
|
213 | + . ' ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], 3) . $resetColor |
|
214 | + ; |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + return $output . PHP_EOL; |
|
219 | + } |
|
220 | + |
|
221 | + protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElements) |
|
222 | + { |
|
223 | + $coverage = Util::percent( |
|
224 | + $numberOfCoveredElements, |
|
225 | + $totalNumberOfElements |
|
226 | + ); |
|
227 | + |
|
228 | + if ($coverage >= $this->highLowerBound) { |
|
229 | + return $this->colors['green']; |
|
230 | + } elseif ($coverage > $this->lowUpperBound) { |
|
231 | + return $this->colors['yellow']; |
|
232 | + } |
|
233 | + |
|
234 | + return $this->colors['red']; |
|
235 | + } |
|
236 | + |
|
237 | + protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $precision) |
|
238 | + { |
|
239 | + $format = '%' . $precision . 's'; |
|
240 | + |
|
241 | + return Util::percent( |
|
242 | + $numberOfCoveredElements, |
|
243 | + $totalNumberOfElements, |
|
244 | + true, |
|
245 | + true |
|
246 | + ) . |
|
247 | + ' (' . sprintf($format, $numberOfCoveredElements) . '/' . |
|
248 | + sprintf($format, $totalNumberOfElements) . ')'; |
|
249 | + } |
|
250 | + |
|
251 | + private function format($color, $padding, $string) |
|
252 | + { |
|
253 | + $reset = $color ? $this->colors['reset'] : ''; |
|
254 | + |
|
255 | + return $color . str_pad($string, $padding) . $reset . PHP_EOL; |
|
256 | + } |
|
257 | 257 | } |
@@ -18,234 +18,234 @@ |
||
18 | 18 | */ |
19 | 19 | class Clover |
20 | 20 | { |
21 | - /** |
|
22 | - * @param CodeCoverage $coverage |
|
23 | - * @param string $target |
|
24 | - * @param string $name |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function process(CodeCoverage $coverage, $target = null, $name = null) |
|
29 | - { |
|
30 | - $xmlDocument = new \DOMDocument('1.0', 'UTF-8'); |
|
31 | - $xmlDocument->formatOutput = true; |
|
32 | - |
|
33 | - $xmlCoverage = $xmlDocument->createElement('coverage'); |
|
34 | - $xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']); |
|
35 | - $xmlDocument->appendChild($xmlCoverage); |
|
36 | - |
|
37 | - $xmlProject = $xmlDocument->createElement('project'); |
|
38 | - $xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']); |
|
39 | - |
|
40 | - if (is_string($name)) { |
|
41 | - $xmlProject->setAttribute('name', $name); |
|
42 | - } |
|
43 | - |
|
44 | - $xmlCoverage->appendChild($xmlProject); |
|
45 | - |
|
46 | - $packages = []; |
|
47 | - $report = $coverage->getReport(); |
|
48 | - unset($coverage); |
|
49 | - |
|
50 | - foreach ($report as $item) { |
|
51 | - if (!$item instanceof File) { |
|
52 | - continue; |
|
53 | - } |
|
54 | - |
|
55 | - /* @var File $item */ |
|
56 | - |
|
57 | - $xmlFile = $xmlDocument->createElement('file'); |
|
58 | - $xmlFile->setAttribute('name', $item->getPath()); |
|
59 | - |
|
60 | - $classes = $item->getClassesAndTraits(); |
|
61 | - $coverage = $item->getCoverageData(); |
|
62 | - $lines = []; |
|
63 | - $namespace = 'global'; |
|
64 | - |
|
65 | - foreach ($classes as $className => $class) { |
|
66 | - $classStatements = 0; |
|
67 | - $coveredClassStatements = 0; |
|
68 | - $coveredMethods = 0; |
|
69 | - $classMethods = 0; |
|
70 | - |
|
71 | - foreach ($class['methods'] as $methodName => $method) { |
|
72 | - if ($method['executableLines'] == 0) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - |
|
76 | - $classMethods++; |
|
77 | - $classStatements += $method['executableLines']; |
|
78 | - $coveredClassStatements += $method['executedLines']; |
|
79 | - |
|
80 | - if ($method['coverage'] == 100) { |
|
81 | - $coveredMethods++; |
|
82 | - } |
|
83 | - |
|
84 | - $methodCount = 0; |
|
85 | - |
|
86 | - foreach (range($method['startLine'], $method['endLine']) as $line) { |
|
87 | - if (isset($coverage[$line]) && ($coverage[$line] !== null)) { |
|
88 | - $methodCount = max($methodCount, count($coverage[$line])); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - $lines[$method['startLine']] = [ |
|
93 | - 'ccn' => $method['ccn'], |
|
94 | - 'count' => $methodCount, |
|
95 | - 'crap' => $method['crap'], |
|
96 | - 'type' => 'method', |
|
97 | - 'visibility' => $method['visibility'], |
|
98 | - 'name' => $methodName |
|
99 | - ]; |
|
100 | - } |
|
101 | - |
|
102 | - if (!empty($class['package']['namespace'])) { |
|
103 | - $namespace = $class['package']['namespace']; |
|
104 | - } |
|
105 | - |
|
106 | - $xmlClass = $xmlDocument->createElement('class'); |
|
107 | - $xmlClass->setAttribute('name', $className); |
|
108 | - $xmlClass->setAttribute('namespace', $namespace); |
|
109 | - |
|
110 | - if (!empty($class['package']['fullPackage'])) { |
|
111 | - $xmlClass->setAttribute( |
|
112 | - 'fullPackage', |
|
113 | - $class['package']['fullPackage'] |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - if (!empty($class['package']['category'])) { |
|
118 | - $xmlClass->setAttribute( |
|
119 | - 'category', |
|
120 | - $class['package']['category'] |
|
121 | - ); |
|
122 | - } |
|
123 | - |
|
124 | - if (!empty($class['package']['package'])) { |
|
125 | - $xmlClass->setAttribute( |
|
126 | - 'package', |
|
127 | - $class['package']['package'] |
|
128 | - ); |
|
129 | - } |
|
130 | - |
|
131 | - if (!empty($class['package']['subpackage'])) { |
|
132 | - $xmlClass->setAttribute( |
|
133 | - 'subpackage', |
|
134 | - $class['package']['subpackage'] |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - $xmlFile->appendChild($xmlClass); |
|
139 | - |
|
140 | - $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
141 | - $xmlMetrics->setAttribute('complexity', $class['ccn']); |
|
142 | - $xmlMetrics->setAttribute('methods', $classMethods); |
|
143 | - $xmlMetrics->setAttribute('coveredmethods', $coveredMethods); |
|
144 | - $xmlMetrics->setAttribute('conditionals', 0); |
|
145 | - $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
146 | - $xmlMetrics->setAttribute('statements', $classStatements); |
|
147 | - $xmlMetrics->setAttribute('coveredstatements', $coveredClassStatements); |
|
148 | - $xmlMetrics->setAttribute('elements', $classMethods + $classStatements /* + conditionals */); |
|
149 | - $xmlMetrics->setAttribute('coveredelements', $coveredMethods + $coveredClassStatements /* + coveredconditionals */); |
|
150 | - $xmlClass->appendChild($xmlMetrics); |
|
151 | - } |
|
152 | - |
|
153 | - foreach ($coverage as $line => $data) { |
|
154 | - if ($data === null || isset($lines[$line])) { |
|
155 | - continue; |
|
156 | - } |
|
157 | - |
|
158 | - $lines[$line] = [ |
|
159 | - 'count' => count($data), 'type' => 'stmt' |
|
160 | - ]; |
|
161 | - } |
|
162 | - |
|
163 | - ksort($lines); |
|
164 | - |
|
165 | - foreach ($lines as $line => $data) { |
|
166 | - $xmlLine = $xmlDocument->createElement('line'); |
|
167 | - $xmlLine->setAttribute('num', $line); |
|
168 | - $xmlLine->setAttribute('type', $data['type']); |
|
169 | - |
|
170 | - if (isset($data['name'])) { |
|
171 | - $xmlLine->setAttribute('name', $data['name']); |
|
172 | - } |
|
173 | - |
|
174 | - if (isset($data['visibility'])) { |
|
175 | - $xmlLine->setAttribute('visibility', $data['visibility']); |
|
176 | - } |
|
177 | - |
|
178 | - if (isset($data['ccn'])) { |
|
179 | - $xmlLine->setAttribute('complexity', $data['ccn']); |
|
180 | - } |
|
181 | - |
|
182 | - if (isset($data['crap'])) { |
|
183 | - $xmlLine->setAttribute('crap', $data['crap']); |
|
184 | - } |
|
185 | - |
|
186 | - $xmlLine->setAttribute('count', $data['count']); |
|
187 | - $xmlFile->appendChild($xmlLine); |
|
188 | - } |
|
189 | - |
|
190 | - $linesOfCode = $item->getLinesOfCode(); |
|
191 | - |
|
192 | - $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
193 | - $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
|
194 | - $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
|
195 | - $xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits()); |
|
196 | - $xmlMetrics->setAttribute('methods', $item->getNumMethods()); |
|
197 | - $xmlMetrics->setAttribute('coveredmethods', $item->getNumTestedMethods()); |
|
198 | - $xmlMetrics->setAttribute('conditionals', 0); |
|
199 | - $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
200 | - $xmlMetrics->setAttribute('statements', $item->getNumExecutableLines()); |
|
201 | - $xmlMetrics->setAttribute('coveredstatements', $item->getNumExecutedLines()); |
|
202 | - $xmlMetrics->setAttribute('elements', $item->getNumMethods() + $item->getNumExecutableLines() /* + conditionals */); |
|
203 | - $xmlMetrics->setAttribute('coveredelements', $item->getNumTestedMethods() + $item->getNumExecutedLines() /* + coveredconditionals */); |
|
204 | - $xmlFile->appendChild($xmlMetrics); |
|
205 | - |
|
206 | - if ($namespace == 'global') { |
|
207 | - $xmlProject->appendChild($xmlFile); |
|
208 | - } else { |
|
209 | - if (!isset($packages[$namespace])) { |
|
210 | - $packages[$namespace] = $xmlDocument->createElement( |
|
211 | - 'package' |
|
212 | - ); |
|
213 | - |
|
214 | - $packages[$namespace]->setAttribute('name', $namespace); |
|
215 | - $xmlProject->appendChild($packages[$namespace]); |
|
216 | - } |
|
217 | - |
|
218 | - $packages[$namespace]->appendChild($xmlFile); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - $linesOfCode = $report->getLinesOfCode(); |
|
223 | - |
|
224 | - $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
225 | - $xmlMetrics->setAttribute('files', count($report)); |
|
226 | - $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
|
227 | - $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
|
228 | - $xmlMetrics->setAttribute('classes', $report->getNumClassesAndTraits()); |
|
229 | - $xmlMetrics->setAttribute('methods', $report->getNumMethods()); |
|
230 | - $xmlMetrics->setAttribute('coveredmethods', $report->getNumTestedMethods()); |
|
231 | - $xmlMetrics->setAttribute('conditionals', 0); |
|
232 | - $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
233 | - $xmlMetrics->setAttribute('statements', $report->getNumExecutableLines()); |
|
234 | - $xmlMetrics->setAttribute('coveredstatements', $report->getNumExecutedLines()); |
|
235 | - $xmlMetrics->setAttribute('elements', $report->getNumMethods() + $report->getNumExecutableLines() /* + conditionals */); |
|
236 | - $xmlMetrics->setAttribute('coveredelements', $report->getNumTestedMethods() + $report->getNumExecutedLines() /* + coveredconditionals */); |
|
237 | - $xmlProject->appendChild($xmlMetrics); |
|
238 | - |
|
239 | - $buffer = $xmlDocument->saveXML(); |
|
240 | - |
|
241 | - if ($target !== null) { |
|
242 | - if (!is_dir(dirname($target))) { |
|
243 | - mkdir(dirname($target), 0777, true); |
|
244 | - } |
|
245 | - |
|
246 | - file_put_contents($target, $buffer); |
|
247 | - } |
|
248 | - |
|
249 | - return $buffer; |
|
250 | - } |
|
21 | + /** |
|
22 | + * @param CodeCoverage $coverage |
|
23 | + * @param string $target |
|
24 | + * @param string $name |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function process(CodeCoverage $coverage, $target = null, $name = null) |
|
29 | + { |
|
30 | + $xmlDocument = new \DOMDocument('1.0', 'UTF-8'); |
|
31 | + $xmlDocument->formatOutput = true; |
|
32 | + |
|
33 | + $xmlCoverage = $xmlDocument->createElement('coverage'); |
|
34 | + $xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']); |
|
35 | + $xmlDocument->appendChild($xmlCoverage); |
|
36 | + |
|
37 | + $xmlProject = $xmlDocument->createElement('project'); |
|
38 | + $xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']); |
|
39 | + |
|
40 | + if (is_string($name)) { |
|
41 | + $xmlProject->setAttribute('name', $name); |
|
42 | + } |
|
43 | + |
|
44 | + $xmlCoverage->appendChild($xmlProject); |
|
45 | + |
|
46 | + $packages = []; |
|
47 | + $report = $coverage->getReport(); |
|
48 | + unset($coverage); |
|
49 | + |
|
50 | + foreach ($report as $item) { |
|
51 | + if (!$item instanceof File) { |
|
52 | + continue; |
|
53 | + } |
|
54 | + |
|
55 | + /* @var File $item */ |
|
56 | + |
|
57 | + $xmlFile = $xmlDocument->createElement('file'); |
|
58 | + $xmlFile->setAttribute('name', $item->getPath()); |
|
59 | + |
|
60 | + $classes = $item->getClassesAndTraits(); |
|
61 | + $coverage = $item->getCoverageData(); |
|
62 | + $lines = []; |
|
63 | + $namespace = 'global'; |
|
64 | + |
|
65 | + foreach ($classes as $className => $class) { |
|
66 | + $classStatements = 0; |
|
67 | + $coveredClassStatements = 0; |
|
68 | + $coveredMethods = 0; |
|
69 | + $classMethods = 0; |
|
70 | + |
|
71 | + foreach ($class['methods'] as $methodName => $method) { |
|
72 | + if ($method['executableLines'] == 0) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + |
|
76 | + $classMethods++; |
|
77 | + $classStatements += $method['executableLines']; |
|
78 | + $coveredClassStatements += $method['executedLines']; |
|
79 | + |
|
80 | + if ($method['coverage'] == 100) { |
|
81 | + $coveredMethods++; |
|
82 | + } |
|
83 | + |
|
84 | + $methodCount = 0; |
|
85 | + |
|
86 | + foreach (range($method['startLine'], $method['endLine']) as $line) { |
|
87 | + if (isset($coverage[$line]) && ($coverage[$line] !== null)) { |
|
88 | + $methodCount = max($methodCount, count($coverage[$line])); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + $lines[$method['startLine']] = [ |
|
93 | + 'ccn' => $method['ccn'], |
|
94 | + 'count' => $methodCount, |
|
95 | + 'crap' => $method['crap'], |
|
96 | + 'type' => 'method', |
|
97 | + 'visibility' => $method['visibility'], |
|
98 | + 'name' => $methodName |
|
99 | + ]; |
|
100 | + } |
|
101 | + |
|
102 | + if (!empty($class['package']['namespace'])) { |
|
103 | + $namespace = $class['package']['namespace']; |
|
104 | + } |
|
105 | + |
|
106 | + $xmlClass = $xmlDocument->createElement('class'); |
|
107 | + $xmlClass->setAttribute('name', $className); |
|
108 | + $xmlClass->setAttribute('namespace', $namespace); |
|
109 | + |
|
110 | + if (!empty($class['package']['fullPackage'])) { |
|
111 | + $xmlClass->setAttribute( |
|
112 | + 'fullPackage', |
|
113 | + $class['package']['fullPackage'] |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + if (!empty($class['package']['category'])) { |
|
118 | + $xmlClass->setAttribute( |
|
119 | + 'category', |
|
120 | + $class['package']['category'] |
|
121 | + ); |
|
122 | + } |
|
123 | + |
|
124 | + if (!empty($class['package']['package'])) { |
|
125 | + $xmlClass->setAttribute( |
|
126 | + 'package', |
|
127 | + $class['package']['package'] |
|
128 | + ); |
|
129 | + } |
|
130 | + |
|
131 | + if (!empty($class['package']['subpackage'])) { |
|
132 | + $xmlClass->setAttribute( |
|
133 | + 'subpackage', |
|
134 | + $class['package']['subpackage'] |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + $xmlFile->appendChild($xmlClass); |
|
139 | + |
|
140 | + $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
141 | + $xmlMetrics->setAttribute('complexity', $class['ccn']); |
|
142 | + $xmlMetrics->setAttribute('methods', $classMethods); |
|
143 | + $xmlMetrics->setAttribute('coveredmethods', $coveredMethods); |
|
144 | + $xmlMetrics->setAttribute('conditionals', 0); |
|
145 | + $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
146 | + $xmlMetrics->setAttribute('statements', $classStatements); |
|
147 | + $xmlMetrics->setAttribute('coveredstatements', $coveredClassStatements); |
|
148 | + $xmlMetrics->setAttribute('elements', $classMethods + $classStatements /* + conditionals */); |
|
149 | + $xmlMetrics->setAttribute('coveredelements', $coveredMethods + $coveredClassStatements /* + coveredconditionals */); |
|
150 | + $xmlClass->appendChild($xmlMetrics); |
|
151 | + } |
|
152 | + |
|
153 | + foreach ($coverage as $line => $data) { |
|
154 | + if ($data === null || isset($lines[$line])) { |
|
155 | + continue; |
|
156 | + } |
|
157 | + |
|
158 | + $lines[$line] = [ |
|
159 | + 'count' => count($data), 'type' => 'stmt' |
|
160 | + ]; |
|
161 | + } |
|
162 | + |
|
163 | + ksort($lines); |
|
164 | + |
|
165 | + foreach ($lines as $line => $data) { |
|
166 | + $xmlLine = $xmlDocument->createElement('line'); |
|
167 | + $xmlLine->setAttribute('num', $line); |
|
168 | + $xmlLine->setAttribute('type', $data['type']); |
|
169 | + |
|
170 | + if (isset($data['name'])) { |
|
171 | + $xmlLine->setAttribute('name', $data['name']); |
|
172 | + } |
|
173 | + |
|
174 | + if (isset($data['visibility'])) { |
|
175 | + $xmlLine->setAttribute('visibility', $data['visibility']); |
|
176 | + } |
|
177 | + |
|
178 | + if (isset($data['ccn'])) { |
|
179 | + $xmlLine->setAttribute('complexity', $data['ccn']); |
|
180 | + } |
|
181 | + |
|
182 | + if (isset($data['crap'])) { |
|
183 | + $xmlLine->setAttribute('crap', $data['crap']); |
|
184 | + } |
|
185 | + |
|
186 | + $xmlLine->setAttribute('count', $data['count']); |
|
187 | + $xmlFile->appendChild($xmlLine); |
|
188 | + } |
|
189 | + |
|
190 | + $linesOfCode = $item->getLinesOfCode(); |
|
191 | + |
|
192 | + $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
193 | + $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
|
194 | + $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
|
195 | + $xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits()); |
|
196 | + $xmlMetrics->setAttribute('methods', $item->getNumMethods()); |
|
197 | + $xmlMetrics->setAttribute('coveredmethods', $item->getNumTestedMethods()); |
|
198 | + $xmlMetrics->setAttribute('conditionals', 0); |
|
199 | + $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
200 | + $xmlMetrics->setAttribute('statements', $item->getNumExecutableLines()); |
|
201 | + $xmlMetrics->setAttribute('coveredstatements', $item->getNumExecutedLines()); |
|
202 | + $xmlMetrics->setAttribute('elements', $item->getNumMethods() + $item->getNumExecutableLines() /* + conditionals */); |
|
203 | + $xmlMetrics->setAttribute('coveredelements', $item->getNumTestedMethods() + $item->getNumExecutedLines() /* + coveredconditionals */); |
|
204 | + $xmlFile->appendChild($xmlMetrics); |
|
205 | + |
|
206 | + if ($namespace == 'global') { |
|
207 | + $xmlProject->appendChild($xmlFile); |
|
208 | + } else { |
|
209 | + if (!isset($packages[$namespace])) { |
|
210 | + $packages[$namespace] = $xmlDocument->createElement( |
|
211 | + 'package' |
|
212 | + ); |
|
213 | + |
|
214 | + $packages[$namespace]->setAttribute('name', $namespace); |
|
215 | + $xmlProject->appendChild($packages[$namespace]); |
|
216 | + } |
|
217 | + |
|
218 | + $packages[$namespace]->appendChild($xmlFile); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + $linesOfCode = $report->getLinesOfCode(); |
|
223 | + |
|
224 | + $xmlMetrics = $xmlDocument->createElement('metrics'); |
|
225 | + $xmlMetrics->setAttribute('files', count($report)); |
|
226 | + $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
|
227 | + $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
|
228 | + $xmlMetrics->setAttribute('classes', $report->getNumClassesAndTraits()); |
|
229 | + $xmlMetrics->setAttribute('methods', $report->getNumMethods()); |
|
230 | + $xmlMetrics->setAttribute('coveredmethods', $report->getNumTestedMethods()); |
|
231 | + $xmlMetrics->setAttribute('conditionals', 0); |
|
232 | + $xmlMetrics->setAttribute('coveredconditionals', 0); |
|
233 | + $xmlMetrics->setAttribute('statements', $report->getNumExecutableLines()); |
|
234 | + $xmlMetrics->setAttribute('coveredstatements', $report->getNumExecutedLines()); |
|
235 | + $xmlMetrics->setAttribute('elements', $report->getNumMethods() + $report->getNumExecutableLines() /* + conditionals */); |
|
236 | + $xmlMetrics->setAttribute('coveredelements', $report->getNumTestedMethods() + $report->getNumExecutedLines() /* + coveredconditionals */); |
|
237 | + $xmlProject->appendChild($xmlMetrics); |
|
238 | + |
|
239 | + $buffer = $xmlDocument->saveXML(); |
|
240 | + |
|
241 | + if ($target !== null) { |
|
242 | + if (!is_dir(dirname($target))) { |
|
243 | + mkdir(dirname($target), 0777, true); |
|
244 | + } |
|
245 | + |
|
246 | + file_put_contents($target, $buffer); |
|
247 | + } |
|
248 | + |
|
249 | + return $buffer; |
|
250 | + } |
|
251 | 251 | } |
@@ -16,157 +16,157 @@ |
||
16 | 16 | |
17 | 17 | class Crap4j |
18 | 18 | { |
19 | - /** |
|
20 | - * @var int |
|
21 | - */ |
|
22 | - private $threshold; |
|
23 | - |
|
24 | - /** |
|
25 | - * @param int $threshold |
|
26 | - */ |
|
27 | - public function __construct($threshold = 30) |
|
28 | - { |
|
29 | - if (!is_int($threshold)) { |
|
30 | - throw InvalidArgumentException::create( |
|
31 | - 1, |
|
32 | - 'integer' |
|
33 | - ); |
|
34 | - } |
|
35 | - |
|
36 | - $this->threshold = $threshold; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @param CodeCoverage $coverage |
|
41 | - * @param string $target |
|
42 | - * @param string $name |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function process(CodeCoverage $coverage, $target = null, $name = null) |
|
47 | - { |
|
48 | - $document = new \DOMDocument('1.0', 'UTF-8'); |
|
49 | - $document->formatOutput = true; |
|
50 | - |
|
51 | - $root = $document->createElement('crap_result'); |
|
52 | - $document->appendChild($root); |
|
53 | - |
|
54 | - $project = $document->createElement('project', is_string($name) ? $name : ''); |
|
55 | - $root->appendChild($project); |
|
56 | - $root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME']))); |
|
57 | - |
|
58 | - $stats = $document->createElement('stats'); |
|
59 | - $methodsNode = $document->createElement('methods'); |
|
60 | - |
|
61 | - $report = $coverage->getReport(); |
|
62 | - unset($coverage); |
|
63 | - |
|
64 | - $fullMethodCount = 0; |
|
65 | - $fullCrapMethodCount = 0; |
|
66 | - $fullCrapLoad = 0; |
|
67 | - $fullCrap = 0; |
|
68 | - |
|
69 | - foreach ($report as $item) { |
|
70 | - $namespace = 'global'; |
|
71 | - |
|
72 | - if (!$item instanceof File) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - |
|
76 | - $file = $document->createElement('file'); |
|
77 | - $file->setAttribute('name', $item->getPath()); |
|
78 | - |
|
79 | - $classes = $item->getClassesAndTraits(); |
|
80 | - |
|
81 | - foreach ($classes as $className => $class) { |
|
82 | - foreach ($class['methods'] as $methodName => $method) { |
|
83 | - $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']); |
|
84 | - |
|
85 | - $fullCrap += $method['crap']; |
|
86 | - $fullCrapLoad += $crapLoad; |
|
87 | - $fullMethodCount++; |
|
88 | - |
|
89 | - if ($method['crap'] >= $this->threshold) { |
|
90 | - $fullCrapMethodCount++; |
|
91 | - } |
|
92 | - |
|
93 | - $methodNode = $document->createElement('method'); |
|
94 | - |
|
95 | - if (!empty($class['package']['namespace'])) { |
|
96 | - $namespace = $class['package']['namespace']; |
|
97 | - } |
|
98 | - |
|
99 | - $methodNode->appendChild($document->createElement('package', $namespace)); |
|
100 | - $methodNode->appendChild($document->createElement('className', $className)); |
|
101 | - $methodNode->appendChild($document->createElement('methodName', $methodName)); |
|
102 | - $methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature']))); |
|
103 | - $methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature']))); |
|
104 | - $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap']))); |
|
105 | - $methodNode->appendChild($document->createElement('complexity', $method['ccn'])); |
|
106 | - $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage']))); |
|
107 | - $methodNode->appendChild($document->createElement('crapLoad', round($crapLoad))); |
|
108 | - |
|
109 | - $methodsNode->appendChild($methodNode); |
|
110 | - } |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - $stats->appendChild($document->createElement('name', 'Method Crap Stats')); |
|
115 | - $stats->appendChild($document->createElement('methodCount', $fullMethodCount)); |
|
116 | - $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount)); |
|
117 | - $stats->appendChild($document->createElement('crapLoad', round($fullCrapLoad))); |
|
118 | - $stats->appendChild($document->createElement('totalCrap', $fullCrap)); |
|
119 | - |
|
120 | - if ($fullMethodCount > 0) { |
|
121 | - $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount); |
|
122 | - } else { |
|
123 | - $crapMethodPercent = 0; |
|
124 | - } |
|
125 | - |
|
126 | - $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent)); |
|
127 | - |
|
128 | - $root->appendChild($stats); |
|
129 | - $root->appendChild($methodsNode); |
|
130 | - |
|
131 | - $buffer = $document->saveXML(); |
|
132 | - |
|
133 | - if ($target !== null) { |
|
134 | - if (!is_dir(dirname($target))) { |
|
135 | - mkdir(dirname($target), 0777, true); |
|
136 | - } |
|
137 | - |
|
138 | - file_put_contents($target, $buffer); |
|
139 | - } |
|
140 | - |
|
141 | - return $buffer; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param float $crapValue |
|
146 | - * @param int $cyclomaticComplexity |
|
147 | - * @param float $coveragePercent |
|
148 | - * |
|
149 | - * @return float |
|
150 | - */ |
|
151 | - private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent) |
|
152 | - { |
|
153 | - $crapLoad = 0; |
|
154 | - |
|
155 | - if ($crapValue >= $this->threshold) { |
|
156 | - $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100); |
|
157 | - $crapLoad += $cyclomaticComplexity / $this->threshold; |
|
158 | - } |
|
159 | - |
|
160 | - return $crapLoad; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param float $value |
|
165 | - * |
|
166 | - * @return float |
|
167 | - */ |
|
168 | - private function roundValue($value) |
|
169 | - { |
|
170 | - return round($value, 2); |
|
171 | - } |
|
19 | + /** |
|
20 | + * @var int |
|
21 | + */ |
|
22 | + private $threshold; |
|
23 | + |
|
24 | + /** |
|
25 | + * @param int $threshold |
|
26 | + */ |
|
27 | + public function __construct($threshold = 30) |
|
28 | + { |
|
29 | + if (!is_int($threshold)) { |
|
30 | + throw InvalidArgumentException::create( |
|
31 | + 1, |
|
32 | + 'integer' |
|
33 | + ); |
|
34 | + } |
|
35 | + |
|
36 | + $this->threshold = $threshold; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @param CodeCoverage $coverage |
|
41 | + * @param string $target |
|
42 | + * @param string $name |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function process(CodeCoverage $coverage, $target = null, $name = null) |
|
47 | + { |
|
48 | + $document = new \DOMDocument('1.0', 'UTF-8'); |
|
49 | + $document->formatOutput = true; |
|
50 | + |
|
51 | + $root = $document->createElement('crap_result'); |
|
52 | + $document->appendChild($root); |
|
53 | + |
|
54 | + $project = $document->createElement('project', is_string($name) ? $name : ''); |
|
55 | + $root->appendChild($project); |
|
56 | + $root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME']))); |
|
57 | + |
|
58 | + $stats = $document->createElement('stats'); |
|
59 | + $methodsNode = $document->createElement('methods'); |
|
60 | + |
|
61 | + $report = $coverage->getReport(); |
|
62 | + unset($coverage); |
|
63 | + |
|
64 | + $fullMethodCount = 0; |
|
65 | + $fullCrapMethodCount = 0; |
|
66 | + $fullCrapLoad = 0; |
|
67 | + $fullCrap = 0; |
|
68 | + |
|
69 | + foreach ($report as $item) { |
|
70 | + $namespace = 'global'; |
|
71 | + |
|
72 | + if (!$item instanceof File) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + |
|
76 | + $file = $document->createElement('file'); |
|
77 | + $file->setAttribute('name', $item->getPath()); |
|
78 | + |
|
79 | + $classes = $item->getClassesAndTraits(); |
|
80 | + |
|
81 | + foreach ($classes as $className => $class) { |
|
82 | + foreach ($class['methods'] as $methodName => $method) { |
|
83 | + $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']); |
|
84 | + |
|
85 | + $fullCrap += $method['crap']; |
|
86 | + $fullCrapLoad += $crapLoad; |
|
87 | + $fullMethodCount++; |
|
88 | + |
|
89 | + if ($method['crap'] >= $this->threshold) { |
|
90 | + $fullCrapMethodCount++; |
|
91 | + } |
|
92 | + |
|
93 | + $methodNode = $document->createElement('method'); |
|
94 | + |
|
95 | + if (!empty($class['package']['namespace'])) { |
|
96 | + $namespace = $class['package']['namespace']; |
|
97 | + } |
|
98 | + |
|
99 | + $methodNode->appendChild($document->createElement('package', $namespace)); |
|
100 | + $methodNode->appendChild($document->createElement('className', $className)); |
|
101 | + $methodNode->appendChild($document->createElement('methodName', $methodName)); |
|
102 | + $methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature']))); |
|
103 | + $methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature']))); |
|
104 | + $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap']))); |
|
105 | + $methodNode->appendChild($document->createElement('complexity', $method['ccn'])); |
|
106 | + $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage']))); |
|
107 | + $methodNode->appendChild($document->createElement('crapLoad', round($crapLoad))); |
|
108 | + |
|
109 | + $methodsNode->appendChild($methodNode); |
|
110 | + } |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + $stats->appendChild($document->createElement('name', 'Method Crap Stats')); |
|
115 | + $stats->appendChild($document->createElement('methodCount', $fullMethodCount)); |
|
116 | + $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount)); |
|
117 | + $stats->appendChild($document->createElement('crapLoad', round($fullCrapLoad))); |
|
118 | + $stats->appendChild($document->createElement('totalCrap', $fullCrap)); |
|
119 | + |
|
120 | + if ($fullMethodCount > 0) { |
|
121 | + $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount); |
|
122 | + } else { |
|
123 | + $crapMethodPercent = 0; |
|
124 | + } |
|
125 | + |
|
126 | + $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent)); |
|
127 | + |
|
128 | + $root->appendChild($stats); |
|
129 | + $root->appendChild($methodsNode); |
|
130 | + |
|
131 | + $buffer = $document->saveXML(); |
|
132 | + |
|
133 | + if ($target !== null) { |
|
134 | + if (!is_dir(dirname($target))) { |
|
135 | + mkdir(dirname($target), 0777, true); |
|
136 | + } |
|
137 | + |
|
138 | + file_put_contents($target, $buffer); |
|
139 | + } |
|
140 | + |
|
141 | + return $buffer; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param float $crapValue |
|
146 | + * @param int $cyclomaticComplexity |
|
147 | + * @param float $coveragePercent |
|
148 | + * |
|
149 | + * @return float |
|
150 | + */ |
|
151 | + private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent) |
|
152 | + { |
|
153 | + $crapLoad = 0; |
|
154 | + |
|
155 | + if ($crapValue >= $this->threshold) { |
|
156 | + $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100); |
|
157 | + $crapLoad += $cyclomaticComplexity / $this->threshold; |
|
158 | + } |
|
159 | + |
|
160 | + return $crapLoad; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param float $value |
|
165 | + * |
|
166 | + * @return float |
|
167 | + */ |
|
168 | + private function roundValue($value) |
|
169 | + { |
|
170 | + return round($value, 2); |
|
171 | + } |
|
172 | 172 | } |
@@ -12,77 +12,77 @@ |
||
12 | 12 | |
13 | 13 | class Node |
14 | 14 | { |
15 | - /** |
|
16 | - * @var \DOMDocument |
|
17 | - */ |
|
18 | - private $dom; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var \DOMElement |
|
22 | - */ |
|
23 | - private $contextNode; |
|
24 | - |
|
25 | - public function __construct(\DOMElement $context) |
|
26 | - { |
|
27 | - $this->setContextNode($context); |
|
28 | - } |
|
29 | - |
|
30 | - protected function setContextNode(\DOMElement $context) |
|
31 | - { |
|
32 | - $this->dom = $context->ownerDocument; |
|
33 | - $this->contextNode = $context; |
|
34 | - } |
|
35 | - |
|
36 | - public function getDom() |
|
37 | - { |
|
38 | - return $this->dom; |
|
39 | - } |
|
40 | - |
|
41 | - protected function getContextNode() |
|
42 | - { |
|
43 | - return $this->contextNode; |
|
44 | - } |
|
45 | - |
|
46 | - public function getTotals() |
|
47 | - { |
|
48 | - $totalsContainer = $this->getContextNode()->firstChild; |
|
49 | - |
|
50 | - if (!$totalsContainer) { |
|
51 | - $totalsContainer = $this->getContextNode()->appendChild( |
|
52 | - $this->dom->createElementNS( |
|
53 | - 'http://schema.phpunit.de/coverage/1.0', |
|
54 | - 'totals' |
|
55 | - ) |
|
56 | - ); |
|
57 | - } |
|
58 | - |
|
59 | - return new Totals($totalsContainer); |
|
60 | - } |
|
61 | - |
|
62 | - public function addDirectory($name) |
|
63 | - { |
|
64 | - $dirNode = $this->getDom()->createElementNS( |
|
65 | - 'http://schema.phpunit.de/coverage/1.0', |
|
66 | - 'directory' |
|
67 | - ); |
|
68 | - |
|
69 | - $dirNode->setAttribute('name', $name); |
|
70 | - $this->getContextNode()->appendChild($dirNode); |
|
71 | - |
|
72 | - return new Directory($dirNode); |
|
73 | - } |
|
74 | - |
|
75 | - public function addFile($name, $href) |
|
76 | - { |
|
77 | - $fileNode = $this->getDom()->createElementNS( |
|
78 | - 'http://schema.phpunit.de/coverage/1.0', |
|
79 | - 'file' |
|
80 | - ); |
|
81 | - |
|
82 | - $fileNode->setAttribute('name', $name); |
|
83 | - $fileNode->setAttribute('href', $href); |
|
84 | - $this->getContextNode()->appendChild($fileNode); |
|
85 | - |
|
86 | - return new File($fileNode); |
|
87 | - } |
|
15 | + /** |
|
16 | + * @var \DOMDocument |
|
17 | + */ |
|
18 | + private $dom; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var \DOMElement |
|
22 | + */ |
|
23 | + private $contextNode; |
|
24 | + |
|
25 | + public function __construct(\DOMElement $context) |
|
26 | + { |
|
27 | + $this->setContextNode($context); |
|
28 | + } |
|
29 | + |
|
30 | + protected function setContextNode(\DOMElement $context) |
|
31 | + { |
|
32 | + $this->dom = $context->ownerDocument; |
|
33 | + $this->contextNode = $context; |
|
34 | + } |
|
35 | + |
|
36 | + public function getDom() |
|
37 | + { |
|
38 | + return $this->dom; |
|
39 | + } |
|
40 | + |
|
41 | + protected function getContextNode() |
|
42 | + { |
|
43 | + return $this->contextNode; |
|
44 | + } |
|
45 | + |
|
46 | + public function getTotals() |
|
47 | + { |
|
48 | + $totalsContainer = $this->getContextNode()->firstChild; |
|
49 | + |
|
50 | + if (!$totalsContainer) { |
|
51 | + $totalsContainer = $this->getContextNode()->appendChild( |
|
52 | + $this->dom->createElementNS( |
|
53 | + 'http://schema.phpunit.de/coverage/1.0', |
|
54 | + 'totals' |
|
55 | + ) |
|
56 | + ); |
|
57 | + } |
|
58 | + |
|
59 | + return new Totals($totalsContainer); |
|
60 | + } |
|
61 | + |
|
62 | + public function addDirectory($name) |
|
63 | + { |
|
64 | + $dirNode = $this->getDom()->createElementNS( |
|
65 | + 'http://schema.phpunit.de/coverage/1.0', |
|
66 | + 'directory' |
|
67 | + ); |
|
68 | + |
|
69 | + $dirNode->setAttribute('name', $name); |
|
70 | + $this->getContextNode()->appendChild($dirNode); |
|
71 | + |
|
72 | + return new Directory($dirNode); |
|
73 | + } |
|
74 | + |
|
75 | + public function addFile($name, $href) |
|
76 | + { |
|
77 | + $fileNode = $this->getDom()->createElementNS( |
|
78 | + 'http://schema.phpunit.de/coverage/1.0', |
|
79 | + 'file' |
|
80 | + ); |
|
81 | + |
|
82 | + $fileNode->setAttribute('name', $name); |
|
83 | + $fileNode->setAttribute('href', $href); |
|
84 | + $this->getContextNode()->appendChild($fileNode); |
|
85 | + |
|
86 | + return new File($fileNode); |
|
87 | + } |
|
88 | 88 | } |