Skip to content

Commit fcb91d9

Browse files
Refactor.
1 parent 5c93364 commit fcb91d9

File tree

4 files changed

+260
-251
lines changed

4 files changed

+260
-251
lines changed

PHP/CodeCoverage/Report/Factory.php

Lines changed: 179 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class PHP_CodeCoverage_Report_Factory
6363
public function create(PHP_CodeCoverage $coverage)
6464
{
6565
$files = $coverage->getData();
66-
$commonPath = PHP_CodeCoverage_Util::reducePaths($files);
66+
$commonPath = $this->reducePaths($files);
6767
$root = new PHP_CodeCoverage_Report_Node_Directory(
6868
$commonPath, NULL
6969
);
7070

7171
$this->addItems(
7272
$root,
73-
PHP_CodeCoverage_Util::buildDirectoryStructure($files),
73+
$this->buildDirectoryStructure($files),
7474
$coverage->getTests(),
7575
$coverage->getCacheTokens()
7676
);
@@ -96,4 +96,181 @@ protected function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array
9696
}
9797
}
9898
}
99+
100+
/**
101+
* Builds an array representation of the directory structure.
102+
*
103+
* For instance,
104+
*
105+
* <code>
106+
* Array
107+
* (
108+
* [Money.php] => Array
109+
* (
110+
* ...
111+
* )
112+
*
113+
* [MoneyBag.php] => Array
114+
* (
115+
* ...
116+
* )
117+
* )
118+
* </code>
119+
*
120+
* is transformed into
121+
*
122+
* <code>
123+
* Array
124+
* (
125+
* [.] => Array
126+
* (
127+
* [Money.php] => Array
128+
* (
129+
* ...
130+
* )
131+
*
132+
* [MoneyBag.php] => Array
133+
* (
134+
* ...
135+
* )
136+
* )
137+
* )
138+
* </code>
139+
*
140+
* @param array $files
141+
* @return array
142+
* @todo Need to be made protected.
143+
*/
144+
public function buildDirectoryStructure($files)
145+
{
146+
$result = array();
147+
148+
foreach ($files as $path => $file) {
149+
$path = explode('/', $path);
150+
$pointer = &$result;
151+
$max = count($path);
152+
153+
for ($i = 0; $i < $max; $i++) {
154+
if ($i == ($max - 1)) {
155+
$type = '/f';
156+
} else {
157+
$type = '';
158+
}
159+
160+
$pointer = &$pointer[$path[$i] . $type];
161+
}
162+
163+
$pointer = $file;
164+
}
165+
166+
return $result;
167+
}
168+
169+
/**
170+
* Reduces the paths by cutting the longest common start path.
171+
*
172+
* For instance,
173+
*
174+
* <code>
175+
* Array
176+
* (
177+
* [/home/sb/Money/Money.php] => Array
178+
* (
179+
* ...
180+
* )
181+
*
182+
* [/home/sb/Money/MoneyBag.php] => Array
183+
* (
184+
* ...
185+
* )
186+
* )
187+
* </code>
188+
*
189+
* is reduced to
190+
*
191+
* <code>
192+
* Array
193+
* (
194+
* [Money.php] => Array
195+
* (
196+
* ...
197+
* )
198+
*
199+
* [MoneyBag.php] => Array
200+
* (
201+
* ...
202+
* )
203+
* )
204+
* </code>
205+
*
206+
* @param array $files
207+
* @return string
208+
* @todo Need to be made protected.
209+
*/
210+
public function reducePaths(&$files)
211+
{
212+
if (empty($files)) {
213+
return '.';
214+
}
215+
216+
$commonPath = '';
217+
$paths = array_keys($files);
218+
219+
if (count($files) == 1) {
220+
$commonPath = dirname($paths[0]) . '/';
221+
$files[basename($paths[0])] = $files[$paths[0]];
222+
223+
unset($files[$paths[0]]);
224+
225+
return $commonPath;
226+
}
227+
228+
$max = count($paths);
229+
230+
for ($i = 0; $i < $max; $i++) {
231+
$paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
232+
233+
if (empty($paths[$i][0])) {
234+
$paths[$i][0] = DIRECTORY_SEPARATOR;
235+
}
236+
}
237+
238+
$done = FALSE;
239+
$max = count($paths);
240+
241+
while (!$done) {
242+
for ($i = 0; $i < $max - 1; $i++) {
243+
if (!isset($paths[$i][0]) ||
244+
!isset($paths[$i+1][0]) ||
245+
$paths[$i][0] != $paths[$i+1][0]) {
246+
$done = TRUE;
247+
break;
248+
}
249+
}
250+
251+
if (!$done) {
252+
$commonPath .= $paths[0][0];
253+
254+
if ($paths[0][0] != DIRECTORY_SEPARATOR) {
255+
$commonPath .= DIRECTORY_SEPARATOR;
256+
}
257+
258+
for ($i = 0; $i < $max; $i++) {
259+
array_shift($paths[$i]);
260+
}
261+
}
262+
}
263+
264+
$original = array_keys($files);
265+
$max = count($original);
266+
267+
for ($i = 0; $i < $max; $i++) {
268+
$files[join('/', $paths[$i])] = $files[$original[$i]];
269+
unset($files[$original[$i]]);
270+
}
271+
272+
ksort($files);
273+
274+
return substr($commonPath, 0, -1);
275+
}
99276
}

