@@ -380,17 +380,23 @@ public function merge(self $that): void
380380 continue ;
381381 }
382382
383- foreach ($ lines as $ line => $ data ) {
384- if ($ data === null || (array_key_exists ($ line , $ this ->data [$ file ]) && $ this ->data [$ file ][$ line ] === null )) {
385- // if the line is marked as "dead code" in either, mark it as dead code in the merged result
386- $ this ->data [$ file ][$ line ] = null ;
387- } elseif (!isset ($ this ->data [$ file ][$ line ])) {
388- // if no data has been set in the current data, overwrite all
389- $ this ->data [$ file ][$ line ] = $ data ;
390- } else {
391- // otherwise merge data from both coverage files
383+ // we should compare the lines if any of two contains data
384+ $ compareLineNumbers = \array_unique (
385+ \array_merge (
386+ \array_keys ($ this ->data [$ file ]),
387+ \array_keys ($ that ->data [$ file ])
388+ )
389+ );
390+
391+ foreach ($ compareLineNumbers as $ line ) {
392+ $ thatPriority = $ this ->getLinePriority ($ that ->data [$ file ], $ line );
393+ $ thisPriority = $ this ->getLinePriority ($ this ->data [$ file ], $ line );
394+
395+ if ($ thatPriority > $ thisPriority ) {
396+ $ this ->data [$ file ][$ line ] = $ that ->data [$ file ][$ line ];
397+ } elseif ($ thatPriority === $ thisPriority && \is_array ($ this ->data [$ file ][$ line ])) {
392398 $ this ->data [$ file ][$ line ] = \array_unique (
393- \array_merge ($ this ->data [$ file ][$ line ], $ data )
399+ \array_merge ($ this ->data [$ file ][$ line ], $ that -> data [ $ file ][ $ line ] )
394400 );
395401 }
396402 }
@@ -400,6 +406,33 @@ public function merge(self $that): void
400406 $ this ->report = null ;
401407 }
402408
409+ /**
410+ * Determine the priority for a line
411+ *
412+ * 1 = the line is not set
413+ * 2 = the line has not been tested
414+ * 3 = the line is dead code
415+ * 4 = the line has been tested
416+ *
417+ * During a merge, a higher number is better.
418+ *
419+ * @param array $data
420+ * @param int $line
421+ * @return int
422+ */
423+ protected function getLinePriority ($ data , $ line )
424+ {
425+ if (!\array_key_exists ($ line , $ data )) {
426+ return 1 ;
427+ } elseif (\is_array ($ data [$ line ]) && \count ($ data [$ line ]) === 0 ) {
428+ return 2 ;
429+ } elseif ($ data [$ line ] === null ) {
430+ return 3 ;
431+ }
432+
433+ return 4 ;
434+ }
435+
403436 public function setCacheTokens (bool $ flag ): void
404437 {
405438 $ this ->cacheTokens = $ flag ;
0 commit comments