gyro/coding-standard
Gyro Coding Standard is a PHP coding-standard package forked from doctrine/coding-standard, incorporating Tideways’ style changes. Use it to enforce consistent code formatting and quality rules across your projects via automated tooling.
Installation Add the package via Composer in your Laravel project:
composer require --dev gyro/coding-standard
Ensure phpcs is installed globally or via Composer:
composer global require squizlabs/php_codesniffer
First Run Run PHPCS against your project’s root directory:
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard
Or alias it in composer.json for convenience:
"scripts": {
"cs-check": "phpcs --standard=./vendor/gyro/coding-standard --report=full src/"
}
Execute with:
composer cs-check
First Use Case Integrate into your CI pipeline (e.g., GitHub Actions) to enforce standards pre-merge:
- name: Run Coding Standard
run: composer cs-check
Pre-Commit Hooks
Use phpcs in a pre-commit hook (e.g., via Husky) to block non-compliant commits:
composer require --dev laravel/pint # Optional: Auto-fix with Pint
Example hook script:
#!/bin/sh
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard --report=summary src/ || exit 1
IDE Integration Configure your IDE (PHPStorm/VSCode) to use the Gyro standard:
Settings > Editor > Inspections > PHP > Coding Style > PHP > Configure > Custom Profile (point to the standard).settings.json:
"intelephense.codingStandards": ["./vendor/gyro/coding-standard"]
Laravel-Specific Rules Extend the standard for Laravel conventions (e.g., route naming, service container binding):
composer require --dev friendsofphp/php-cs-fixer
Create a custom rule set in phpcs.xml:
<config name="installed_paths" value="./vendor/gyro/coding-standard,./vendor/friendsofphp/php-cs-fixer"/>
Team Onboarding
Document the standard in your CONTRIBUTING.md:
## Coding Standards
Run `composer cs-check` before submitting PRs. Fix issues with:
```bash
composer cs-fix # If using PHP-CS-Fixer
Rule Conflicts
.phpcs.xml:
<rule ref="Generic.Files.LineEndings" severity="warning">
<properties>
<property name="lineEnding" value="LF"/>
</properties>
</rule>
PSR12 vs. Gyro’s property hooks syntax (see PR #1).Performance
vendor/ and node_modules/ from checks to speed up runs:
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard --ignore=vendor/,node_modules/ src/
phpcs --cache-file=.phpcs.cache.PHP Version Mismatch
composer require php:^8.0
Verbose Output
Use --verbose to diagnose rule failures:
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard --verbose src/
Look for FILE:LINE -- [TYPE] Found X messages.
Rule-Specific Help List available rules:
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard --list-rules
Get help for a specific rule (e.g., Generic.Files.LineEndings):
vendor/bin/phpcs --standard=./vendor/gyro/coding-standard --help=Generic.Files.LineEndings
Custom Rules Add project-specific rules by extending the standard:
composer require --dev phpmd/phpmd
Create CustomRuleset.xml:
<ruleset>
<rule ref="./vendor/gyro/coding-standard"/>
<rule ref="custom-rules.xml"/>
</ruleset>
Property Hooks Syntax
The standard supports both #[Property] and @property syntax. Enforce consistency in phpcs.xml:
<rule ref="Gyro.PropertyHooks.Syntax">
<properties>
<property name="preferred" value="verbose"/> <!-- or "regular" -->
</properties>
</rule>
Autofix Integration
Pair with php-cs-fixer for auto-correction:
composer require --dev friendsofphp/php-cs-fixer
Configure in .php-cs-fixer.dist.php:
return (new PhpCsFixer\Config())
->setRules([
'@Gyro' => true,
'array_syntax' => ['syntax' => 'short'],
]);
How can I help you explore Laravel packages today?