Carnet Wiki

Tester la conformité d’un style code php avec les PSR SPIP

Version 13 — 14 May JLuc

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

codesniffer parse et teste le source selon des jeux de règles. Ces jeux de règles peuvent être les PSR par défaut, ou des jeux de règles fournis.

- Installer : https://github.com/squizlabs/PHP_CodeSniffer
-  Utiliser : https://github.com/squizlabs/PHP_CodeSniffer/wiki

Exemple : Jeu de régle “compatibilité PHP7”

Ce jeu de règle permet de tester la compatibilité d’un code avec PHP7 et 7.2
-  téléchargement : https://github.com/PHPCompatibility/PHPCompatibility
-  paramétrage de codesniffer avec le jeu de règle phpcompatibility :
php phpcs.phar --config-set installed_paths /chemin/vers/phpcompatibility
-  usage :
php phpcs.phar /chemin/vers/source/php

-----

Toute la suite de cette doc est plus ancienne et certaines indications sont peut être périmées

Rq : phpcs 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

Jeu Code de règles phpcs.xml pour SPIP

SPIP :

-  télécharger master : https://gist.github.com/brunob/c92c9b7bfec67aadc149
-  une fois créé localement , codesniffer peut l’utiliser version au 8 décembre :
<
code>php phpcs . phar —standard=spipcs.xml /chemin/vers/source/php

- pour mémoire, version au 8 décembre :

<?xml version="1."?>
<ruleset name="SPIP">
<!--
	Liens utiles
                documentation : http://contrib.spip.net/style-code-php
		https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
		https://github.com/ucfcdl/fuelphp-phpcs/tree/master/Standards/FuelPHP
		https://github.com/vanilla/addons/tree/master/standards/Vanilla
