Hướng dẫn php code sniffer custom rules - quy tắc tùy chỉnh trình duyệt mã php

PHP_CodesNiffer cho phép các nhà phát triển thiết kế các tiêu chuẩn mã hóa của riêng họ bằng cách tạo một tệp XML quy tắc đơn giản, cả hai đều thu hơi từ các tiêu chuẩn hiện có và tùy chỉnh chúng cho nhu cầu của nhà phát triển. Tệp XML này có thể được đặt tên bất cứ thứ gì bạn thích, miễn là nó có tiện ích mở rộng xml và tuân thủ định dạng quy tắc.xml. Tệp có thể được lưu trữ ở bất cứ đâu, làm cho nó hoàn hảo để đặt dưới điều khiển phiên bản với mã nguồn và kiểm tra đơn vị của dự án.

Sau khi được tạo, một tệp quy tắc có thể được sử dụng với đối số dòng lệnh --standard. Trong ví dụ sau, PHP_CodesNiffer sẽ sử dụng tiêu chuẩn mã hóa được xác định trong tệp quy tắc tùy chỉnh có tên Custom_ruleset.xml:

$ phpcs --standard=/path/to/custom_ruleset.xml test.php

Tệp mẫu được chú thích

Tệp mẫu sau đây ghi lại định dạng quy tắc.xml và hiển thị cho bạn phạm vi đầy đủ các tính năng mà định dạng hỗ trợ. Tệp chỉ được thiết kế cho mục đích tài liệu và không phải là một tiêu chuẩn mã hóa làm việc.

xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">

 

 
 <description>A custom coding standarddescription>

 
 <config name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>


 <file>./path/to/directoryfile>
 <file>./path/to/file.phpfile>

 
 <exclude-pattern>*/tests/*exclude-pattern>
 <exclude-pattern>*/data/*exclude-pattern>


 <exclude-pattern type="relative">^/tests/*exclude-pattern>
 <exclude-pattern type="relative">^/data/*exclude-pattern>

 
 <arg name="extensions" value="php,inc" />
 <arg name="report" value="summary"/>
 <arg name="colors"/>
 <arg value="sp"/>

 
 <ini name="memory_limit" value="64M"/>

 
 <autoload>/path/to/autoload.phpautoload>
 <autoload>/path/to/other/autoload.phpautoload>
 <autoload>./autoload.phpautoload>

 
 <rule ref="PEAR"/>

 
 <rule ref="/home/username/standards/mystandard"/>

 
 <rule ref="/home/username/standards/custom.xml"/>

 
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP"/>
 rule>


 <rule ref="Squiz">
  <exclude name="Generic"/>
 rule>

 
 <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
 <rule ref="Generic.Commenting.Todo"/>
 <rule ref="Generic.ControlStructures.InlineControlStructure"/>

 
 <rule ref="/path/to/standards/Generic/Sniffs/Commenting/TodoSniff.php"/>
 <rule ref="../Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php"/>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %smessage>
  <severity>3severity>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings">
  <type>warningtype>
 rule>
 <rule ref="PSR12">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Files.LineLength.MaxExceeded">
  <message>Line contains %2$s chars, which is more than the limit of %1$smessage>
 rule>
 <rule ref="Generic.Files.LineLength.TooLong">
  <message>Line longer than %s characters; contains %s charactersmessage>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="90"/>
   <property name="absoluteLineLimit" value="100"/>
  properties>
 rule>

 
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  properties>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <properties>
   <property name="maxPadding" value="8"/>
   <property name="ignoreMultiLine" value="true"/>
  properties>
 rule>
 

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array">
    <element key="delete" value="unset"/>
    <element key="print" value="echo"/>
    <element key="create_function" value="null"/>
   property>
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo,create_function=>null" />
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   property>
  properties>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <severity>0severity>
 rule>

 
 <rule ref="Internal.NoCodeFound">
  <severity>0severity>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

  
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

ruleset>

Áp dụng chọn lọc các quy tắc

Tất cả các thẻ trong một tệp quy tắc, ngoại trừ rulesetdescription, có thể được áp dụng có chọn lọc khi một công cụ cụ thể đang được chạy. Hai công cụ có sẵn là phpcs (trình kiểm tra tiêu chuẩn mã hóa) và phpcbf (trình sửa lỗi tiêu chuẩn mã hóa). Các hạn chế được áp dụng bằng cách sử dụng các thuộc tính thẻ phpcs-only

xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">

 

 
 <description>A custom coding standarddescription>

 
 <config name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>


 <file>./path/to/directoryfile>
 <file>./path/to/file.phpfile>

 
 <exclude-pattern>*/tests/*exclude-pattern>
 <exclude-pattern>*/data/*exclude-pattern>


 <exclude-pattern type="relative">^/tests/*exclude-pattern>
 <exclude-pattern type="relative">^/data/*exclude-pattern>

 
 <arg name="extensions" value="php,inc" />
 <arg name="report" value="summary"/>
 <arg name="colors"/>
 <arg value="sp"/>

 
 <ini name="memory_limit" value="64M"/>

 
 <autoload>/path/to/autoload.phpautoload>
 <autoload>/path/to/other/autoload.phpautoload>
 <autoload>./autoload.phpautoload>

 
 <rule ref="PEAR"/>

 
 <rule ref="/home/username/standards/mystandard"/>

 
 <rule ref="/home/username/standards/custom.xml"/>

 
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP"/>
 rule>


 <rule ref="Squiz">
  <exclude name="Generic"/>
 rule>

 
 <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
 <rule ref="Generic.Commenting.Todo"/>
 <rule ref="Generic.ControlStructures.InlineControlStructure"/>

 
 <rule ref="/path/to/standards/Generic/Sniffs/Commenting/TodoSniff.php"/>
 <rule ref="../Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php"/>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %smessage>
  <severity>3severity>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings">
  <type>warningtype>
 rule>
 <rule ref="PSR12">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Files.LineLength.MaxExceeded">
  <message>Line contains %2$s chars, which is more than the limit of %1$smessage>
 rule>
 <rule ref="Generic.Files.LineLength.TooLong">
  <message>Line longer than %s characters; contains %s charactersmessage>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="90"/>
   <property name="absoluteLineLimit" value="100"/>
  properties>
 rule>

 
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  properties>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <properties>
   <property name="maxPadding" value="8"/>
   <property name="ignoreMultiLine" value="true"/>
  properties>
 rule>
 

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array">
    <element key="delete" value="unset"/>
    <element key="print" value="echo"/>
    <element key="create_function" value="null"/>
   property>
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo,create_function=>null" />
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   property>
  properties>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <severity>0severity>
 rule>

 
 <rule ref="Internal.NoCodeFound">
  <severity>0severity>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

  
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

ruleset>
0.

Đặt thuộc tính phpcs-only thành

xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">

 

 
 <description>A custom coding standarddescription>

 
 <config name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>


 <file>./path/to/directoryfile>
 <file>./path/to/file.phpfile>

 
 <exclude-pattern>*/tests/*exclude-pattern>
 <exclude-pattern>*/data/*exclude-pattern>


 <exclude-pattern type="relative">^/tests/*exclude-pattern>
 <exclude-pattern type="relative">^/data/*exclude-pattern>

 
 <arg name="extensions" value="php,inc" />
 <arg name="report" value="summary"/>
 <arg name="colors"/>
 <arg value="sp"/>

 
 <ini name="memory_limit" value="64M"/>

 
 <autoload>/path/to/autoload.phpautoload>
 <autoload>/path/to/other/autoload.phpautoload>
 <autoload>./autoload.phpautoload>

 
 <rule ref="PEAR"/>

 
 <rule ref="/home/username/standards/mystandard"/>

 
 <rule ref="/home/username/standards/custom.xml"/>

 
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP"/>
 rule>


 <rule ref="Squiz">
  <exclude name="Generic"/>
 rule>

 
 <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
 <rule ref="Generic.Commenting.Todo"/>
 <rule ref="Generic.ControlStructures.InlineControlStructure"/>

 
 <rule ref="/path/to/standards/Generic/Sniffs/Commenting/TodoSniff.php"/>
 <rule ref="../Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php"/>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %smessage>
  <severity>3severity>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings">
  <type>warningtype>
 rule>
 <rule ref="PSR12">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Files.LineLength.MaxExceeded">
  <message>Line contains %2$s chars, which is more than the limit of %1$smessage>
 rule>
 <rule ref="Generic.Files.LineLength.TooLong">
  <message>Line longer than %s characters; contains %s charactersmessage>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="90"/>
   <property name="absoluteLineLimit" value="100"/>
  properties>
 rule>

 
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  properties>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <properties>
   <property name="maxPadding" value="8"/>
   <property name="ignoreMultiLine" value="true"/>
  properties>
 rule>
 

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array">
    <element key="delete" value="unset"/>
    <element key="print" value="echo"/>
    <element key="create_function" value="null"/>
   property>
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo,create_function=>null" />
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   property>
  properties>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <severity>0severity>
 rule>

 
 <rule ref="Internal.NoCodeFound">
  <severity>0severity>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

  
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

ruleset>
2 sẽ chỉ áp dụng quy tắc khi công cụ phpcs đang chạy. Quy tắc sẽ không được áp dụng trong khi tệp đang được sửa bằng công cụ phpcbf.

Đặt thuộc tính

xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">

 

 
 <description>A custom coding standarddescription>

 
 <config name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>


 <file>./path/to/directoryfile>
 <file>./path/to/file.phpfile>

 
 <exclude-pattern>*/tests/*exclude-pattern>
 <exclude-pattern>*/data/*exclude-pattern>


 <exclude-pattern type="relative">^/tests/*exclude-pattern>
 <exclude-pattern type="relative">^/data/*exclude-pattern>

 
 <arg name="extensions" value="php,inc" />
 <arg name="report" value="summary"/>
 <arg name="colors"/>
 <arg value="sp"/>

 
 <ini name="memory_limit" value="64M"/>

 
 <autoload>/path/to/autoload.phpautoload>
 <autoload>/path/to/other/autoload.phpautoload>
 <autoload>./autoload.phpautoload>

 
 <rule ref="PEAR"/>

 
 <rule ref="/home/username/standards/mystandard"/>

 
 <rule ref="/home/username/standards/custom.xml"/>

 
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP"/>
 rule>


 <rule ref="Squiz">
  <exclude name="Generic"/>
 rule>

 
 <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
 <rule ref="Generic.Commenting.Todo"/>
 <rule ref="Generic.ControlStructures.InlineControlStructure"/>

 
 <rule ref="/path/to/standards/Generic/Sniffs/Commenting/TodoSniff.php"/>
 <rule ref="../Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php"/>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %smessage>
  <severity>3severity>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings">
  <type>warningtype>
 rule>
 <rule ref="PSR12">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Files.LineLength.MaxExceeded">
  <message>Line contains %2$s chars, which is more than the limit of %1$smessage>
 rule>
 <rule ref="Generic.Files.LineLength.TooLong">
  <message>Line longer than %s characters; contains %s charactersmessage>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="90"/>
   <property name="absoluteLineLimit" value="100"/>
  properties>
 rule>

 
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  properties>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <properties>
   <property name="maxPadding" value="8"/>
   <property name="ignoreMultiLine" value="true"/>
  properties>
 rule>
 

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array">
    <element key="delete" value="unset"/>
    <element key="print" value="echo"/>
    <element key="create_function" value="null"/>
   property>
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo,create_function=>null" />
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   property>
  properties>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <severity>0severity>
 rule>

 
 <rule ref="Internal.NoCodeFound">
  <severity>0severity>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

  
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

ruleset>
0 thành
xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">

 

 
 <description>A custom coding standarddescription>

 
 <config name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>


 <file>./path/to/directoryfile>
 <file>./path/to/file.phpfile>

 
 <exclude-pattern>*/tests/*exclude-pattern>
 <exclude-pattern>*/data/*exclude-pattern>


 <exclude-pattern type="relative">^/tests/*exclude-pattern>
 <exclude-pattern type="relative">^/data/*exclude-pattern>

 
 <arg name="extensions" value="php,inc" />
 <arg name="report" value="summary"/>
 <arg name="colors"/>
 <arg value="sp"/>

 
 <ini name="memory_limit" value="64M"/>

 
 <autoload>/path/to/autoload.phpautoload>
 <autoload>/path/to/other/autoload.phpautoload>
 <autoload>./autoload.phpautoload>

 
 <rule ref="PEAR"/>

 
 <rule ref="/home/username/standards/mystandard"/>

 
 <rule ref="/home/username/standards/custom.xml"/>

 
 <rule ref="./standards/mystandard"/>
 <rule ref="../username/custom.xml"/>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
 rule>

 
 <rule ref="Squiz">
  <exclude name="Squiz.PHP"/>
 rule>


 <rule ref="Squiz">
  <exclude name="Generic"/>
 rule>

 
 <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
 <rule ref="Generic.Commenting.Todo"/>
 <rule ref="Generic.ControlStructures.InlineControlStructure"/>

 
 <rule ref="/path/to/standards/Generic/Sniffs/Commenting/TodoSniff.php"/>
 <rule ref="../Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php"/>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %smessage>
  <severity>3severity>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <type>errortype>
 rule>
 <rule ref="Squiz.Strings">
  <type>warningtype>
 rule>
 <rule ref="PSR12">
  <type>warningtype>
 rule>

 
 <rule ref="Generic.Files.LineLength.MaxExceeded">
  <message>Line contains %2$s chars, which is more than the limit of %1$smessage>
 rule>
 <rule ref="Generic.Files.LineLength.TooLong">
  <message>Line longer than %s characters; contains %s charactersmessage>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="90"/>
   <property name="absoluteLineLimit" value="100"/>
  properties>
 rule>

 
 <rule ref="Generic.Files.LineEndings">
  <properties>
   <property name="eolChar" value="\r\n"/>
  properties>
 rule>

 
 <rule ref="Generic.Formatting.MultipleStatementAlignment">
  <properties>
   <property name="maxPadding" value="8"/>
   <property name="ignoreMultiLine" value="true"/>
  properties>
 rule>
 

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array">
    <element key="delete" value="unset"/>
    <element key="print" value="echo"/>
    <element key="create_function" value="null"/>
   property>
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo,create_function=>null" />
  properties>
 rule>

 
 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   property>
  properties>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
  <severity>0severity>
 rule>

 
 <rule ref="Internal.NoCodeFound">
  <severity>0severity>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <exclude-pattern>*/tests/*exclude-pattern>
    <exclude-pattern>*/data/*exclude-pattern>
 rule>

  
 <rule ref="Squiz.Strings.DoubleQuoteUsage">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

 
 <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
    <include-pattern>*/templates/*include-pattern>
    <include-pattern>*\.tpl$include-pattern>
 rule>

ruleset>
2 sẽ chỉ áp dụng quy tắc khi công cụ phpcbf đang sửa một tệp. Quy tắc sẽ không được áp dụng trong khi tệp đang được kiểm tra với công cụ phpcs.

Tệp mẫu sau đây hiển thị tệp Ruleset.xml sử dụng các quy tắc chọn lọc. Tệp chỉ được thiết kế cho mục đích tài liệu và không phải là một tiêu chuẩn mã hóa làm việc.

xml version="1.0"?>
<ruleset name="Selective Standard">

 
 <config phpcs-only="true" name="zend_ca_path" value="/path/to/ZendCodeAnalyzer"/>

 
 <exclude-pattern phpcbf-only="true">*/3rdparty/*exclude-pattern>

 
 <rule ref="Squiz">
  <exclude phpcbf-only="true" name="Generic.WhiteSpace.ScopeIndent"/>
 rule>

 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <severity phpcbf-only="true">0severity>
 rule>

 
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property phpcs-only="true" name="lineLimit" value="80"/>
   <property phpcbf-only="true" name="lineLimit" value="120"/>
  properties>
 rule>

ruleset>

Sniffer mã PHP làm gì?

Code Sniffer (PHPCS) PHP là một gói để kiểm tra cú pháp, có sẵn từ PEAR.Nó có thể kiểm tra mã chống lại các quy tắc được xác định bao gồm bất cứ điều gì từ khoảng trắng thông qua các nhận xét của DOC đến các quy ước đặt tên biến và hơn thế nữa.check code against defined rules covering anything from whitespace through doc comments to variable naming conventions and beyond.

Làm cách nào để chạy CodeSniffer PHP?

Trong hộp thoại Cài đặt, truy cập Biên tập viên> Kiểm tra.Từ màn hình kiểm tra, mở rộng PHP |Nút công cụ chất lượng và bật xác thực mã PHP của Php.Trong ngăn cấu hình hiện được bật, chọn tùy chỉnh của Google từ danh sách thả xuống Tiêu chuẩn mã hóa, định vị cấu hình quy tắc (PHPCS.

SquizLabs PHP_CodesNiffer là gì?

GitHub - SquizLabs/PHP_CodesNiffer: PHP_CodesNiffer mã hóa các tệp PHP và phát hiện vi phạm một tập hợp các tiêu chuẩn mã hóa được xác định.tokenizes PHP files and detects violations of a defined set of coding standards.