|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Hal\Report\OpenMetrics; |
| 5 | + |
| 6 | +use Hal\Application\Config\ConfigBagInterface; |
| 7 | +use Hal\Component\File\WriterInterface; |
| 8 | +use Hal\Exception\NotInstalledOpenMetricsPhpExpositionTextException; |
| 9 | +use Hal\Exception\NotWritableOpenMetricsReportException; |
| 10 | +use Hal\Report\SummaryProvider; |
| 11 | +use OpenMetricsPhp\Exposition\Text\Collections\GaugeCollection; |
| 12 | +use OpenMetricsPhp\Exposition\Text\Metrics\Gauge; |
| 13 | +use OpenMetricsPhp\Exposition\Text\Types\MetricName; |
| 14 | + |
| 15 | +/** |
| 16 | + * Dedicated writer that defines the content to write in a file when exporting the OpenMetrics summary of the metrics. |
| 17 | + */ |
| 18 | +final class SummaryWriter extends SummaryProvider |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + ConfigBagInterface $config, |
| 22 | + private readonly WriterInterface $fileWriter |
| 23 | + ) { |
| 24 | + parent::__construct($config); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Return the report of the summary, as a string for the OpenMetrics report format. |
| 29 | + * |
| 30 | + * @return string |
| 31 | + */ |
| 32 | + public function getReport(): string |
| 33 | + { |
| 34 | + if (!class_exists(GaugeCollection::class)) { |
| 35 | + throw NotInstalledOpenMetricsPhpExpositionTextException::notInstalled(); |
| 36 | + } |
| 37 | + |
| 38 | + $reportMetrics = [ |
| 39 | + // LOC |
| 40 | + 'lines_of_code' => [$this->sum->loc, 'Lines of code'], |
| 41 | + 'logical_lines_of_code' => [$this->sum->lloc, 'Logical lines of code'], |
| 42 | + 'comment_lines_of_code' => [$this->sum->cloc, 'Comment lines of code'], |
| 43 | + 'average_volume' => [$this->avg->volume, 'Average volume'], |
| 44 | + 'average_comment_weight' => [$this->avg->commentWeight, 'Average comment weight'], |
| 45 | + 'average_intelligent_content' => [$this->avg->intelligentContent, 'Average intelligent content'], |
| 46 | + 'loc_by_class' => [$this->locByClass, 'Logical lines of code by class'], |
| 47 | + 'loc_by_method' => [$this->locByMethod, 'Logical lines of code by method'], |
| 48 | + // Object oriented programming |
| 49 | + 'number_of_classes' => [$this->sum->nbClasses, 'Number of classes'], |
| 50 | + 'number_of_interfaces' => [$this->sum->nbInterfaces, 'Number of interfaces'], |
| 51 | + 'number_of_methods' => [$this->sum->nbMethods, 'Number of methods'], |
| 52 | + 'methods_by_class' => [$this->methodsByClass, 'Methods by class'], |
| 53 | + 'lack_cohesion_methods' => [$this->avg->lcom, 'Lack of cohesion of methods'], |
| 54 | + // Coupling |
| 55 | + 'afferent_coupling' => [$this->avg->afferentCoupling, 'Average afferent coupling'], |
| 56 | + 'efferent_coupling' => [$this->avg->efferentCoupling, 'Average efferent coupling'], |
| 57 | + 'instability' => [$this->avg->instability, 'Average instability'], |
| 58 | + 'tree_inheritance_depth' => [$this->treeInheritanceDepth, 'Depth of inheritance tree'], |
| 59 | + // Package |
| 60 | + 'number_of_packages' => [$this->sum->nbPackages, 'Number of packages'], |
| 61 | + 'classes_by_package' => [$this->avg->classesPerPackage, 'Average classes by package'], |
| 62 | + 'distance' => [$this->avg->distance, 'Average distance'], |
| 63 | + 'incoming_class_dependencies' => [$this->avg->incomingCDep, 'Average incoming class dependencies'], |
| 64 | + 'outgoing_class_dependencies' => [$this->avg->outgoingCDep, 'Average outgoing class dependencies'], |
| 65 | + 'incoming_package_dependencies' => [$this->avg->incomingPDep, 'Average incoming package dependencies'], |
| 66 | + 'outgoing_package_dependencies' => [$this->avg->outgoingPDep, 'Average outgoing package dependencies'], |
| 67 | + // Complexity |
| 68 | + 'cyclomatic_complexity_by_class' => [$this->avg->ccn, 'Average cyclomatic complexity by class'], |
| 69 | + 'weighted_method_count_by_class' => [$this->avg->wmc, 'Average weighted method count by class'], |
| 70 | + 'relative_system_complexity' => [ |
| 71 | + $this->avg->relativeSystemComplexity, |
| 72 | + 'Average relative system complexity' |
| 73 | + ], |
| 74 | + 'difficulty' => [$this->avg->difficulty, 'Average difficulty'], |
| 75 | + // Bugs |
| 76 | + 'bugs_by_class' => [$this->avg->bugs, 'Average bugs by class'], |
| 77 | + 'defects_by_class' => [$this->avg->kanDefect, 'Average defects by class (Kan)'], |
| 78 | + // Violations |
| 79 | + 'critical_violations' => [$this->sum->violations->critical, 'Critical violations'], |
| 80 | + 'error_violations' => [$this->sum->violations->error, 'Error violations'], |
| 81 | + 'warning_violations' => [$this->sum->violations->warning, 'Warning violations'], |
| 82 | + 'information_violations' => [$this->sum->violations->information, 'Information violations'] |
| 83 | + ]; |
| 84 | + |
| 85 | + $reportStrings = []; |
| 86 | + |
| 87 | + foreach ($reportMetrics as $metricName => $reportMetric) { |
| 88 | + $reportStrings[] = GaugeCollection::fromGauges( |
| 89 | + MetricName::fromString($metricName), |
| 90 | + Gauge::fromValue($reportMetric[0]) |
| 91 | + )->withHelp($reportMetric[1])->getMetricsString(); |
| 92 | + } |
| 93 | + |
| 94 | + return implode("\n", $reportStrings) . "\n# EOF\n"; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritDoc} |
| 99 | + */ |
| 100 | + public function getReportFile(): string|false |
| 101 | + { |
| 102 | + if ($this->config->has('quiet')) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /** @var null|string $logFile */ |
| 107 | + $logFile = $this->config->get('report-openmetrics'); |
| 108 | + if (null === $logFile) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + if (!$this->fileWriter->exists(dirname($logFile)) || !$this->fileWriter->isWritable(dirname($logFile))) { |
| 113 | + throw NotWritableOpenMetricsReportException::noPermission($logFile); |
| 114 | + } |
| 115 | + |
| 116 | + return $logFile; |
| 117 | + } |
| 118 | +} |
0 commit comments