@@ -92,6 +92,13 @@ class PHP_CodeCoverage
9292 */
9393 protected $ currentId ;
9494
95+ /**
96+ * SHA1 checksum of covered files.
97+ *
98+ * @var array
99+ */
100+ protected $ checksums = array ();
101+
95102 /**
96103 * Code coverage data.
97104 *
@@ -298,9 +305,16 @@ public function append(array $data, $id = NULL)
298305 * Merges the data from another instance of PHP_CodeCoverage.
299306 *
300307 * @param PHP_CodeCoverage $that
308+ * @param boolean $matchPaths
301309 */
302- public function merge (PHP_CodeCoverage $ that )
310+ public function merge (PHP_CodeCoverage $ that, $ matchPaths = FALSE )
303311 {
312+ if ($ matchPaths ) {
313+ $ thatData = $ this ->matchPaths ($ that ->checksums , $ that ->data );
314+ } else {
315+ $ thatData = $ that ->data ;
316+ }
317+
304318 foreach ($ that ->data as $ file => $ lines ) {
305319 if (!isset ($ this ->data [$ file ])) {
306320 if (!$ this ->filter ->isFiltered ($ file )) {
@@ -446,7 +460,8 @@ protected function initializeFilesThatAreSeenTheFirstTime($data)
446460 {
447461 foreach ($ data as $ file => $ lines ) {
448462 if (!isset ($ this ->data [$ file ])) {
449- $ this ->data [$ file ] = array ();
463+ $ this ->checksums [$ file ] = sha1_file ($ file );
464+ $ this ->data [$ file ] = array ();
450465
451466 foreach ($ lines as $ k => $ v ) {
452467 $ this ->data [$ file ][$ k ] = $ v == -2 ? NULL : array ();
@@ -523,4 +538,37 @@ protected function processUncoveredFilesFromWhitelist()
523538
524539 $ this ->append ($ data , 'UNCOVERED_FILES_FROM_WHITELIST ' );
525540 }
541+
542+ /**
543+ * @param array $checksums
544+ * @param array $data
545+ * @return array
546+ * @since Method available since Release 1.1.0
547+ */
548+ protected function matchPaths (array $ checksums , array $ data )
549+ {
550+ $ coverageWithLocalPaths = array ();
551+
552+ foreach ($ data as $ originalRemotePath => $ coverage ) {
553+ $ remotePath = $ originalRemotePath ;
554+
555+ if (strpos ($ path , '/ ' ) !== FALSE ) {
556+ $ separator = '/ ' ;
557+ } else {
558+ $ separator = '\\' ;
559+ }
560+
561+ while (!($ localPath = PHPUnit_Util_Filesystem::fileExistsInIncludePath ($ remotePath )) &&
562+ strpos ($ remotePath , $ separator ) !== FALSE ) {
563+ $ remotePath = substr ($ remotePath , strpos ($ remotePath , $ separator ) + 1 );
564+ }
565+
566+ if ($ localPath &&
567+ sha1_file ($ localPath ) == $ checksums [$ originalRemotePath ]) {
568+ $ coverageWithLocalPaths [$ localPath ] = $ coverage ;
569+ }
570+ }
571+
572+ return $ coverageWithLocalPaths ;
573+ }
526574}
0 commit comments