Repository navigation
Description
This library uses PHP-Parser to statically analyse every file that is covered: executable lines, code units, ignored lines, dead code, complexity, and size metrics. When PHP-Parser cannot parse a file, ParsingSourceAnalyser wraps the parse error in a ParserException.
This exception is not recoverable for the consumer. In PHPUnit, static analysis runs as part of CodeCoverage::append(), so every test whose coverage touches the unparseable file errors with "Cannot parse ...", and report generation fails for every configured format as well. A single unparseable file effectively disables code coverage for the whole test run.
This is about to become a practical problem (again): PHP 8.6 introduces partial function application, and until PHP-Parser supports that syntax, any covered file that uses it cannot be parsed. The same situation will recur with every future PHP version whose syntax additions land before PHP-Parser support does.
Proposal
Treat a parse failure as a per-file degradation instead of a fatal error:
ParsingSourceAnalysercatches the parser error and returns anAnalysisResultthat is marked as "could not be parsed" and carries the parser's error message. The degraded result has no code units, no executable/dead/branch-operator lines, and line counts without the comment/non-comment split. Lines ignored using line-based annotations (@codeCoverageIgnorecomments) are still honored, because that scan is based ontoken_get_all()and does not require the parser.FilterProcessor::applyExecutableLinesFilter()skips files whose analysis is degraded, leaving the raw data reported by the driver untouched. Line coverage for such a file is then based on the driver's notion of executable lines, which is what php-code-coverage did for all files before static analysis was introduced.- The degradation must not be silent:
CodeCoverage::parseErrors()exposes the affected files together with the parser's error message so that a test runner can warn about them once per run. CachingSourceAnalyserdoes not write degraded results to the static analysis cache. The cache key does not include the installed PHP-Parser version, so a cached degraded result would outlive a PHP-Parser upgrade that adds support for the syntax in question.
A file that PHP-Parser cannot parse gets approximate, driver-based line coverage and appears in reports without class/method/complexity details, accompanied by a warning. All other files are unaffected. Tests no longer error and reports are still generated.
Activity