phpro / grumphp

A PHP code-quality tool
MIT License
4.15k stars 431 forks source link

Stdin input for "RUN" command for JOBS: psalm, phpcsfixer; don't work. #840

Closed FedOken closed 4 years ago

FedOken commented 4 years ago
Q A
Version GrumPHP 1.0.0
Bug? yes
New feature? no
Question? yes
Documentation? no
Related tickets

My configuration

# grumphp.yml
grumphp:
    git_hook_variables:
        EXEC_GRUMPHP_COMMAND: 'docker run --rm --tty --volume $(pwd):/app ...
    tasks:
        composer_script:
            script: lint
            triggered_by:
                - xlf
                - twig
                - yaml
        psalm:
            report: report-psalm.junit.xml
            output_format: junit
            triggered_by:
                - php
            show_info: false
        phpunit: ~
        securitychecker:
            lockfile: ./composer.lock
            format: ~
            end_point: ~
            timeout: ~
            run_always: false
        phpcs:
            standard:
                - .qa/phpcs.xml
            whitelist_patterns:
                - /^src\/(.*)/
                - /^tests\/(.*)/
            triggered_by:
                - php
        phpcsfixer:
            config: .qa/phpcsfixer.dist
            verbose: true
            diff: true
            triggered_by:
                - php
        phpstan:
            autoload_file: ~
            configuration: .qa/phpstan.neon
            level: 5
            force_patterns: []
            ignore_patterns: []
            triggered_by:
                - php
            memory_limit: "-1"
            use_grumphp_paths: true
    environment:
        variables:
            SYMFONY_DEPRECATIONS_HELPER: disabled

Problem: Stdin input for "run" command for jobs: psalm, phpcsfixer; don't work. 1) Step one, get diff.

#Check-diff
git diff --name-only
#Result
src/Controller/Account/AccountActivationController.php

2) Run pre-commit for stdin diff input:

 git diff | vendor/bin/grumphp git:pre-commit

Result:

phpcs
=====

FILE: ...m-website/src/Controller/Account/AccountActivationController.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 47 | ERROR | [x] Line indented incorrectly; expected at least 8
    |       |     spaces, found 0
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

phpcsfixer
==========

1) .qa/../src/Controller/Account/AccountActivationController.php (braces)

--- Original
+++ New
@@ @@
     {
-$test = 1;
+        $test = 1;
         $form = $this->createForm(RequestActivationLinkType::class);
         $form->handleRequest($request);
...

All is fine, we get error from file what in diff.

3) Letss try run for stdin input

 git diff | vendor/bin/grumphp run
phpcs
=====

FILE: ...m-website/src/Controller/Account/AccountActivationController.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 47 | ERROR | [x] Line indented incorrectly; expected at least 8
    |       |     spaces, found 0
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

phpcsfixer
==========
...
161) .qa/../src/ParamConverter/MatchdayParamConverter.php (phpdoc_no_package, yoda_style, phpdoc_separation, phpdoc_trim, phpdoc_align)

--- Original
+++ New
@@ @@
  * Class MatchdayParamConverter
- *
- * @package App\ParamConverter
@@ @@
     /**
-     * @param Request $request
+     * @param Request        $request
      * @param ParamConverter $configuration
+     *
@@ @@
      * @param ParamConverter $configuration
+     *
      * @return bool
      */
     public function supports(ParamConverter $configuration)
     {
-        if ($configuration->getClass() === MatchdayDto::class &&
+        if (MatchdayDto::class === $configuration->getClass() &&
             $configuration
         ) {
             return true;
         }

         return false;
     }

     private function getAnnotationName(ParamConverter $configuration)
     {
         $r = new ReflectionClass($configuration);

         return $r->getShortName();
     }
 }

162) .qa/../src/ParamConverter/BettingPoolParamConverter.php (no_unneeded_control_parentheses, yoda_style, phpdoc_separation)

--- Original
+++ New
@@ @@
      * @return bool
+     *
@@ @@
     {
-        return ($configuration->getClass() === BettingPoolDto::class && $configuration);
+        return BettingPoolDto::class === $configuration->getClass() && $configuration;
@@ @@
      * @return string
+     *
      * @throws \ReflectionException
      */
     private function getAnnotationName(ParamConverter $configuration)
     {
         $r = new ReflectionClass($configuration);

         return $r->getShortName();
     }
 }

As you can see, we steel have one error for phpcs and all project errors for phpcsfixer, how to fix it?

Or how to run with --tasks=phpcsfixer and it show me error only in my diff?

veewee commented 4 years ago

Run doesn't always behave exactly the same as pre-commit.

Both tasks for php-cs-fixer and psalm don't specify any files on the CLI during 'run'. Only on pre-commit, files are added to the command.

Reference:

Therefore, you'll get other results based on what command you run - even if you pass in a list of changed files.

FedOken commented 4 years ago

So, how I can run only php-cs-fixer job (--tasks=phpcsfixer) in my CI? This job shoud check only diff files.