Phpcs la gi

Static code analysis tools

  1. PHP_CodeSniffer

Tool phổ biến và thiết yếu nhất có lẽ làPHP_CodeSniffer (phpcs), giúp đảm bảo convention của dự án. Nó cung cấp 2 công cụ chính:

  • PHPCS: được dùng để tìm ra những lỗi sai coding convention
  • PHPCBF: tự động fix một lỗi convention

Tại Sun* cũng có một bộ quy tắc coding convention mở rộng cho PHP được viết như là một standard của PHPCS. Gần đây, chúng ta vừa có một vài thay đổi về cách cài đặt standard nói trên cho PHPCS. Sunners có thể cài trực tiếp cho từng project thay vì phải cài PHPCS globally, chi tiết đã được cập nhật tại đây. 

2. PHP Coding Standards Fixer

PHP Coding Standards Fixer (php-cs-fixer)được dùng để fix tự động các lỗi convention thường gặp với tính năng ương tự với toolPHPCBF ở trên, nhưng hoạt động tốt hơn. Có thể bạn đã từng thấy file .php_cs trong folder dự án, đây chính là file cấu hình (xem ví dụ tại đây).

3. PHPMess Detector

PHP Mess Detector (phpmd) có thể phát hiện ra rất nhiều vấn tiềm tàng trong mã nguồn như:

  • Phát hiện lỗi tiềm ẩn.
  • Tối ưu code.
  • Phát hiện các đoạn code quá phức tạp.
  • Phát hiện các tham số thừa, thuộc tính, phương thức không được sử dụng đến.

4. PHP Copy/Paste Detector

PHP Copy/Paste Detector (phpcpd) được dùng để phát hiện những đoạn code trùng lặp trong các file, hay nghi vấn là do copy / paste từ file này sang file khác, có thể vi phạm nguyên tắc DRY.

Ngoài ra, có 2 tool được nhắc đến rất nhiều trong thời gian gần đây đó làPHPStanPsalm với các chức năng như:

  • Kiểm tra syntax
  • Kiếm tra sự tồn tại của classes, methods, functions và constants
  • Kiểm tra sự tồn tại của biến
  • Kiểm tra tham số hoặc biến không được dùng đến
  • Kiểm tra đúng kiểu dữ liệu (strict type)

Xem thêm danh sách các tool khác tại đây.

Với các tool này, bạn hoàn toàn có thể sử dụng trước khi đẩy code lên CI. Việc này có thể được làm thủ công hoặc tích hợp vào IDE, Code Editor hay sử dụng git hooks để check trước khi commit hoặc trước mỗi khi push code lên remote repository (Với PHP project có thể sử dụng https://github.com/captainhookphp/captainhook để tích hợp git hook bằng composer).

About

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

Phpcs la gi

Requirements

PHP_CodeSniffer requires PHP version 5.4.0 or greater, although individual sniffs may have additional requirements such as external applications and scripts. See the Configuration Options manual page for a list of these requirements.

If you're using PHP_CodeSniffer as part of a team, or you're running it on a CI server, you may want to configure your project's settings using a configuration file.

Installation

The easiest way to get started with PHP_CodeSniffer is to download the Phar files for each of the commands:

# Download using curl curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar # Or download using wget wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar # Then test the downloaded PHARs php phpcs.phar -h php phpcbf.phar -h

Composer

If you use Composer, you can install PHP_CodeSniffer system-wide with the following command:

composer global require "squizlabs/php_codesniffer=*"

Make sure you have the composer bin dir in your PATH. The default value is ~/.composer/vendor/bin/, but you can check the value that you need to use by running composer global config bin-dir --absolute.

Or alternatively, include a dependency for squizlabs/php_codesniffer in your composer.json file. For example:

{ "require-dev": { "squizlabs/php_codesniffer": "3.*" } }

You will then be able to run PHP_CodeSniffer from the vendor bin directory:

./vendor/bin/phpcs -h ./vendor/bin/phpcbf -h

Phive

If you use Phive, you can install PHP_CodeSniffer as a project tool using the following commands:

phive install phpcs phive install phpcbf

You will then be able to run PHP_CodeSniffer from the tools directory:

./tools/phpcs -h ./tools/phpcbf -h

PEAR

If you use PEAR, you can install PHP_CodeSniffer using the PEAR installer. This will make the phpcs and phpcbf commands immediately available for use. To install PHP_CodeSniffer using the PEAR installer, first ensure you have installed PEAR and then run the following command:

pear install PHP_CodeSniffer

Git Clone

You can also download the PHP_CodeSniffer source and run the phpcs and phpcbf commands directly from the Git clone:

git clone https://github.com/squizlabs/PHP_CodeSniffer.git cd PHP_CodeSniffer php bin/phpcs -h php bin/phpcbf -h

Getting Started

The default coding standard used by PHP_CodeSniffer is the PEAR coding standard. To check a file against the PEAR coding standard, simply specify the file's location:

phpcs /path/to/code/myfile.php

Or if you wish to check an entire directory you can specify the directory location instead of a file.

phpcs /path/to/code-directory

If you wish to check your code against the PSR-12 coding standard, use the --standard command line argument:

phpcs --standard=PSR12 /path/to/code-directory

If PHP_CodeSniffer finds any coding standard errors, a report will be shown after running the command.

Full usage information and example reports are available on the usage page.

Documentation

The documentation for PHP_CodeSniffer is available on the Github wiki.

Issues

Bug reports and feature requests can be submitted on the Github Issue Tracker.

Contributing

See CONTRIBUTING.md for information.

Versioning

PHP_CodeSniffer uses a MAJOR.MINOR.PATCH version number format.

The MAJOR version is incremented when:

  • backwards-incompatible changes are made to how the phpcs or phpcbf commands are used, or
  • backwards-incompatible changes are made to the ruleset.xml format, or
  • backwards-incompatible changes are made to the API used by sniff developers, or
  • custom PHP_CodeSniffer token types are removed, or
  • existing sniffs are removed from PHP_CodeSniffer entirely

The MINOR version is incremented when:

  • new backwards-compatible features are added to the phpcs and phpcbf commands, or
  • backwards-compatible changes are made to the ruleset.xml format, or
  • backwards-compatible changes are made to the API used by sniff developers, or
  • new sniffs are added to an included standard, or
  • existing sniffs are removed from an included standard

NOTE: Backwards-compatible changes to the API used by sniff developers will allow an existing sniff to continue running without producing fatal errors but may not result in the sniff reporting the same errors as it did previously without changes being required.

The PATCH version is incremented when:

  • backwards-compatible bug fixes are made

NOTE: As PHP_CodeSniffer exists to report and fix issues, most bugs are the result of coding standard errors being incorrectly reported or coding standard errors not being reported when they should be. This means that the messages produced by PHP_CodeSniffer, and the fixes it makes, are likely to be different between PATCH versions.