تجاهل قصاصات الرمز في php_codesniffer
-
29-09-2019 - |
سؤال
من الممكن تجاهل بعض أجزاء الكود من ملف PHP عند تحليله بواسطة PHP_CodeSniffer
?
المحلول
نعم ، من الممكن مع التعليقات التوضيحية @codingStandArdardsInsorStart
<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();
يوصف أيضا في الوثائق.
نصائح أخرى
يمكنك إما استخدام المجموعة: @codingStandardsIgnoreStart
و @codingStandardsIgnoreEnd
أو يمكنك استخدام @codingStandardsIgnoreLine
.
مثال:
<?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
قبل الإصدار 3.2.0 ، استخدم php_codesniffer بناء جملة مختلف لتجاهل أجزاء من الكود من الملف. انظر Anti Veeranna's و مارتن فسيكا إجابات. ستتم إزالة بناء الجملة القديم في الإصدار 4.0
يستخدم php_codesniffer الآن // phpcs:disable
و // phpcs:enable
تعليقات لتجاهل أجزاء من الملفات و // phpcs:ignore
لتجاهل سطر واحد.
الآن ، من الممكن أيضًا تعطيل أو تمكين رموز رسائل خطأ محددة أو شم أو فئات من شم الشم أو معايير الترميز بأكملها. يجب عليك تحديدهم بعد التعليقات. إذا لزم الأمر ، يمكنك إضافة ملاحظة تشرح سبب تعطيل الشم وإعادة تمكينه باستخدام --
فاصل.
<?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
لمزيد من التفاصيل انظر وثائق php_codesniffer.