tailieunhanh - Expert PHP 5 Tools phần 2

Trong bước trước, chúng tôi đã tạo ra một trình giữ chỗ cho các tập tin lớp học chính trong thư mục ProjectStandard của chúng tôi. Bây giờ chúng ta hãy đặt một số mã trong đó và xác định mình là một tiêu chuẩn mã hóa PHP_CodeSniffer. Các mã hóa tập tin đạt tiêu chuẩn chính | Chapter 1 The main coding standard class file In the previous step we have already created a placeholder for the main class file in our ProjectStandard directory. Now let s put some code in there and identify ourselves as a PHP_CodeSniffer coding standard. php make sure the parent class is in our include path if class_exists PHP_CodeSniffer_Standards_CodingStandard true false throw new PHP_CodeSniffer_Exception Class PHP_CodeSniffer_Standards_CodingStandard not found our main coding standard class definition class PHP_CodeSniffer_Standards_ProjectStandard_ ProjectStandardCodingStandard extends PHP_CodeSniffer_Standards_ CodingStandard include sniffs from other directories or even whole coding standards great way to create your standard and build on it public function getIncludedSniffs return an array of sniffs directories of sniffs or coding standards to include return array Generic exclude sniffs from previously included ones public function getExcludedSniffs return a list of sniffs or directories of sniffs to exclude return array Generic Sniffs 31 Download from Wow eBook Coding Style and Standards Our main class extends PHP_CodeSniffer_Standards_CodingStandard. This is required of all classes identifying a coding standard to be used with phpcs. However the two methods we are implementing getincludedSniffs and getExcludedSniffs are pretty important because they let us assemble our own coding standard from parts of existing standards thus saving us a lot of time since we don t have to write all the sniffs ourselves. Both classes return simple arrays. The items in the array are either names of existing coding standards paths to directories of sniffs of existing coding standards or paths to individual sniff files. For example it turns out that our own coding standard is pretty close to the Generic coding standard included with PHP_CodeSniffer. Therefore to make things easier for us we include the whole Generic coding standard in .