Carnet Wiki

style code php

SPIP-Contrib :: Carnet Wiki :: Recherche :

style code php

Les recommandations pour le code source de SPIP s’appuient sur PSR2 avec quelques modifications (voir doc SPIP.net)

Voici les fichiers de configuration pour :
-  CodeSniffer
-  PhpStorm

Avec CodeSniffer

Rq : php-cs-fixer ne permet pas de corriger les tabulations actuellement.

-   ? Télécharger et installer phpcs https://github.com/squizlabs/PHP_CodeSniffer
-  Créer le fichier de config spipcs.xml en y recopiant les paramètres plus bas
-  Se mettre à la racine du dossier à vérifier et lancer la commande php phpcs.phar --standard=spipcs.xml
-  ou bien se mettre à la racine du spip et lancer php phpcs.phar --standard=spipcs.xml chemin/vers/dossier/a/verifier

Code de phpcs.xml pour SPIP :

-  master : https://gist.github.com/brunob/c92c9b7bfec67aadc149
-  version au 8 décembre :

  1. <?xml version="1.0"?>
  2. <ruleset name="SPIP">
  3. <!--
  4. Liens utiles
  5. documentation : http://contrib.spip.net/style-code-php
  6. https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
  7. https://github.com/ucfcdl/fuelphp-phpcs/tree/master/Standards/FuelPHP
  8. https://github.com/vanilla/addons/tree/master/standards/Vanilla
  9. -->
  10.  
  11. <description>Coding rules for SPIP</description>
  12.  
  13. <exclude-pattern>config/*</exclude-pattern>
  14. <exclude-pattern>IMG/*</exclude-pattern>
  15. <exclude-pattern>lib/*</exclude-pattern>
  16. <exclude-pattern>local/*</exclude-pattern>
  17. <exclude-pattern>plugins/*</exclude-pattern>
  18. <exclude-pattern>squelettes/*</exclude-pattern>
  19. <exclude-pattern>tmp/*</exclude-pattern>
  20.  
  21. <!-- Appliquer PSR-2 moins nos exceptions -->
  22. <rule ref="PSR2" >
  23. <!-- Désactiver la vérification sur les noms de classes/fonctions -->
  24. <exclude name="Squiz.Classes.ValidClassName" />
  25. <!-- Désactiver la vérification sur l'indentation -->
  26. <exclude name="Generic.WhiteSpace.ScopeIndent" />
  27. <exclude name="Generic.WhiteSpace.DisallowTabIndent" />
  28. <!-- Désactiver la vérification sur les accolades -->
  29. <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
  30. <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
  31. <exclude name="PSR2.Classes.PropertyDeclaration" />
  32. </rule>
  33.  
  34. <!-- Tabulations pour l'indentation -->
  35. <arg name="tab-width" value="4"/>
  36. <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
  37. <rule ref="Generic.WhiteSpace.ScopeIndent">
  38. <properties>
  39. <property name="indent" value="4"/>
  40. <property name="tabIndent" value="true"/>
  41. </properties>
  42. </rule>
  43.  
  44. <!-- Accolades -->
  45. <rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
  46. <rule ref="Generic.ControlStructures.InlineControlStructure" />
  47. <rule ref="Squiz.ControlStructures.ControlSignature" />
  48. <rule ref="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace">
  49. <severity>0</severity>
  50. </rule>
  51.  
  52. <!-- Guillemets doubles -->
  53. <rule ref="Squiz.Strings.DoubleQuoteUsage"/>
  54. <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  55. <severity>0</severity>
  56. </rule>
  57.  
  58. <!-- Constantes en majuscules -->
  59. <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
  60.  
  61. </ruleset>

Télécharger

CodeStyle SPIP pour PhpStorm

Voir aussi https://gist.github.com/Cerdic/efdb1d5c9318670d19fe

  1. <code_scheme name="SPIP">
  2. <option name="OTHER_INDENT_OPTIONS">
  3. <value>
  4. <option name="INDENT_SIZE" value="2" />
  5. <option name="CONTINUATION_INDENT_SIZE" value="8" />
  6. <option name="TAB_SIZE" value="2" />
  7. <option name="USE_TAB_CHARACTER" value="true" />
  8. <option name="SMART_TABS" value="false" />
  9. <option name="LABEL_INDENT_SIZE" value="0" />
  10. <option name="LABEL_INDENT_ABSOLUTE" value="false" />
  11. <option name="USE_RELATIVE_INDENTS" value="false" />
  12. </value>
  13. </option>
  14. <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
  15. <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
  16. <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  17. <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  18. <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
  19. <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
  20. <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
  21. <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
  22. <PHPCodeStyleSettings>
  23. <option name="PHPDOC_BLANK_LINE_BEFORE_TAGS" value="true" />
  24. <option name="LOWER_CASE_BOOLEAN_CONST" value="true" />
  25. <option name="LOWER_CASE_NULL_CONST" value="true" />
  26. <option name="BLANK_LINE_BEFORE_RETURN_STATEMENT" value="true" />
  27. <option name="KEEP_RPAREN_AND_LBRACE_ON_ONE_LINE" value="true" />
  28. </PHPCodeStyleSettings>
  29. <XML>
  30. <option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
  31. </XML>
  32. <codeStyleSettings language="CSS">
  33. <indentOptions>
  34. <option name="INDENT_SIZE" value="2" />
  35. <option name="TAB_SIZE" value="2" />
  36. <option name="USE_TAB_CHARACTER" value="true" />
  37. </indentOptions>
  38. </codeStyleSettings>
  39. <codeStyleSettings language="CoffeeScript">
  40. <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
  41. <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
  42. <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  43. <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  44. <option name="PARENT_SETTINGS_INSTALLED" value="true" />
  45. </codeStyleSettings>
  46. <codeStyleSettings language="HTML">
  47. <indentOptions>
  48. <option name="INDENT_SIZE" value="2" />
  49. <option name="CONTINUATION_INDENT_SIZE" value="2" />
  50. <option name="TAB_SIZE" value="2" />
  51. <option name="USE_TAB_CHARACTER" value="true" />
  52. <option name="SMART_TABS" value="true" />
  53. </indentOptions>
  54. </codeStyleSettings>
  55. <codeStyleSettings language="JavaScript">
  56. <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
  57. <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
  58. <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  59. <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  60. <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
  61. <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
  62. <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
  63. <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
  64. <option name="PARENT_SETTINGS_INSTALLED" value="true" />
  65. <indentOptions>
  66. <option name="INDENT_SIZE" value="2" />
  67. <option name="CONTINUATION_INDENT_SIZE" value="2" />
  68. <option name="TAB_SIZE" value="2" />
  69. <option name="USE_TAB_CHARACTER" value="true" />
  70. <option name="SMART_TABS" value="true" />
  71. </indentOptions>
  72. </codeStyleSettings>
  73. <codeStyleSettings language="LESS">
  74. <indentOptions>
  75. <option name="TAB_SIZE" value="2" />
  76. <option name="USE_TAB_CHARACTER" value="true" />
  77. </indentOptions>
  78. </codeStyleSettings>
  79. <codeStyleSettings language="PHP">
  80. <option name="BLANK_LINES_AFTER_PACKAGE" value="1" />
  81. <option name="CLASS_BRACE_STYLE" value="1" />
  82. <option name="METHOD_BRACE_STYLE" value="1" />
  83. <option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
  84. <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  85. <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  86. <option name="CALL_PARAMETERS_WRAP" value="1" />
  87. <option name="METHOD_PARAMETERS_WRAP" value="5" />
  88. <option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
  89. <option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" />
  90. <option name="KEEP_SIMPLE_METHODS_IN_ONE_LINE" value="true" />
  91. <option name="ARRAY_INITIALIZER_WRAP" value="5" />
  92. <option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
  93. <option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" />
  94. <option name="IF_BRACE_FORCE" value="3" />
  95. <option name="DOWHILE_BRACE_FORCE" value="3" />
  96. <option name="WHILE_BRACE_FORCE" value="3" />
  97. <option name="FOR_BRACE_FORCE" value="3" />
  98. <indentOptions>
  99. <option name="INDENT_SIZE" value="2" />
  100. <option name="CONTINUATION_INDENT_SIZE" value="2" />
  101. <option name="TAB_SIZE" value="2" />
  102. <option name="USE_TAB_CHARACTER" value="true" />
  103. <option name="SMART_TABS" value="true" />
  104. </indentOptions>
  105. </codeStyleSettings>
  106. <codeStyleSettings language="SASS">
  107. <indentOptions>
  108. <option name="TAB_SIZE" value="2" />
  109. <option name="USE_TAB_CHARACTER" value="true" />
  110. </indentOptions>
  111. </codeStyleSettings>
  112. <codeStyleSettings language="TypeScript">
  113. <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
  114. <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
  115. <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  116. <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  117. <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
  118. <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
  119. <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
  120. <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
  121. <option name="PARENT_SETTINGS_INSTALLED" value="true" />
  122. </codeStyleSettings>
  123. <codeStyleSettings language="XML">
  124. <indentOptions>
  125. <option name="INDENT_SIZE" value="2" />
  126. <option name="CONTINUATION_INDENT_SIZE" value="2" />
  127. <option name="TAB_SIZE" value="2" />
  128. <option name="USE_TAB_CHARACTER" value="true" />
  129. </indentOptions>
  130. </codeStyleSettings>
  131. <codeStyleSettings language="yaml">
  132. <indentOptions>
  133. <option name="TAB_SIZE" value="2" />
  134. </indentOptions>
  135. </codeStyleSettings>
  136. </code_scheme>

Télécharger

CodeStyle SPIP pour Sublime Text

-  master : https://gist.github.com/magikcypress/200f2d2491ba5cc57051fdf6d9412fbd
-  version au 12 mai 2016 :

  1. <?xml version="1.0"?>
  2. <ruleset name="SPIP">
  3. <!--
  4. Liens utiles
  5. documentation : http://contrib.spip.net/style-code-php
  6. https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
  7. https://github.com/ucfcdl/fuelphp-phpcs/tree/master/Standards/FuelPHP
  8. https://github.com/vanilla/addons/tree/master/standards/Vanilla
  9. -->
  10.  
  11. <description>Coding rules for SPIP with Sublime text</description>
  12.  
  13. <exclude-pattern>config/*</exclude-pattern>
  14. <exclude-pattern>IMG/*</exclude-pattern>
  15. <exclude-pattern>lib/*</exclude-pattern>
  16. <exclude-pattern>local/*</exclude-pattern>
  17. <exclude-pattern>plugins-dist/*</exclude-pattern>
  18. <exclude-pattern>squelettes/*</exclude-pattern>
  19. <exclude-pattern>tmp/*</exclude-pattern>
  20.  
  21. <!-- Appliquer PSR-2 moins nos exceptions -->
  22. <rule ref="PSR2" >
  23. <!-- Désactiver la vérification sur les noms de classes/fonctions -->
  24. <exclude name="Squiz.Classes.ValidClassName" />
  25. <!-- Désactiver la vérification sur l'indentation -->
  26. <exclude name="Generic.WhiteSpace.ScopeIndent" />
  27. <exclude name="Generic.WhiteSpace.DisallowTabIndent" />
  28. <!-- Désactiver les camel caps sur les fonctions -->
  29. <exclude name="Generic.NamingConventions.CamelCapsFunctionName" />
  30. <!-- Désactiver la vérification sur les accolades -->
  31. <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
  32. <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
  33. <exclude name="PSR2.Classes.PropertyDeclaration" />
  34. </rule>
  35.  
  36. <!-- Tabulations pour l'indentation -->
  37. <arg name="tab-width" value="4"/>
  38. <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
  39. <rule ref="Generic.WhiteSpace.ScopeIndent">
  40. <properties>
  41. <property name="indent" value="4"/>
  42. <property name="tabIndent" value="true"/>
  43. </properties>
  44. </rule>
  45.  
  46. <!-- Accolades -->
  47. <rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
  48. <rule ref="Generic.ControlStructures.InlineControlStructure" />
  49. <rule ref="Squiz.ControlStructures.ControlSignature" />
  50. <rule ref="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace">
  51. <severity>0</severity>
  52. </rule>
  53.  
  54. <!-- Guillemets doubles -->
  55. <rule ref="Squiz.Strings.DoubleQuoteUsage"/>
  56. <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  57. <severity>0</severity>
  58. </rule>
  59.  
  60. <!-- Constantes en majuscules -->
  61. <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
  62.  
  63. </ruleset>

Télécharger