class Facade
Same name in this branch
- 11.1.x vendor/phpunit/php-code-coverage/src/Report/Html/Facade.php \SebastianBergmann\CodeCoverage\Report\Html\Facade
- 11.1.x vendor/phpunit/phpunit/src/TextUI/Output/Facade.php \PHPUnit\TextUI\Output\Facade
- 11.1.x vendor/phpunit/phpunit/src/Event/Facade.php \PHPUnit\Event\Facade
- 11.1.x vendor/phpunit/phpunit/src/Runner/Extension/Facade.php \PHPUnit\Runner\Extension\Facade
- 11.1.x vendor/phpunit/phpunit/src/Runner/TestResult/Facade.php \PHPUnit\TestRunner\TestResult\Facade
- 11.1.x vendor/phpunit/php-file-iterator/src/Facade.php \SebastianBergmann\FileIterator\Facade
Hierarchy
- class \SebastianBergmann\CodeCoverage\Report\Xml\Facade
Expanded class hierarchy of Facade
1 file declares its use of Facade
- CodeCoverage.php in vendor/
phpunit/ phpunit/ src/ Runner/ CodeCoverage.php
File
-
vendor/
phpunit/ php-code-coverage/ src/ Report/ Xml/ Facade.php, line 41
Namespace
SebastianBergmann\CodeCoverage\Report\XmlView source
final class Facade {
private string $target;
private Project $project;
private readonly string $phpUnitVersion;
public function __construct(string $version) {
$this->phpUnitVersion = $version;
}
/**
* @throws XmlException
*/
public function process(CodeCoverage $coverage, string $target) : void {
if (substr($target, -1, 1) !== DIRECTORY_SEPARATOR) {
$target .= DIRECTORY_SEPARATOR;
}
$this->target = $target;
$this->initTargetDirectory($target);
$report = $coverage->getReport();
$this->project = new Project($coverage->getReport()
->name());
$this->setBuildInformation();
$this->processTests($coverage->getTests());
$this->processDirectory($report, $this->project);
$this->saveDocument($this->project
->asDom(), 'index');
}
private function setBuildInformation() : void {
$buildNode = $this->project
->buildInformation();
$buildNode->setRuntimeInformation(new Runtime());
$buildNode->setBuildTime(new DateTimeImmutable());
$buildNode->setGeneratorVersions($this->phpUnitVersion, Version::id());
}
/**
* @throws PathExistsButIsNotDirectoryException
* @throws WriteOperationFailedException
*/
private function initTargetDirectory(string $directory) : void {
if (is_file($directory)) {
if (!is_dir($directory)) {
throw new PathExistsButIsNotDirectoryException($directory);
}
if (!is_writable($directory)) {
throw new WriteOperationFailedException($directory);
}
}
DirectoryUtil::createDirectory($directory);
}
/**
* @throws XmlException
*/
private function processDirectory(DirectoryNode $directory, Node $context) : void {
$directoryName = $directory->name();
if ($this->project
->projectSourceDirectory() === $directoryName) {
$directoryName = '/';
}
$directoryObject = $context->addDirectory($directoryName);
$this->setTotals($directory, $directoryObject->totals());
foreach ($directory->directories() as $node) {
$this->processDirectory($node, $directoryObject);
}
foreach ($directory->files() as $node) {
$this->processFile($node, $directoryObject);
}
}
/**
* @throws XmlException
*/
private function processFile(FileNode $file, Directory $context) : void {
$fileObject = $context->addFile($file->name(), $file->id() . '.xml');
$this->setTotals($file, $fileObject->totals());
$path = substr($file->pathAsString(), strlen($this->project
->projectSourceDirectory()));
$fileReport = new Report($path);
$this->setTotals($file, $fileReport->totals());
foreach ($file->classesAndTraits() as $unit) {
$this->processUnit($unit, $fileReport);
}
foreach ($file->functions() as $function) {
$this->processFunction($function, $fileReport);
}
foreach ($file->lineCoverageData() as $line => $tests) {
if (!is_array($tests) || count($tests) === 0) {
continue;
}
$coverage = $fileReport->lineCoverage((string) $line);
foreach ($tests as $test) {
$coverage->addTest($test);
}
$coverage->finalize();
}
$fileReport->source()
->setSourceCode(file_get_contents($file->pathAsString()));
$this->saveDocument($fileReport->asDom(), $file->id());
}
private function processUnit(array $unit, Report $report) : void {
if (isset($unit['className'])) {
$unitObject = $report->classObject($unit['className']);
}
else {
$unitObject = $report->traitObject($unit['traitName']);
}
$unitObject->setLines($unit['startLine'], $unit['executableLines'], $unit['executedLines']);
$unitObject->setCrap((double) $unit['crap']);
$unitObject->setNamespace($unit['namespace']);
foreach ($unit['methods'] as $method) {
$methodObject = $unitObject->addMethod($method['methodName']);
$methodObject->setSignature($method['signature']);
$methodObject->setLines((string) $method['startLine'], (string) $method['endLine']);
$methodObject->setCrap($method['crap']);
$methodObject->setTotals((string) $method['executableLines'], (string) $method['executedLines'], (string) $method['coverage']);
}
}
private function processFunction(array $function, Report $report) : void {
$functionObject = $report->functionObject($function['functionName']);
$functionObject->setSignature($function['signature']);
$functionObject->setLines((string) $function['startLine']);
$functionObject->setCrap($function['crap']);
$functionObject->setTotals((string) $function['executableLines'], (string) $function['executedLines'], (string) $function['coverage']);
}
private function processTests(array $tests) : void {
$testsObject = $this->project
->tests();
foreach ($tests as $test => $result) {
$testsObject->addTest($test, $result);
}
}
private function setTotals(AbstractNode $node, Totals $totals) : void {
$loc = $node->linesOfCode();
$totals->setNumLines($loc['linesOfCode'], $loc['commentLinesOfCode'], $loc['nonCommentLinesOfCode'], $node->numberOfExecutableLines(), $node->numberOfExecutedLines());
$totals->setNumClasses($node->numberOfClasses(), $node->numberOfTestedClasses());
$totals->setNumTraits($node->numberOfTraits(), $node->numberOfTestedTraits());
$totals->setNumMethods($node->numberOfMethods(), $node->numberOfTestedMethods());
$totals->setNumFunctions($node->numberOfFunctions(), $node->numberOfTestedFunctions());
}
private function targetDirectory() : string {
return $this->target;
}
/**
* @throws XmlException
*/
private function saveDocument(DOMDocument $document, string $name) : void {
$filename = sprintf('%s/%s.xml', $this->targetDirectory(), $name);
$document->formatOutput = true;
$document->preserveWhiteSpace = false;
$this->initTargetDirectory(dirname($filename));
file_put_contents($filename, $this->documentAsString($document));
}
/**
* @throws XmlException
*
* @see https://bugs.php.net/bug.php?id=79191
*/
private function documentAsString(DOMDocument $document) : string {
$xmlErrorHandling = libxml_use_internal_errors(true);
$xml = $document->saveXML();
if ($xml === false) {
$message = 'Unable to generate the XML';
foreach (libxml_get_errors() as $error) {
$message .= PHP_EOL . $error->message;
}
throw new XmlException($message);
}
libxml_clear_errors();
libxml_use_internal_errors($xmlErrorHandling);
return $xml;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Facade::$phpUnitVersion | private | property | |
Facade::$project | private | property | |
Facade::$target | private | property | |
Facade::documentAsString | private | function | |
Facade::initTargetDirectory | private | function | |
Facade::process | public | function | |
Facade::processDirectory | private | function | |
Facade::processFile | private | function | |
Facade::processFunction | private | function | |
Facade::processTests | private | function | |
Facade::processUnit | private | function | |
Facade::saveDocument | private | function | |
Facade::setBuildInformation | private | function | |
Facade::setTotals | private | function | |
Facade::targetDirectory | private | function | |
Facade::__construct | public | function |