<commit_before><?php
namespace Scheb\Tombstone\Analyzer\Report\Html\Renderer;

use Scheb\Tombstone\Analyzer\AnalyzerFileResult;
use Scheb\Tombstone\Analyzer\AnalyzerResult;
use Scheb\Tombstone\Analyzer\Cli\Application;
use Scheb\Tombstone\Analyzer\Report\Html\TemplateFactory;
use Scheb\Tombstone\Analyzer\Report\ReportGeneratorInterface;
use Scheb\Tombstone\Tombstone;
use Scheb\Tombstone\Tracing\PathNormalizer;

class FileRenderer implements ReportGeneratorInterface
{
    /**
     * @var string
     */
    private $reportDir;

    /**
     * @var string
     */
    private $sourceDir;

    /**
     * @var \Text_Template
     */
    private $fileTemplate;

    /**
     * @var \Text_Template
     */
    private $tombstoneTemplate;

    /**
     * @param string $reportDir
     * @param string $sourceDir
     */
    public function __construct($reportDir, $sourceDir)
    {
        $this->reportDir = $reportDir;
        $this->sourceDir = $sourceDir;
        $this->fileTemplate = TemplateFactory::getTemplate('file.html');
        $this->tombstoneTemplate = TemplateFactory::getTemplate('file_tombstone.html');
    }

    /**
     * @param AnalyzerResult $result
     */
    public function generate(AnalyzerResult $result)
    {
        foreach ($result->getPerFile() as $file => $fileResult) {
            if ($fileResult->getDeadCount() || $fileResult->getUndeadCount()) {
                $this->renderFile($fileResult);
            }
        }
    }

    /**
     * @param AnalyzerFileResult $fileResult
     */
    private function renderFile(AnalyzerFileResult $fileResult) {
        $tombstonesList = $this->renderTombstonesList($fileResult);
        $sourceCode = $this->formatSourceCode($fileResult);
        $relativeFilePath = PathNormalizer::makeRelativeTo($fileResult->getFile(), $this->sourceDir);
        $this->fileTemplate->setVar(array(
            'path_to_root' => './' . str_repeat('../', substr_count($relativeFilePath, '/')),
            'full_path' => $fileResult->getFile(),
            'breadcrumb' => $this->renderBreadcrumb($relativeFilePath),
            'tombstones_list' => $tombstonesList,
            'source_code' => $sourceCode,
            'date' => date('r'),
            'version' => Application::VERSION,
        ));

        $this->fileTemplate->renderTo($this->reportDir . DIRECTORY_SEPARATOR . $relativeFilePath . '.html');
    }

    /**
     * @param AnalyzerFileResult $fileResult
     *
     * @return string
     */
    private function renderTombstonesList(AnalyzerFileResult $fileResult)
    {<commit_msg>Create directory when necessary<commit_after><?php
namespace Scheb\Tombstone\Analyzer\Report\Html\Renderer;

use Scheb\Tombstone\Analyzer\AnalyzerFileResult;
use Scheb\Tombstone\Analyzer\AnalyzerResult;
use Scheb\Tombstone\Analyzer\Cli\Application;
use Scheb\Tombstone\Analyzer\Report\Html\TemplateFactory;
use Scheb\Tombstone\Analyzer\Report\ReportGeneratorInterface;
use Scheb\Tombstone\Tombstone;
use Scheb\Tombstone\Tracing\PathNormalizer;

class FileRenderer implements ReportGeneratorInterface
{
    /**
     * @var string
     */
    private $reportDir;

    /**
     * @var string
     */
    private $sourceDir;

    /**
     * @var \Text_Template
     */
    private $fileTemplate;

    /**
     * @var \Text_Template
     */
    private $tombstoneTemplate;

    /**
     * @param string $reportDir
     * @param string $sourceDir
     */
    public function __construct($reportDir, $sourceDir)
    {
        $this->reportDir = $reportDir;
        $this->sourceDir = $sourceDir;
        $this->fileTemplate = TemplateFactory::getTemplate('file.html');
        $this->tombstoneTemplate = TemplateFactory::getTemplate('file_tombstone.html');
    }

    /**
     * @param AnalyzerResult $result
     */
    public function generate(AnalyzerResult $result)
    {
        foreach ($result->getPerFile() as $file => $fileResult) {
            if ($fileResult->getDeadCount() || $fileResult->getUndeadCount()) {
                $this->renderFile($fileResult);
            }
        }
    }

    /**
     * @param AnalyzerFileResult $fileResult
     */
    private function renderFile(AnalyzerFileResult $fileResult) {
        $tombstonesList = $this->renderTombstonesList($fileResult);
        $sourceCode = $this->formatSourceCode($fileResult);
        $relativeFilePath = PathNormalizer::makeRelativeTo($fileResult->getFile(), $this->sourceDir);
        $this->fileTemplate->setVar(array(
            'path_to_root' => './' . str_repeat('../', substr_count($relativeFilePath, '/')),
            'full_path' => $fileResult->getFile(),
            'breadcrumb' => $this->renderBreadcrumb($relativeFilePath),
            'tombstones_list' => $tombstonesList,
            'source_code' => $sourceCode,
            'date' => date('r'),
            'version' => Application::VERSION,
        ));

        $reportFile = $this->reportDir . DIRECTORY_SEPARATOR . $relativeFilePath . '.html';
        $reportDir = dirname($reportFile);
        if (!is_dir($reportDir)) {
            mkdir($reportDir, 0777, true);
        }
        $this->fileTemplate->renderTo($reportFile);
    }

    /**
     * @param AnalyzerFileResult $fileResult
     *
     * @return string
     */
    private function renderTombstonesList(AnalyzerFileResult $fileResult)
    {
{'fraction_non_alphanumeric': 0.13559322033898305, 'fraction_numerical': 0.0007532956685499058, 'mean_word_length': 2.807168458781362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '661522', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1544, 'n_words': 240}