| Utilisation (Previous) | (Next) Configuration Options | ||||
| |
|||||
|
|||||
Par défaut, PHP_CodeSniffer ne va vérifier que les fichiers trouvés avec les extentions .inc ou .php. Parfois, cela signifie que PHP_CodeSniffer ne vérifie pas assez de vos fichiers. Parfois, l'inverse est vrai. PHP_CodeSniffer permet d'indiquer la liste des extentions valides en utilisant l'argument --extensions. Les extensions sont à séparer par des virgules.
Exemple 58-1. Ne vérifier QUE les fichiers .php
|
Exemple 58-2. Vérifier les fichiers avec les extentions .php, .inc et .lib
|
Note : Si vous demandez à PHP_CodeSniffer de vérifier un fichier en particulier à la place d'un répertoire entier, la contrainte sur l'extension sera ignorée. Le fichier sera vérifié même s'il n'a pas l'extension spécifiée ou considérée par défaut.
Dans l'exemple suivant, le fichier main.inc sera vérifié par PHP_CodeSniffer à l'aide de l'argument --extensions qui spécifie que seuls les fichiers .php peuvent être vérifié.
Exemple 58-3. Extension ignored when checking specific file
$ phpcs --extensions=php /path/to/code/main.inc
Note : The ignoring of file extensions for specific files is a feature of PHP_CodeSniffer and is the only way to check files without an extension. If you check an entire directory of files, all files without extensions will be ignored, so you must check each of these file separately.
Parfois vous voudrez que PHP_CodeSniffer s'exécute sur un très grand nombre de fichiers, mais vous voulez ignorer quelques dossiers et fichiers. L'argument de commande --ignore peut être employé pour dire à PHP_CodeSniffer d'ignorer les dossiers et fichiers correspondants à un ou plusieurs modèles.
Dans l'exemple suivant, PHP_CodeSniffer va ignorer tous les fichier à l'intérieur du package se trouvant dans les répertoires tests et data. C'est utile si vous vérifiez un package de PEAR mais sans vouloir vérifier que vos fichiers de test ou de données se conforment à votre norme de codage.
Exemple 58-4. Ignoring test and data files
|
Most of the sniffs written for PHP_CodeSniffer do not support the usage of tabs for indentation and alignment. You can write your own sniffs that check for tabs instead of spaces, but you can also get PHP_CodeSniffer to convert your tabs into spaces before a file is checked. This allows you to use the existing space-based sniffs on your tab-based files.
In the following example, PHP_CodeSniffer will replace all tabs in the files being checked with between 1 and 4 spaces, depending on the column the tab indents to.
Exemple 58-5. Conversion des tabulations en espaces
|
PHP_CodeSniffer can output an XML report to allow you to parse the output easily and use the results in your own scripts. To print an XML report, use the --report=xml command line argument. The output will look like this:
Exemple 58-6. Sample PHP_CodeSniffer XML output
|
As with the full report, you can suppress the printing of warnings with the -n command line argument.
Exemple 58-7. Sample PHP_CodeSniffer XML output with no warnings
|
PHP_CodeSniffer can output an XML report similar to the one produced by Checkstyle, allowing you to use the output in scripts and applications that already support Checkstyle. To print a Checkstyle report, use the --report=checkstyle command line argument. The output will look like this:
Exemple 58-8. Sample PHP_CodeSniffer Checkstyle output
|
As with the full report, you can suppress the printing of warnings with the -n command line argument.
Exemple 58-9. Sample PHP_CodeSniffer Checkstyle output with no warnings
|
PHP_CodeSniffer peut sortir un rapport au format CSV pour permettre un parsing facile et utiliser le résultat dans vos propres scripts. Pour sortir un rapport CSV, utilisez l'argument de ligne de commande --report=csv. L'affichage devrait ressembler à ceci:
Exemple 58-10. Exemple de rapport CSV de PHP_CodeSniffer
|
Comme pour le rapport complet, vous pouvez supprimer l'affichage des warnings avec l'argument -n.
Exemple 58-11. Exemple de rapport CSV de PHP_CodeSniffer sans warnings
|
Note : The first row of the CSV output defines the order of information. When using the CSV output, please parse this header row to determine the order correctly as the format may change over time or new information may be added.
PHP_CodeSniffer has some configuration options that can be set. Individual coding standards may also require configuration options to be set before functionality can be used. View a full list of configuration options.
To set a configuration option, use the --config-set command line argument.
Exemple 58-12. Setting a configuration option
|
PHP_CodeSniffer permet de déactiver une option de configuration, pour lui faire reprendre sa valeur par défaut. View a full list of configuration options.
Pour déactiver une option de configuration, utilisez l'argument --config-delete.
Exemple 58-13. Déactiver une option de configuration
|
To view the currently set configuration options, use the --config-show command line argument.
Exemple 58-14. Viewing configuration options
|
Note : Cette fonctionnalité est proposée seulement pour debugger. L'utilisation de cette fonctionnalité augmente terriblement la taille de ce qui sera affiché et le temps d'éxécution du script.
PHP_CodeSniffer propose différents niveaux de quantité d'information à afficher. Le second niveau (indiqué par l'argument -vv) affichera toutes les informations affichée au niveau (file specific token and line counts with running times) as well as verbose tokeniser output.
The output of the PHP_CodeSniffer tokeniser shows the step-by-step creation of the scope map and the level map.
Le scope map est plus facile à expliquer avec un exemple. Pour le fichier suivant:
|
L'affichage du scope map sera:
Exemple 58-15. Exemple d'affichage de scope map
|
The scope map output above shows the following pieces of information about the file:
A scope token, if, a été trouvé comme lexème 1 (Notez que le lexème 0 est le tag d'ouverture PHP).
L'ouverture pour l'instruction if, l'accolade ouvrante, a été trouvé au lexème 7.
La fermeture de l'instruction if, l'accolade fermante, a été trouvée au lexème 15.
Tokens 8 - 15 are all included in the scope set by the scope opener at token 7, the open curly brace. This indicates that these tokens are all within the if statement.
The scope map output is most useful when debugging PHP_CodeSniffer's scope map, which is critically important to the successful checking of a file, but is also useful for checking the type of a particular token. For example, if you are unsure of the token type for an opening curly brace, the scope map output shows you that the type is T_OPEN_CURLY_BRACKET and not, for example, T_OPEN_CURLY_BRACE.
La level map est plus facile à expliquer avec un exemple. Pour le fichier suivant:
|
L'affichage du level map est:
Exemple 58-16. Exemple d'affichage de level map
|
The level map output above shows the following pieces of information about the file:
A scope opener, an open curly brace, was found at token 7 and opened the scope for an if statement, defined at token 1.
Les lexèmes 8 - 15 are all included in the scope set by the scope opener at token 7, the open curly brace. All these tokens are at level 1, indicating that they are enclosed in 1 scope condition, and all these tokens are enclosed in a single condition; an if statement.
The level map is most commonly used to determine indentation rules (eg. a token 4 levels deep requires 16 spaces of indentation) or to determine if a particular token is within a particular scope (eg. a function keyword is within a class scope, making it a method).
Note : Cette fonctionnalité est juste fournie pour du debuging L'utilisation de cette fonctionnalité augmente terriblement le temps d'éxécution du script et la taille de l'affichage généré.
PHP_CodeSniffer dispose de plusieurs niveau de détails. Niveau 3 (indicated by the command line argument -vvv) will print all verbosity information for Niveau 1 (file specific token and line counts with running times), Niveau 2 (tokeniser output) as well as token processing output with sniff running times.
Le traitement des lexèmes est aussi plus facile à expliquer par l'exemple. For the following file:
|
The token processing output is:
Exemple 58-17. Sample token processing output
|
Every token processed is shown, along with its ID, type and contents. For each token, all sniffs that were executed on the token are displayed, along with the running time.
For example, the output above shows us that token 1, an if keyword, had 3 sniffs executed on it; the ControlSignature sniff, the ScopeClosingBrace sniff and the ScopeIndent sniff. Each was executed fairly quickly, but the slowest was the ScopeClosingBrace sniff, taking 0.0248 seconds to process that token.
The other interesting piece of information we get from the output above is that only 2 tokens in the whole file had sniffs executed on them; tokens 0 and 1. This is normal behavior for PHP_CodeSniffer as most sniffs listen for a very specific and rarely used token and then execute on it and a number of tokens following it.
For example, the ScopeIndentSniff executes on the if statement's token only, but actually checks the indentation of every line within the if statement. The sniff uses the scope map to find all tokens within the if statement.
| Utilisation (Previous) | (Next) Configuration Options | ||||||||
| |
|||||||||
|
|||||||||