Coder Social home page Coder Social logo

coder's Introduction

Coder

Build Status

Coder is a library for automated Drupal code reviews and coding standard fixes. It defines rules for PHP_CodeSniffer

Built-in support for:

Online documentation: https://www.drupal.org/node/1419980

Note that Javascript support has been removed. To check and fix Javascript files please use ESLint and see the Drupal ESLint documentation.

Installation

First, make sure Composer is installed correctly:

which composer

If you get composer not found or similar, follow Composer's installation instructions.

Install Coder (8.x-3.x) in your global Composer directory in your home directory (~/.composer):

composer global require drupal/coder

To make the phpcs and phpcbf commands available globally, add the Composer bin path to your $PATH variable in ~/.profile, ~/.bashrc or ~/.zshrc:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Register the Drupal and DrupalPractice Standard with PHPCS:

phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer

Composer Installer Plugins

The Coder package (>= 8.2.11) now works with Composer Installer Plugins, that find and register standards whenever packages are installed or updated. To use such a plugin within your project, follow these steps.

composer require --dev dealerdirect/phpcodesniffer-composer-installer
composer require --dev drupal/coder

Now, you will see Drupal and DrupalPractice listed in the available PHP CodeSniffer standards.

vendor/bin/phpcs -i

The same can be done for a Composer global installation.

composer global require dealerdirect/phpcodesniffer-composer-installer
composer global require drupal/coder

Usage

Check Drupal coding standards

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module

Check Drupal best practices

phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module

Automatically fix coding standards

phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module

Store settings in a phpcs.xml.dist file

In order to save and commit your PHPCS settings to Git you can use a phpcs.xml.dist file in your project like this:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="myproject">
  <description>PHP CodeSniffer configuration for myproject development.</description>
  <!-- Check all files in the current directory and below. -->
  <file>.</file>
  <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/>
  <!-- Change this value to 7 if you want to check Drupal 7 code. -->
  <config name="drupal_core_version" value="8"/>

  <!-- If you have Coder installed locally then you can reference the Drupal
  standards with relative paths. Otherwise simply use "Drupal" and
  "DrupalPractice. -->
  <rule ref="../vendor/drupal/coder/coder_sniffer/Drupal">
    <!-- Example how you would disable a rule you are not compliant with yet:
    <exclude name="Drupal.Commenting.Deprecated"/>
    -->
  </rule>
  <rule ref="../vendor/drupal/coder/coder_sniffer/DrupalPractice"/>

  <!-- Example how you would disable an external rule you do not like:
  <rule ref="PEAR.Functions.ValidDefaultValue.NotAtEnd">
    <severity>0</severity>
  </rule>
  -->
</ruleset>

Then you can invoke phpcs without any options and it will read phpcs.xml.dist from the current directory. This can also be useful for Continuous Integration setups.

Working with Editors

Drupal Code Sniffer can be used with various editors.

Editors:

Automated Testing (PHPUnit + PHPCS + PHPStan)

Coder Sniffer comes with a PHPUnit test suite to make sure the sniffs work correctly. Use Composer to install the dependencies:

composer install

Then execute the tests:

./vendor/bin/phpunit

Then execute the coding standards checker on Coder itself:

./vendor/bin/phpcs

Then execute static analysis:

./vendor/bin/phpstan analyse

Contributing

  1. Make sure an issue exists at https://www.drupal.org/project/issues/coder
  2. Create a Pull Request against https://github.com/pfrenssen/coder
  3. Post a link to the pull request to the issue on drupal.org and set the issue to "needs review"

Thank you!

Maintainers

Credits

Greg Sherwood and Squiz Pty Ltd, many sniffs are modified copies of their original work on PHPCS.

coder's People

Contributors

alexpott avatar arkener avatar brambaud avatar cburschka avatar chipway avatar christopher-hopper avatar das-peter avatar douggreen avatar eiriksm avatar ericduran avatar fenetikm avatar jhedstrom avatar jonathan1055 avatar jsacksick avatar jtubex avatar klausi avatar lobsterr avatar mfernea avatar nicasso avatar paul-m avatar pfrenssen avatar pwolanin avatar snpower avatar solotandem avatar sun avatar t2l avatar truls1502 avatar yurkinpark avatar zaporylie avatar zeip avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.