Phpinsights: Add support for phpro/grumphp

Created on 5 Jan 2020  路  6Comments  路  Source: nunomaduro/phpinsights

| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | yes

It would be great if we could create an extension to run phpinsights on commit with a grumphp task

https://github.com/phpro/grumphp

All 6 comments

I don't know that much about grumphp, but as far as I understand, for adding support for grumphp, it has to be added to grum, not phpinsights right?

You can create extensions yourself but if you're not interested in containing that in this repo then I am happy to close this issue out. I have some time next week to work on this so can either do a PR here or on Grumphp (with a link in the README if you would allow that)

https://github.com/phpro/grumphp/blob/master/doc/extensions.md

@nunomaduro what do you say about this, is this something we want to maintain or should we leave it to grumphp to maintain?

I don't use that tool myself. I would prefer them to maintain that.

@schrapel thanks for open the issue. We would appreciate a lot if you created a pr on grumphp for this. :smile:

For anyone looking to do this. Here's a custom GrumPHP task that I built.

namespace Tests\GrumPHP;

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

class PhpInsightsTask extends AbstractExternalTask
{
    /**
     * {@inheritdoc}
     */
    public static function getConfigurableOptions(): OptionsResolver
    {
        $resolver = new OptionsResolver();
        $resolver->setDefaults([
            'config-path' => null,
            'min-architecture' => 85,
            'min-complexity' => 70,
            'min-quality' => 90,
            'min-style' => 95,
        ]);

        $resolver->addAllowedTypes('config-path', ['null', 'string']);
        $resolver->addAllowedTypes('min-architecture', ['null', 'int']);
        $resolver->addAllowedTypes('min-complexity', ['null', 'int']);
        $resolver->addAllowedTypes('min-quality', ['null', 'int']);
        $resolver->addAllowedTypes('min-style', ['null', 'int']);

        return $resolver;
    }

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

    /**
     * {@inheritdoc}
     */
    public function run(ContextInterface $context): TaskResultInterface
    {
        $config = $this->getConfig()->getOptions();

        $arguments = $this->processBuilder->createArgumentsForCommand('php');
        $arguments->addSeparatedArgumentArray('artisan', ['insights', '--quiet', '--no-interaction']);
        $arguments->addOptionalArgument('--min-architecture=%s', $config['min-architecture']);
        $arguments->addOptionalArgument('--min-complexity=%s', $config['min-complexity']);
        $arguments->addOptionalArgument('--min-quality=%s', $config['min-quality']);
        $arguments->addOptionalArgument('--min-style=%s', $config['min-style']);

        $process = $this->processBuilder->buildProcess($arguments);
        $process->run();

        if (!$process->isSuccessful()) {
            return TaskResult::createFailed($this, $context, $this->formatter->format($process));
        }

        return TaskResult::createPassed($this, $context);
    }
}

The task uses the php artisan insights Laravel command, but it's easy to shift it to another one. After that, all you have to do is add the task to the grumphp.yml like this:

services:
  Tests\GrumPHP\PhpInsightsTask:
    arguments:
      - '@process_builder'
      - '@formatter.raw_process'
    tags:
      - {name: grumphp.task, task: phpinsights}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sciracioglu picture sciracioglu  路  3Comments

calvinchiulele picture calvinchiulele  路  5Comments

erwinw picture erwinw  路  6Comments

nunomaduro picture nunomaduro  路  4Comments

brunocfalcao picture brunocfalcao  路  5Comments