<commit_before><?php

declare(strict_types=1);

namespace GrumPHP\Task;

use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Deptrac task.
 */
class Deptrac extends AbstractExternalTask
{
    public function getName(): string
    {
        return 'deptrac';
    }

    public function getConfigurableOptions(): OptionsResolver
    {
        $resolver = new OptionsResolver();
        $resolver->setDefaults([
            'depfile' => null,
            'formatter_graphviz' => false,
            'formatter_graphviz_display' => false,
            'formatter_graphviz_dump_image' => null,
            'formatter_graphviz_dump_dot' => null,
            'formatter_graphviz_dump_html' => null,
        ]);

        $resolver->addAllowedTypes('depfile', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz', ['bool']);
        $resolver->addAllowedTypes('formatter_graphviz_display', ['bool']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_image', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_dot', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_html', ['null', 'string']);

        return $resolver;
    }

    public function canRunInContext(ContextInterface $context): bool
    {
        return $context instanceof GitPreCommitContext || $context instanceof RunContext;
    }

    public function run(ContextInterface $context): TaskResultInterface
    {
        $config = $this->getConfiguration();

        $files = $context->getFiles()->name('*.php');
        if (0 === \count($files)) {
            return TaskResult::createSkipped($this, $context);
        }

        $arguments = $this->processBuilder->createArgumentsForCommand('deptrac');
        $arguments->add('analyze');
        $arguments->add('--formatter-graphviz='.(int) $config['formatter_graphviz']);
        $arguments->addOptionalArgument('--formatter-graphviz-display=%s', $config['formatter_graphviz_display']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-image=%s', $config['formatter_graphviz_dump_image']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-dot=%s', $config['formatter_graphviz_dump_dot']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-html=%s', $config['formatter_graphviz_dump_html']);<commit_msg>Fix: do not display graphiz when argument is set false<commit_after><?php

declare(strict_types=1);

namespace GrumPHP\Task;

use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Deptrac task.
 */
class Deptrac extends AbstractExternalTask
{
    public function getName(): string
    {
        return 'deptrac';
    }

    public function getConfigurableOptions(): OptionsResolver
    {
        $resolver = new OptionsResolver();
        $resolver->setDefaults([
            'depfile' => null,
            'formatter_graphviz' => false,
            'formatter_graphviz_display' => false,
            'formatter_graphviz_dump_image' => null,
            'formatter_graphviz_dump_dot' => null,
            'formatter_graphviz_dump_html' => null,
        ]);

        $resolver->addAllowedTypes('depfile', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz', ['bool']);
        $resolver->addAllowedTypes('formatter_graphviz_display', ['bool']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_image', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_dot', ['null', 'string']);
        $resolver->addAllowedTypes('formatter_graphviz_dump_html', ['null', 'string']);

        return $resolver;
    }

    public function canRunInContext(ContextInterface $context): bool
    {
        return $context instanceof GitPreCommitContext || $context instanceof RunContext;
    }

    public function run(ContextInterface $context): TaskResultInterface
    {
        $config = $this->getConfiguration();

        $files = $context->getFiles()->name('*.php');
        if (0 === \count($files)) {
            return TaskResult::createSkipped($this, $context);
        }

        $arguments = $this->processBuilder->createArgumentsForCommand('deptrac');
        $arguments->add('analyze');
        $arguments->add('--formatter-graphviz='.(int) $config['formatter_graphviz']);
        $arguments->add('--formatter-graphviz-display='.(int) $config['formatter_graphviz_display']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-image=%s', $config['formatter_graphviz_dump_image']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-dot=%s', $config['formatter_graphviz_dump_dot']);
        $arguments->addOptionalArgument('--formatter-graphviz-dump-html=%s', $config['formatter_graphviz_dump_html']);
{'fraction_non_alphanumeric': 0.14413018209996126, 'fraction_numerical': 0.0007748934521503294, 'mean_word_length': 3.9218303145853195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '3997105', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1472, 'n_words': 220}