sebastianbergmann / php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.
BSD 3-Clause "New" or "Revised" License
8.82k stars 374 forks source link

Cobertura report pulls functions from report scope, not the individual element #1012

Closed SolovievEvgenii closed 1 year ago

SolovievEvgenii commented 1 year ago
Q A
php-code-coverage version 9.2.28
PHP version 7.4
Driver Xdebug
Xdebug version (if used) 3.1.6
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.6.12

We are working with one legacy PHP project that contains about 3000 classes and over 1000 global functions. We try to plug in the code coverage feature to our gitlab. When I tried to export the coverage report to Cobertura XML format, I received a Killed Error from Linux:

[19369437.006651] Out of memory: Kill process 27901 (php) score 680 or sacrifice child
[19369437.007441] Killed process 27901 (php) total-vm:22660172kB, anon-rss:22550668kB, file-rss:0kB, shmem-rss:0kB
[19369437.554951] oom_reaper: reaped process 27901 (php), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB

I simulated the situation by simple files (https://github.com/SolovievEvgenii/cobertura_bug1), debugged the Cobertura class and found that the nodes of all our global functions is included in the body of each package (which corresponds to a single file), although in reality the file may not have any relationship with global functions, no calls. no definitions. Look at the screenshot with resulted XML and the corresponded codes:

изображение

Pay attention at nodes, underlined by magenta lines, these nodes are talking about two different methods, but both are pointing to the same line. It is wrong, is it? It happens because at each package's iteration the list of functions is taken from global scope instead of list from package's functions

To get the fixed XML I added to your code my little crutch, I take a list of functions not from an iterating element "item", but from element from "children" array of the "report" object.

See the function with my try to fix here

At last I'he got the cobertura format of our big project without any killings. But I am not sure that it is really right, is it difficult to validate the report of large project. I suspect that my fix is not optimized way and hope to receive feedback about the correctness of this approach.

sebastianbergmann commented 1 year ago

Unfortunately, I do not use the Cobertura XML report myself and also have no experience with the format or its implementation in PHPUnit.

Maybe @smmccabe, @bastien-phi, @dvdoug, @jseniuk, or @tm1000, who have worked on this code in the past, can help.

tm1000 commented 1 year ago

I actually only used Cobertura for https://github.com/clearlyip/code-coverage-report-action (for support) but I use clover myself