PHP/CodeCoverage/Util.php

Lines changed: 0 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -92,74 +92,6 @@ class PHP_CodeCoverage_Util
9292
*/
9393
protected static $ids = array();
9494

95-
/**
96-
* Builds an array representation of the directory structure.
97-
*
98-
* For instance,
99-
*
100-
* <code>
101-
* Array
102-
* (
103-
* [Money.php] => Array
104-
* (
105-
* ...
106-
* )
107-
*
108-
* [MoneyBag.php] => Array
109-
* (
110-
* ...
111-
* )
112-
* )
113-
* </code>
114-
*
115-
* is transformed into
116-
*
117-
* <code>
118-
* Array
119-
* (
120-
* [.] => Array
121-
* (
122-
* [Money.php] => Array
123-
* (
124-
* ...
125-
* )
126-
*
127-
* [MoneyBag.php] => Array
128-
* (
129-
* ...
130-
* )
131-
* )
132-
* )
133-
* </code>
134-
*
135-
* @param array $files
136-
* @return array
137-
*/
138-
public static function buildDirectoryStructure($files)
139-
{
140-
$result = array();
141-
142-
foreach ($files as $path => $file) {
143-
$path = explode('/', $path);
144-
$pointer = &$result;
145-
$max = count($path);
146-
147-
for ($i = 0; $i < $max; $i++) {
148-
if ($i == ($max - 1)) {
149-
$type = '/f';
150-
} else {
151-
$type = '';
152-
}
153-
154-
$pointer = &$pointer[$path[$i] . $type];
155-
}
156-
157-
$pointer = $file;
158-
}
159-
160-
return $result;
161-
}
162-
16395
/**
16496
* Calculates the Change Risk Anti-Patterns (CRAP) index for a unit of code
16597
* based on its cyclomatic complexity and percentage of code coverage.
@@ -372,113 +304,6 @@ public static function percent($a, $b, $asString = FALSE)
372304
}
373305
}
374306

375-
/**
376-
* Reduces the paths by cutting the longest common start path.
377-
*
378-
* For instance,
379-
*
380-
* <code>
381-
* Array
382-
* (
383-
* [/home/sb/Money/Money.php] => Array
384-
* (
385-
* ...
386-
* )
387-
*
388-
* [/home/sb/Money/MoneyBag.php] => Array
389-
* (
390-
* ...
391-
* )
392-
* )
393-
* </code>
394-
*
395-
* is reduced to
396-
*
397-
* <code>
398-
* Array
399-
* (
400-
* [Money.php] => Array
401-
* (
402-
* ...
403-
* )
404-
*
405-
* [MoneyBag.php] => Array
406-
* (
407-
* ...
408-
* )
409-
* )
410-
* </code>
411-
*
412-
* @param array $files
413-
* @return string
414-
*/
415-
public static function reducePaths(&$files)
416-
{
417-
if (empty($files)) {
418-
return '.';
419-
}
420-
421-
$commonPath = '';
422-
$paths = array_keys($files);
423-
424-
if (count($files) == 1) {
425-
$commonPath = dirname($paths[0]) . '/';
426-
$files[basename($paths[0])] = $files[$paths[0]];
427-
428-
unset($files[$paths[0]]);
429-
430-
return $commonPath;
431-
}
432-
433-
$max = count($paths);
434-
435-
for ($i = 0; $i < $max; $i++) {
436-
$paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
437-
438-
if (empty($paths[$i][0])) {
439-
$paths[$i][0] = DIRECTORY_SEPARATOR;
440-
}
441-
}
442-
443-
$done = FALSE;
444-
$max = count($paths);
445-
446-
while (!$done) {
447-
for ($i = 0; $i < $max - 1; $i++) {
448-
if (!isset($paths[$i][0]) ||
449-
!isset($paths[$i+1][0]) ||
450-
$paths[$i][0] != $paths[$i+1][0]) {
451-
$done = TRUE;
452-
break;
453-
}
454-
}
455-
456-
if (!$done) {
457-
$commonPath .= $paths[0][0];
458-
459-
if ($paths[0][0] != DIRECTORY_SEPARATOR) {
460-
$commonPath .= DIRECTORY_SEPARATOR;
461-
}
462-
463-
for ($i = 0; $i < $max; $i++) {
464-
array_shift($paths[$i]);
465-
}
466-
}
467-
}
468-
469-
$original = array_keys($files);
470-
$max = count($original);
471-
472-
for ($i = 0; $i < $max; $i++) {
473-
$files[join('/', $paths[$i])] = $files[$original[$i]];
474-
unset($files[$original[$i]]);
475-
}
476-
477-
ksort($files);
478-
479-
return substr($commonPath, 0, -1);
480-
}
481-
482307
/**
483308
* @param string $coveredElement
484309
* @return array

0 commit comments

Comments
 (0)