Carnet Wiki

style code php

Version 12 — Janvier 2017 — goetsu

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

Télécharger