Frage

Es ist möglich, einige Teile des Codes aus einer PHP-Datei zu ignorieren, wenn sie von PHP_CodeSniffer analysiert wird?

War es hilfreich?

Lösung

Ja, es ist möglich, mit @codingStandardsIgnoreStart und @codingStandardsIgnoreEnd Anmerkungen

<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();

Es ist auch in der Dokumentation.

Andere Tipps

Sie können entweder die Kombination verwenden: @codingStandardsIgnoreStart und @codingStandardsIgnoreEnd oder Sie können a href verwenden <= "https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/File.php#L1630" rel = "noreferrer „> @codingStandardsIgnoreLine .

Beispiel:

<?php

command1();
// @codingStandardsIgnoreStart
command2(); // this line will be ignored by Codesniffer
command3(); // this one too
command4(); // this one too
// @codingStandardsIgnoreEnd

command6();

// @codingStandardsIgnoreLine
command7(); // this line will be ignored by Codesniffer

Vor der Version 3.2.0 verwendet PHP_CodeSniffer andere Syntax für Teile des Codes aus der Datei zu ignorieren. Sehen Sie sich die Anti Veeranna des und Martin Všeticka der Antworten. Die alte Syntax wird in der Version 4.0

entfernt werden

PHP_CodeSniffer ist jetzt mit // phpcs:disable und // phpcs:enable Kommentare Teile von Dateien und // phpcs:ignore ignorieren eine Zeile zu ignorieren.

Nun ist es auch möglich, nur deaktivieren oder aktivieren spezifische Fehlermeldung Codes, schnüffelt, Kategorien von schnüffelt oder ganze Kodierungsstandards. Sie sollten sie nach Kommentaren angeben. Bei Bedarf können Sie eine Notiz hinzufügen, zu erklären, warum schnüffelt werden deaktivieren und wieder aktiviert, indem der -- Separator verwendet wird.

<?php

/* Example: Ignoring parts of file for all sniffs */
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable

/* Example: Ignoring parts of file for only specific sniffs */
// phpcs:disable Generic.Commenting.Todo.Found
$xmlPackage = new XMLPackage;
$xmlPackage['error_code'] = get_default_error_code_value();
// TODO: Add an error message here.
$xmlPackage->send();
// phpcs:enable

/* Example: Ignoring next line */
// phpcs:ignore
$foo = [1,2,3];
bar($foo, false);

/* Example: Ignoring current line */
$foo = [1,2,3]; // phpcs:ignore
bar($foo, false);

/* Example: Ignoring one line for only specific sniffs */
// phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
$foo = [1,2,3];
bar($foo, false);

/* Example: Optional note */ 
// phpcs:disable PEAR,Squiz.Arrays -- this isn't our code
$foo = [1,2,3];
bar($foo,true);
// phpcs:enable PEAR.Functions.FunctionCallSignature -- check function calls again
bar($foo,false);
// phpcs:enable -- this is out code again, so turn everything back on

Weitere Informationen finden Sie unter PHP_CodeSniffer Dokumentation .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top