Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yii2 Coding Standards Laravel Package

yiisoft/yii2-coding-standards

Yii 2 coding standards package providing PHP_CodeSniffer ruleset, sniffs, and tooling configs based on PSR-12 with Yii2-specific tweaks. Use it to enforce Yii2 core style in framework development or in existing Yii2 applications.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require --dev yiisoft/yii2-coding-standards
    

    Add to your project’s composer.json under require-dev if not using globally.

  2. First Run Integrate with PHP_CodeSniffer via:

    ./vendor/bin/phpcs --standard=Yiisoft src/
    

    Verify the default rules align with Yii 2’s conventions (e.g., PSR-12 + Yii-specific sniffs).

  3. Quick Use Case Fix a common Yii 2 issue (e.g., missing @property PHPDoc):

    ./vendor/bin/phpcbf --standard=Yiisoft --ruleset=Yii2 src/models/User.php
    

Implementation Patterns

Workflow Integration

  • CI/CD Hooks Add to .github/workflows/php.yml:

    - name: Run Coding Standards
      run: ./vendor/bin/phpcs --standard=Yiisoft --warning-severity=3 src/
    

    Fail builds on warnings (adjust severity as needed).

  • IDE Setup Configure PHPStorm to use Yiisoft as the default CodeSniffer standard (via Settings > PHP > Code Sniffer).

  • Custom Rulesets Extend defaults by creating a custom .phpcs.xml:

    <config name="standard" value="Yiisoft" />
    <rule ref="Yiisoft">
        <exclude name="Yiisoft.NamingConventions.ValidClassName.PublicProperty" />
    </rule>
    

Common Patterns

  • Model Validation Rules Enforce Yii 2’s rules() method conventions (e.g., ['safe', 'on' => 'scenario']):

    ./vendor/bin/phpcs --standard=Yiisoft src/models/
    
  • Controller Actions Validate action naming (e.g., actionIndex() vs. action_create()):

    ./vendor/bin/phpcs --standard=Yiisoft --extensions=php src/controllers/
    
  • Dependency Injection Check for proper use of Dependency trait or constructor injection:

    ./vendor/bin/phpcs --standard=Yiisoft --ruleset=Yii2.Dependency src/
    

Gotchas and Tips

Pitfalls

  • False Positives

    • Issue: Rules like Yiisoft.NamingConventions.ValidClassName may flag ActiveRecord subclasses incorrectly.
    • Fix: Exclude or adjust in .phpcs.xml:
      <exclude-pattern>*/models/*.php</exclude-pattern>
      
  • Performance

    • Issue: Running on large codebases (e.g., tests/) can be slow.
    • Fix: Use --ignore=tests/ or parallelize with php-parallel-lint.
  • Outdated Rules

    • Issue: Some rules (e.g., Yiisoft.Files.NamingConventions) may conflict with PSR-12.
    • Fix: Override in .phpcs.xml:
      <rule ref="Yiisoft.Files.NamingConventions">
          <severity value="0" />
      </rule>
      

Debugging Tips

  • Verbose Output Use --report=full to see all violations with line numbers:

    ./vendor/bin/phpcs --standard=Yiisoft --report=full src/
    
  • Rule Documentation Check the Yii2 Coding Standards docs for rule specifics (e.g., Yiisoft.CodingStyle vs. Yiisoft.NamingConventions).

Extension Points

  • Custom Sniffs Extend the standard by creating a Sniff class in ruleset.xml:

    <rule ref="Custom.Yiisoft.ExtraRule" />
    
  • Integration with PHPStan Use phpstan-baseline to ignore false positives while keeping CodeSniffer for style:

    ./vendor/bin/phpstan analyse --level=5 --generate-baseline
    ./vendor/bin/phpcs --standard=Yiisoft --ignore=tests/
    
  • Git Hooks Add to .git/hooks/pre-commit:

    #!/bin/sh
    ./vendor/bin/phpcs --standard=Yiisoft --warning-severity=7 --extensions=php $@ || exit 1
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests