Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-74511

Include .phpcs.xml with core

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.11.6, 4.0
    • 3.11.7, 4.0.1
    • General
    • MOODLE_311_STABLE, MOODLE_400_STABLE
    • MOODLE_311_STABLE, MOODLE_400_STABLE
    • MDL-74511-311
    • MDL-74511-400
    • MDL-74511-master
    • Hide
      Setup
      1. Install `phpcs` and the moodlehq coding standards globally:

        composer global require moodlehq/moodle-cs
        

      2. Ensure that your global vendor binaries directory is in your $PATH as noted at https://getcomposer.org/doc/03-cli.md#global - this will depend on your installation, but usually it's a case of updating your .bashrc or .zshrc (or whatever shell you use) to add:

        export PATH=~/.composer/vendor/bin:$PATH
        

      Test
      1. Go to your moodle instance's root folder.
      2. Make sure you're using the correct nodejs version and that grunt-cli is installed. Assuming you have nvm:

        nvm install
        npm i
        

      3. Ensure you do not already have the phpcs.xml file:

        rm .phpcs.xml
        

      4. Remove any default standard for phpcs:

        phpcs --config-delete default_standard
        

      5. Run phpcs against course/format/classes:

        phpcs -sp course/format/classes
        

        1. Confirm that there were some warnings about print_error() (which is a Moodleism)
          Note: There are also some other warnings
      6. Run phpcs against an external file, for example lib/zipstream:

        phpcs -sp lib/zipstream
        

        1. Confirm that loads of Moodle-specific errors/warnings are shown (e.g. header)
          (At this point we don't know how to ignore it)
      7. Generate the ignorefile:

        npx grunt ignorefiles
        

      8. Run phpcs against an external file again, for example lib/zipstream:

        phpcs -sp lib/zipstream
        

        1. Confirm that there was no output at all
      9. Run phpcs with increased verbosity to check that files are appropriately ignored:

        phpcs -spvv lib/zipstream
        

        1. Confirm that the file was loaded:

          Processing ruleset /Users/nicols/Sites/moodles/sm/moodle/.phpcs.xml
          

        2. *Confirm that the Moodle ruleset was then used:

           	Processing rule "moodle"
          

        3. Confirm that a number of global ignore patterns were added, including lib/zipstream:

          	=> added global absolute ignore pattern: lib/zipstream/
          

      10. Remove the Moodle standard but ensure that phpcs is still instaled

        composer global remove moodlehq/moodle-cs
        composer global require squizlabs/php_codesniffer
        

      11. Run phpcs on a file again

        phpcs index.php
        

        1. Confirm that it complained that no sniffs were avaialble:

          ERROR: No sniffs were registered
           
          Run "phpcs --help" for usage information
          

      12. Add the moodle-local_codechecker plugin:

        git clone https://github.com/moodlehq/moodle-local_codechecker local/codechecker
        

      13. Try to run the global phpcs again:

        phpcs index.php
        

        1. Confirm that it failed spectacularly:

          ➜  sm git:(MDL-74511-master) ✗ phpcs lib/zipstream
          PHP Fatal error:  Cannot declare class PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff, because the name is already in use in /Users/nicols/Sites/moodles/sm/moodle/local/codechecker/phpcs/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php on line 16
          PHP Stack trace:
          PHP   1. {main}() /Users/nicols/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs:0
          ...
          

      14. Instead run the phpcsp from the local/codechecker directory:

        local/codechecker/phpcs/bin/phpcs index.php
        

        1. *Confirm that it ran the included Moodle sniffs:

          FILE: /Users/nicols/Sites/moodles/sm/moodle/index.php
          -------------------------------------------------------------------------------------------------------
          FOUND 1 ERROR AFFECTING 1 LINE
          -------------------------------------------------------------------------------------------------------
           25 | ERROR | Expected MOODLE_INTERNAL check or config.php inclusion. Change in global state detected.
          -------------------------------------------------------------------------------------------------------
           
          Time: 157ms; Memory: 12MB
          

      15. Run the local/codechecker version against lib/zipstream again:

        local/codechecker/phpcs/bin/phpcs -sp lib/zipstream
        

        1. Confirm that no files were processed
      16. Run phpcs with increased verbosity to check that files are appropriately ignored:

        local/codechecker/phpcs/bin/phpcs -spvv lib/zipstream
        

        1. Confirm that the file was loaded:

          Processing ruleset /Users/nicols/Sites/moodles/sm/moodle/.phpcs.xml
          

        2. *Confirm that the Moodle ruleset was then used:

           	Processing rule "moodle"
          

        3. Confirm that a number of global ignore patterns were added, including lib/zipstream:

          	=> added global absolute ignore pattern: lib/zipstream/
          

      Show
      Setup Install `phpcs` and the moodlehq coding standards globally: composer global require moodlehq/moodle-cs Ensure that your global vendor binaries directory is in your $PATH as noted at https://getcomposer.org/doc/03-cli.md#global - this will depend on your installation, but usually it's a case of updating your .bashrc or .zshrc (or whatever shell you use) to add: export PATH=~/.composer/vendor/bin:$PATH Test Go to your moodle instance's root folder. Make sure you're using the correct nodejs version and that grunt-cli is installed. Assuming you have nvm : nvm install npm i Ensure you do not already have the phpcs.xml file: rm .phpcs.xml Remove any default standard for phpcs: phpcs --config-delete default_standard Run phpcs against course/format/classes : phpcs -sp course/format/classes Confirm that there were some warnings about print_error() (which is a Moodleism) Note: There are also some other warnings Run phpcs against an external file, for example lib/zipstream : phpcs -sp lib/zipstream Confirm that loads of Moodle-specific errors/warnings are shown (e.g. header) (At this point we don't know how to ignore it) Generate the ignorefile: npx grunt ignorefiles Run phpcs against an external file again, for example lib/zipstream : phpcs -sp lib/zipstream Confirm that there was no output at all Run phpcs with increased verbosity to check that files are appropriately ignored: phpcs -spvv lib/zipstream Confirm that the file was loaded: Processing ruleset /Users/nicols/Sites/moodles/sm/moodle/.phpcs.xml *Confirm that the Moodle ruleset was then used: Processing rule "moodle" Confirm that a number of global ignore patterns were added, including lib/zipstream : => added global absolute ignore pattern: lib/zipstream/ Remove the Moodle standard but ensure that phpcs is still instaled composer global remove moodlehq/moodle-cs composer global require squizlabs/php_codesniffer Run phpcs on a file again phpcs index.php Confirm that it complained that no sniffs were avaialble : ERROR: No sniffs were registered   Run "phpcs --help" for usage information Add the moodle-local_codechecker plugin: git clone https://github.com/moodlehq/moodle-local_codechecker local/codechecker Try to run the global phpcs again: phpcs index.php Confirm that it failed spectacularly : ➜ sm git:(MDL-74511-master) ✗ phpcs lib/zipstream PHP Fatal error: Cannot declare class PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff, because the name is already in use in /Users/nicols/Sites/moodles/sm/moodle/local/codechecker/phpcs/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php on line 16 PHP Stack trace: PHP 1. {main}() /Users/nicols/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs:0 ... Instead run the phpcsp from the local/codechecker directory: local/codechecker/phpcs/bin/phpcs index.php *Confirm that it ran the included Moodle sniffs: FILE: /Users/nicols/Sites/moodles/sm/moodle/index.php ------------------------------------------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ------------------------------------------------------------------------------------------------------- 25 | ERROR | Expected MOODLE_INTERNAL check or config.php inclusion. Change in global state detected. -------------------------------------------------------------------------------------------------------   Time: 157ms; Memory: 12MB Run the local/codechecker version against lib/zipstream again: local/codechecker/phpcs/bin/phpcs -sp lib/zipstream Confirm that no files were processed Run phpcs with increased verbosity to check that files are appropriately ignored: local/codechecker/phpcs/bin/phpcs -spvv lib/zipstream Confirm that the file was loaded: Processing ruleset /Users/nicols/Sites/moodles/sm/moodle/.phpcs.xml *Confirm that the Moodle ruleset was then used: Processing rule "moodle" Confirm that a number of global ignore patterns were added, including lib/zipstream : => added global absolute ignore pattern: lib/zipstream/

    Description

      We should include a .phpcs.xml file and ship with core. We should build it from the various thirdpartylibs.xml files and ensure that it's up-to-date when we add third party libraries.

      The file is documented in https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset and we can also have it extend the moodle standard with:

      <rule ref="moodle" />
      

      Attachments

        1. step_11.png
          step_11.png
          23 kB
        2. step_13.png
          step_13.png
          54 kB
        3. step_14.png
          step_14.png
          67 kB
        4. step_15.png
          step_15.png
          25 kB
        5. step_16_1.png
          step_16_1.png
          106 kB
        6. step_16_2.png
          step_16_2.png
          98 kB
        7. step_5.png
          step_5.png
          158 kB
        8. step_6.png
          step_6.png
          480 kB
        9. step_8.png
          step_8.png
          62 kB
        10. step_9_1.png
          step_9_1.png
          105 kB
        11. step_9_2.png
          step_9_2.png
          99 kB

        Issue Links

          Activity

            People

              dobedobedoh Andrew Lyons
              dobedobedoh Andrew Lyons
              Sara Arjona (@sarjona) Sara Arjona (@sarjona)
              Jun Pataleta Jun Pataleta
              Huong Nguyen Huong Nguyen
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                9/May/22

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 6 hours, 26 minutes
                  6h 26m