-->
 
 
<description>Coding rules for SPIP</description>
 
 
<exclude-pattern>config/*</exclude-pattern>
	<exclude-pattern>IMG/*</exclude-pattern>
	<exclude-pattern>lib/*</exclude-pattern>
	<exclude-pattern>local/*</exclude-pattern>
	<exclude-pattern>plugins/*</exclude-pattern>
	<exclude-pattern>squelettes/*</exclude-pattern>
	<exclude-pattern>tmp/*</exclude-pattern>
 
 
<!-- Appliquer PSR-2 moins nos exceptions -->
	<rule ref="PSR2" >
		<!-- Désactiver la vérification sur les noms de classes/fonctions -->
		<exclude name="Squiz.Classes.ValidClassName" />
		<!-- Désactiver la vérification sur l'indentation -->
		<exclude name="Generic.WhiteSpace.ScopeIndent" />
		<exclude name="Generic.WhiteSpace.DisallowTabIndent" />
		<!-- Désactiver la vérification sur les accolades -->
		<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
		<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
		<exclude name="PSR2.Classes.PropertyDeclaration" />
	</rule>
 
 
<!-- Tabulations pour l'indentation -->
	<arg name="tab-width" value="4"/>
	<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
	<rule ref="Generic.WhiteSpace.ScopeIndent">
		<properties>
			<property name="indent" value="4"/>
			<property name="tabIndent" value="true"/>
		</properties>
	</rule>
 
 
<!-- Accolades -->
	<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
	<rule ref="Generic.ControlStructures.InlineControlStructure" />
	<rule ref="Squiz.ControlStructures.ControlSignature" />
	<rule ref="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace">
		<severity>0</severity>
	</rule>
 
 
<!-- Guillemets doubles -->
	<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
	<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
		<severity>0</severity>
	</rule>
 
	<!-- Constantes en majuscules -->
	<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
 
 
</ruleset>

CodeStyle SPIP pour PhpStorm

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

<code_scheme name="SPIP">
  <option name="OTHER_INDENT_OPTIONS">
    <value>
      <option name="INDENT_SIZE" value="2" />
      <option name="CONTINUATION_INDENT_SIZE" value="8" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
      <option name="SMART_TABS" value="false" />
      <option name="LABEL_INDENT_SIZE" value="0" />
      <option name="LABEL_INDENT_ABSOLUTE" value="false" />
      <option name="USE_RELATIVE_INDENTS" value="false" />
    </value>
  </option>
  <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
  <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
  <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
  <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
  <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
  <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
  <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
  <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
  <PHPCodeStyleSettings>
    <option name="PHPDOC_BLANK_LINE_BEFORE_TAGS" value="true" />
    <option name="LOWER_CASE_BOOLEAN_CONST" value="true" />
    <option name="LOWER_CASE_NULL_CONST" value="true" />
    <option name="BLANK_LINE_BEFORE_RETURN_STATEMENT" value="true" />
    <option name="KEEP_RPAREN_AND_LBRACE_ON_ONE_LINE" value="true" />
  </PHPCodeStyleSettings>
  <XML>
    <option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
  </XML>
  <codeStyleSettings language="CSS">
    <indentOptions>
      <option name="INDENT_SIZE" value="2" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="CoffeeScript">
    <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
    <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
    <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
    <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
    <option name="PARENT_SETTINGS_INSTALLED" value="true" />
  </codeStyleSettings>
  <codeStyleSettings language="HTML">
    <indentOptions>
      <option name="INDENT_SIZE" value="2" />
      <option name="CONTINUATION_INDENT_SIZE" value="2" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
      <option name="SMART_TABS" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="JavaScript">
    <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
    <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
    <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
    <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
    <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
    <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
    <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
    <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
    <option name="PARENT_SETTINGS_INSTALLED" value="true" />
    <indentOptions>
      <option name="INDENT_SIZE" value="2" />
      <option name="CONTINUATION_INDENT_SIZE" value="2" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
      <option name="SMART_TABS" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="LESS">
    <indentOptions>
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="PHP">
    <option name="BLANK_LINES_AFTER_PACKAGE" value="1" />
    <option name="CLASS_BRACE_STYLE" value="1" />
    <option name="METHOD_BRACE_STYLE" value="1" />
    <option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
    <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
    <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
    <option name="CALL_PARAMETERS_WRAP" value="1" />
    <option name="METHOD_PARAMETERS_WRAP" value="5" />
    <option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
    <option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" />
    <option name="KEEP_SIMPLE_METHODS_IN_ONE_LINE" value="true" />
    <option name="ARRAY_INITIALIZER_WRAP" value="5" />
    <option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
    <option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" />
    <option name="IF_BRACE_FORCE" value="3" />
    <option name="DOWHILE_BRACE_FORCE" value="3" />
    <option name="WHILE_BRACE_FORCE" value="3" />
    <option name="FOR_BRACE_FORCE" value="3" />
    <indentOptions>
      <option name="INDENT_SIZE" value="2" />
      <option name="CONTINUATION_INDENT_SIZE" value="2" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
      <option name="SMART_TABS" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="SASS">
    <indentOptions>
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="TypeScript">
    <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
    <option name="SPACE_AROUND_RELATIONAL_OPERATORS" value="false" />
    <option name="SPACE_AROUND_ADDITIVE_OPERATORS" value="false" />
    <option name="SPACE_AROUND_MULTIPLICATIVE_OPERATORS" value="false" />
    <option name="SPACE_BEFORE_METHOD_LBRACE" value="false" />
    <option name="SPACE_BEFORE_IF_LBRACE" value="false" />
    <option name="SPACE_BEFORE_WHILE_LBRACE" value="false" />
    <option name="SPACE_BEFORE_FOR_LBRACE" value="false" />
    <option name="PARENT_SETTINGS_INSTALLED" value="true" />
  </codeStyleSettings>
  <codeStyleSettings language="XML">
    <indentOptions>
      <option name="INDENT_SIZE" value="2" />
      <option name="CONTINUATION_INDENT_SIZE" value="2" />
      <option name="TAB_SIZE" value="2" />
      <option name="USE_TAB_CHARACTER" value="true" />
    </indentOptions>
  </codeStyleSettings>
  <codeStyleSettings language="yaml">
    <indentOptions>
      <option name="TAB_SIZE" value="2" />
    </indentOptions>
  </codeStyleSettings>
</code_scheme>

CodeStyle SPIP pour Sublime Text

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

<?xml version="1."?>
<ruleset name="SPIP">
<!--
	Liens utiles
                documentation : http://contrib.spip.net/style-code-php
		https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
		https://github.com/ucfcdl/fuelphp-phpcs/tree/master/Standards/FuelPHP
		https://github.com/vanilla/addons/tree/master/standards/Vanilla
-->
 
 
<description>Coding rules for SPIP with Sublime text</description>
 
 
<exclude-pattern>config/*</exclude-pattern>
	<exclude-pattern>IMG/*</exclude-pattern>
	<exclude-pattern>lib/*</exclude-pattern>
	<exclude-pattern>local/*</exclude-pattern>
	<exclude-pattern>plugins-dist/*</exclude-pattern>
	<exclude-pattern>squelettes/*</exclude-pattern>
	<exclude-pattern>tmp/*</exclude-pattern>
 
 
<!-- Appliquer PSR-2 moins nos exceptions -->
	<rule ref="PSR2" >
		<!-- Désactiver la vérification sur les noms de classes/fonctions -->
		<exclude name="Squiz.Classes.ValidClassName" />
		<!-- Désactiver la vérification sur l'indentation -->
		<exclude name="Generic.WhiteSpace.ScopeIndent" />
		<exclude name="Generic.WhiteSpace.DisallowTabIndent" />
		<!-- Désactiver les camel caps sur les fonctions -->
		<exclude name="Generic.NamingConventions.CamelCapsFunctionName" />
		<!-- Désactiver la vérification sur les accolades -->
		<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
		<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
		<exclude name="PSR2.Classes.PropertyDeclaration" />
	</rule>
 
 
<!-- Tabulations pour l'indentation -->
	<arg name="tab-width" value="4"/>
	<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
	<rule ref="Generic.WhiteSpace.ScopeIndent">
		<properties>
			<property name="indent" value="4"/>
			<property name="tabIndent" value="true"/>
		</properties>
	</rule>
 
 
<!-- Accolades -->
	<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
	<rule ref="Generic.ControlStructures.InlineControlStructure" />
	<rule ref="Squiz.ControlStructures.ControlSignature" />
	<rule ref="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace">
		<severity>0</severity>
	</rule>
 
 
<!-- Guillemets doubles -->
	<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
	<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
		<severity>0</severity>
	</rule>
 
	<!-- Constantes en majuscules -->
	<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
 
 
</ruleset>