shipmonk/coding-standard
ShipMonk’s PHP_CodeSniffer ruleset for consistent PHP style across ShipMonk packages. Install via Composer, add a phpcs.xml.dist pointing to ShipMonkCodingStandard, then run phpcs to check and phpcbf to auto-fix issues.
Installation
Run composer require --dev shipmonk/coding-standard in your Laravel project to add the package to your devDependencies.
Configuration
Create a phpcs.xml.dist file in your project root (if it doesn’t exist) and include the ShipMonk ruleset as shown in the README. Ensure the <rule ref="ShipMonkCodingStandard"/> line is present.
First Use Case
Run vendor/bin/phpcs in your terminal to analyze your codebase for violations. Start with the src/ and tests/ directories to align with ShipMonk’s conventions.
Pre-Commit Hooks
Integrate phpcs into your Git workflow using a pre-commit hook to enforce standards before code is committed. Example:
# .git/hooks/pre-commit
#!/bin/sh
vendor/bin/phpcs --standard=ShipMonkCodingStandard src/ tests/ || exit 1
CI/CD Pipeline
Add a step in your CI (e.g., GitHub Actions, GitLab CI) to run phpcs and fail the build if violations are found. Example GitHub Actions snippet:
- name: Run PHP_CodeSniffer
run: vendor/bin/phpcs --standard=ShipMonkCodingStandard --warning-severity=0 src/ tests/
Auto-Fixing
Use phpcbf to automatically fix common issues (e.g., indentation, spacing) before code reviews:
vendor/bin/phpcbf --standard=ShipMonkCodingStandard src/
Custom Rulesets
Extend the ShipMonk ruleset by creating a custom phpcs.xml file that overrides specific rules:
<ruleset>
<rule ref="ShipMonkCodingStandard">
<exclude name="PSR12.Methods.MethodDeclaration"/>
</rule>
</ruleset>
Laravel-Specific Adjustments
Exclude Laravel-specific files (e.g., vendor/, bootstrap/) from analysis by adding:
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>bootstrap/</exclude-pattern>
Team Onboarding
Document the coding standard in your CONTRIBUTING.md and include a one-time phpcbf run for new contributors to align their codebase.
Partial Runs Target specific directories or files for faster feedback during development:
vendor/bin/phpcs src/Http/Controllers/
Rule Conflicts
ShipMonk’s ruleset may conflict with other installed coding standards (e.g., slevomat/coding-standard). Ensure only ShipMonk’s ruleset is referenced in phpcs.xml.dist to avoid ambiguity.
False Positives
Some rules (e.g., line length) may flag Laravel-specific constructs (e.g., long controller methods). Use <exclude> or <severity> tags to adjust:
<rule ref="ShipMonkCodingStandard">
<severity name="PSR12.Files.FileOpeningComment.SpacingAfterComment" value="warning"/>
</rule>
Performance
Caching (<arg name="cache" value="var/phpcs.cache"/>) speeds up repeated runs but may cause stale results if the ruleset updates. Clear the cache with:
rm -rf var/phpcs.cache
Auto-Fix Limitations
phpcbf won’t fix all issues (e.g., semantic violations). Review output manually for unresolved warnings.
Verbose Output
Use --verbose to debug rule application:
vendor/bin/phpcs --standard=ShipMonkCodingStandard --verbose src/
Rule Explanation Check the slevomat/coding-standard docs for details on individual rules, as ShipMonk’s ruleset builds on top of it.
Custom Rules Add project-specific rules by extending the ruleset:
<rule ref="ShipMonkCodingStandard">
<config name="custom_rules" value="vendor/your-package/coding-standard"/>
</rule>
Severity Adjustments Temporarily lower severity for specific rules during refactoring:
<rule ref="ShipMonkCodingStandard">
<severity name="PSR12.ControlStructures.SpaceAfterControlStructure" value="warning"/>
</rule>
Sniffs Configuration
Fine-tune individual sniffs (e.g., line length) via the <config> tag:
<config name="line_ending" value="LF"/>
How can I help you explore Laravel packages today?