Installing Required Tools on CentOS
Before configuring code review in PhpStorm, ensure your CentOS system has the necessary dependencies installed. Start by installing PHP and essential extensions (e.g., php-cli, php-xml, php-json) using yum:
sudo yum install php php-cli php-xml php-json
Verify PHP installation with php -v. Next, install Composer (a PHP dependency manager) to manage code analysis tools globally.
Installing Code Analysis Tools via Composer
PhpStorm integrates with popular static analysis tools to enforce coding standards and detect issues. Install the following tools globally using Composer:
composer global require "squizlabs/php_codesniffer=*"
composer global require "phpmd/phpmd=*"
composer global require "phpstan/phpstan"
composer global require "vimeo/psalm"
After installation, confirm the tools are accessible by checking their paths (e.g., ~/.composer/vendor/bin/phpcs).
Configuring PhpStorm for Code Review
File → Settings → Languages & Frameworks → PHP. Click the ... button next to “CLI Interpreter” and select the PHP binary (e.g., /usr/bin/php). Click Apply to save.Settings → Languages & Frameworks → PHP → Code Sniffer. Click the Configuration button, browse to the phpcs path (e.g., ~/.composer/vendor/bin/phpcs), and click Validate to confirm. In Editor → Inspections, expand the PHP node, check PHP Code Sniffer Validation, and select a ruleset (e.g., PSR2) from the dropdown.Settings → Languages & Frameworks → PHP → Quality Tools → PHP Mess Detector. Click Configuration, select the phpmd path (e.g., ~/.composer/vendor/bin/phpmd), and validate. Enable PHPMD in inspections to run it alongside phpcs.Editor → Inspections and adjust severity levels (e.g., mark “Undefined variable” as an error). Use Code → Inspect Code to run manual checks on the entire project or selected files.Running and Managing Code Reviews
Code → Inspect Code. Choose a profile (e.g., “PHP Code Sniffer”) and click OK. Results appear in the Inspection Results panel, where you can view issues by severity (errors, warnings) and apply fixes (e.g., “Fix all” for automatic corrections).Alt+Enter to apply quick fixes (e.g., adding missing docblocks).Settings → Version Control → Git) to track changes. Use Commit → Commit and Inspect Code to run inspections before committing, ensuring only compliant code is pushed.Optional: Customizing Rulesets
For project-specific standards, create a custom ruleset XML file (e.g., custom-ruleset.xml) and reference it in PhpStorm’s Code Sniffer/PHPMD settings. This allows you to enforce additional rules (e.g., banning eval()) or disable default ones.