<commit_before><?php

/*
 * (c) <NAME> <https://twitter.com/Halleck45>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Hal\Application\Formater\Summary;
use Hal\Application\Formater\FormaterInterface;
use Hal\Application\Formater\Twig\FormatingExtension;
use Hal\Application\Rule\Validator;
use Hal\Component\Bounds\BoundsInterface;
use Hal\Component\Result\ResultCollection;


/**
 * Formater for html export
 *
 * @author <NAME> <https://twitter.com/Halleck45>
 */
class Html implements FormaterInterface {

    /**
     * Bounds
     *
     * @var BoundsInterface
     */
    private $bound;

    /**
     * Validator
     *
     * @var Validator
     */
    private $validator;

    /**
     * Constructor
     *
     * @param Validator $validator
     * @param BoundsInterface $bound
     */
    public function __construct(Validator $validator, BoundsInterface $bound)
    {
        $this->bound = $bound;
        $this->validator = $validator;
    }

    /**
     * @inheritdoc
     */
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
        \Twig_Autoloader::register();
        $loader = new \Twig_Loader_Filesystem(__DIR__.'/../../../../../templates/html');
        $twig = new \Twig_Environment($loader, array('cache' => false));
        $twig->addExtension(new FormatingExtension($this->validator));

        $bound = $this->bound->calculate($collection);
        return $twig->render('summary/report.html.twig', array(
            'keys' => array_keys(current($collection->asArray()))
            , 'results' => $collection->asArray()
            , 'groupedResults' => $groupedResults
            , 'root' => current(current($groupedResults))
            , 'relations' => $this->prepareDataRelations($collection)
            , 'scores' => $collection->getScore()->all()
            , 'ruleSet' => $this->validator->getRuleSet()
            , 'bounds' => $bound
            , 'withOOP' => null !== $bound->getSum('instability')
        ));
    }

    /**
     * Build flat array of relations
     *
     * @param ResultCollection $collection
     * @return array
     */
    private function prepareDataRelations(ResultCollection $collection) {
        $array = array();

        // map of classes an relations
        foreach($collection as $item) {

            // case of oop is disabled
            if(!$item->getOOP()) {
                continue;
            }

            foreach($item->getOOP()->getClasses() as $class) {
                $array[$class->getName()] = (object) array(
                    'name' => $class->getName()
                    , 'size' => 3000
                    , 'relations' => array_merge(
                        !is_null($class->getParent()) ? array($class->getParent()) : array()
                        , $class->getDependencies()
                    )
                );
            }
        }

        // dependency can not be in the parsed sources (for example, native PHP classes)
        foreach($array as $class => $infos) {<commit_msg>Always use fqcn for class names. Otherwise most classes would exists two times within the dependency graph: once with short name and once with fqcn.<commit_after><?php

/*
 * (c) <NAME> <https://twitter.com/Halleck45>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Hal\Application\Formater\Summary;
use Hal\Application\Formater\FormaterInterface;
use Hal\Application\Formater\Twig\FormatingExtension;
use Hal\Application\Rule\Validator;
use Hal\Component\Bounds\BoundsInterface;
use Hal\Component\Result\ResultCollection;


/**
 * Formater for html export
 *
 * @author <NAME> <https://twitter.com/Halleck45>
 */
class Html implements FormaterInterface {

    /**
     * Bounds
     *
     * @var BoundsInterface
     */
    private $bound;

    /**
     * Validator
     *
     * @var Validator
     */
    private $validator;

    /**
     * Constructor
     *
     * @param Validator $validator
     * @param BoundsInterface $bound
     */
    public function __construct(Validator $validator, BoundsInterface $bound)
    {
        $this->bound = $bound;
        $this->validator = $validator;
    }

    /**
     * @inheritdoc
     */
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
        \Twig_Autoloader::register();
        $loader = new \Twig_Loader_Filesystem(__DIR__.'/../../../../../templates/html');
        $twig = new \Twig_Environment($loader, array('cache' => false));
        $twig->addExtension(new FormatingExtension($this->validator));

        $bound = $this->bound->calculate($collection);
        return $twig->render('summary/report.html.twig', array(
            'keys' => array_keys(current($collection->asArray()))
            , 'results' => $collection->asArray()
            , 'groupedResults' => $groupedResults
            , 'root' => current(current($groupedResults))
            , 'relations' => $this->prepareDataRelations($collection)
            , 'scores' => $collection->getScore()->all()
            , 'ruleSet' => $this->validator->getRuleSet()
            , 'bounds' => $bound
            , 'withOOP' => null !== $bound->getSum('instability')
        ));
    }

    /**
     * Build flat array of relations
     *
     * @param ResultCollection $collection
     * @return array
     */
    private function prepareDataRelations(ResultCollection $collection) {
        $array = array();

        // map of classes an relations
        foreach($collection as $item) {

            // case of oop is disabled
            if(!$item->getOOP()) {
                continue;
            }

            foreach($item->getOOP()->getClasses() as $class) {
                $array[$class->getFullname()] = (object) array(
                    'name' => $class->getFullname()
                    , 'size' => 3000
                    , 'relations' => array_merge(
                        !is_null($class->getParent()) ? array($class->getParent()) : array()
                        , $class->getDependencies()
                    )
                );
            }
        }

        // dependency can not be in the parsed sources (for example, native PHP classes)
        foreach($array as $class => $infos) {
{'fraction_non_alphanumeric': 0.13942981772861818, 'fraction_numerical': 0.0024926000934725034, 'mean_word_length': 2.1641202562838835, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 85, 'https://': 4, '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': '5003039', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1714, 'n_words': 382}