custom_rules.rst 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. =====================
  2. Creating custom rules
  3. =====================
  4. If you need to enforce some specific code style rules, you can implement your
  5. own fixers.
  6. For each rule you want to add, create a class that implements
  7. `PhpCsFixer\\Fixer\\FixerInterface <../src/Fixer/FixerInterface.php>`_.
  8. Note that there is a specific constraint
  9. regarding custom rules names: they must match the pattern
  10. ``/^[A-Z][a-zA-Z0-9]*\/[a-z][a-z0-9_]*$/``.
  11. Then register your custom fixers and enable them in the config file:
  12. .. code-block:: php
  13. <?php
  14. // ...
  15. return (new PhpCsFixer\Config())
  16. // ...
  17. ->registerCustomFixers([
  18. new CustomerFixer1(),
  19. new CustomerFixer2(),
  20. ])
  21. ->setRules([
  22. // ...
  23. 'YourVendorName/custome_rule' => true,
  24. 'YourVendorName/custome_rule_2' => true,
  25. ])
  26. ;
  27. There are several interfaces that your fixers can also implement if needed:
  28. * `PhpCsFixer\\Fixer\\WhitespacesAwareFixerInterface <../src/Fixer/WhitespacesAwareFixerInterface.php>`_: for fixers that need to know the configured indentation and line endings;
  29. * `PhpCsFixer\\Fixer\\ConfigurableFixerInterface <../src/Fixer/ConfigurableFixerInterface.php>`_: to create a configurable fixer;
  30. * `PhpCsFixer\\Fixer\\DeprecatedFixerInterface <../src/Fixer/DeprecatedFixerInterface.php>`_: to deprecate a fixer.