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

Phpstan No Private Laravel Package

swissspidy/phpstan-no-private

PHPStan extension that reports deprecation warnings when code uses “pseudo-private” elements marked with @access private. Helps prevent relying on internal classes, methods, functions, or properties. Easy install via Composer with optional extension-installer support.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

The swissspidy/phpstan-no-private package aligns well with Laravel’s dependency injection (DI) principles and explicit encapsulation patterns. It enforces stricter visibility rules by treating @access private-annotated elements as deprecated, which complements Laravel’s:

  • Service Container: Encourages constructor injection over private property access.
  • Domain-Driven Design (DDD): Prevents accidental coupling by disallowing private method leaks.
  • Testability: Public APIs are easier to mock and verify.

Key Synergies:

  • Laravel’s protected vs. private: The package’s focus on @access private (not protected) avoids conflicts with Laravel’s common use of protected for internal class methods.
  • PHPStan Integration: Leverages Laravel’s growing adoption of static analysis (e.g., laravel-pint, pestphp/pest) to enforce consistency.

Integration Feasibility

  • Greenfield Projects: High feasibility—minimal refactoring needed if following Laravel’s DI patterns.
  • Legacy Projects: Moderate to Low—existing private properties (e.g., in Eloquent models) may require mass refactoring to public getters/setters or dependency injection.
  • Laravel-Specific Challenges:
    • Eloquent Models: Private properties (e.g., $fillable) are common but may violate the rule. Workarounds:
      • Use protected instead of private.
      • Annotate with @access public or @ignore in PHPStan config.
    • Service Providers: Private methods called from container bindings will trigger violations.

Technical Risk

Risk Area Severity Mitigation Strategy
PHPStan 2.0 Breaking Change High Test locally before CI/CD adoption; document upgrade steps.
False Positives Medium Use phpstan.neon exclusions or @ignore annotations.
Refactoring Overhead High Phase adoption (start with new code); use --generate-baseline.
CI/CD Pipeline Impact Medium Cache PHPStan results to reduce build time.
Third-Party Packages Low Ignore vendor directories in PHPStan config.

Key Questions for the TPM

  1. Codebase Maturity:

    • What percentage of classes use private properties/methods? (Target: <20% for smooth adoption.)
    • Are there critical paths (e.g., legacy APIs) that rely on private access?
  2. Tooling Stack:

    • Is PHPStan already used in the team? If so, at what level (e.g., Level 5 vs. Level 10)?
    • Does the CI/CD pipeline support PHPStan 2.0+? (Check GitHub Actions/Pipelines.)
  3. Team Readiness:

    • Has the team adopted Laravel’s DI patterns (e.g., constructor injection)?
    • Is there appetite for refactoring, or should this be a "new code only" rule?
  4. Customization Needs:

    • Are there exceptions (e.g., internal libraries) that should bypass the rule?
    • Should the rule be configurable (e.g., @access private vs. @deprecated)?
  5. Performance:

    • Will PHPStan runs be gated in PRs, or only in CI? (Affects developer velocity.)

Integration Approach

Stack Fit

Laravel Component Fit Level Notes
Service Container High Encourages DI over private property access.
Eloquent Models Medium Private properties (e.g., $fillable) may need refactoring.
Controllers High Private methods called from routes will trigger violations.
Jobs/Commands High Aligns with Laravel’s explicit dependency patterns.
Packages Low Third-party packages may require exclusions.

Migration Path

  1. Preparation Phase:

    • Audit: Run phpstan analyse --generate-baseline to identify violations.
    • Upgrade PHPStan:
      composer require --dev phpstan/phpstan:^2.0
      
    • Update Config:
      # phpstan.neon
      includes:
          - vendor/swissspidy/phpstan-no-private/extension.neon
      parameters:
          level: 8  # Start with Level 8 (strict) to avoid false positives
      
  2. Pilot Phase:

    • New Features: Enforce the rule only in new code (use pathExclusionFilters).
    • Critical Paths: Refactor high-impact modules (e.g., API controllers) first.
  3. Full Adoption:

    • CI/CD Integration:
      # .github/workflows/phpstan.yml
      - name: PHPStan
        run: vendor/bin/phpstan analyse --level=max --error-format=github
      
    • Developer Workflow:
      • Add PHPStan to composer.json scripts:
        "scripts": {
            "test": [
                "phpstan"
            ]
        }
        
      • Use phpstan --generate-baseline to suppress known issues temporarily.

Compatibility

  • Laravel 8+: High compatibility with DI patterns; minimal refactoring needed.
  • Laravel 7.x: Moderate—may require updating to use protected instead of private.
  • Legacy Code:
    • Workarounds:
      • Replace private with protected (less strict but avoids violations).
      • Use public getters/setters for private properties.
      • Annotate exceptions:
        /** @ignore */
        private function legacyMethod() { ... }
        

Sequencing

  1. Phase 1: Configuration (1–2 days)

    • Upgrade PHPStan and install the package.
    • Configure phpstan.neon to exclude non-critical paths.
  2. Phase 2: Pilot (2–4 weeks)

    • Enforce rules in a single module (e.g., API routes).
    • Refactor violations iteratively.
  3. Phase 3: Full Rollout (4–8 weeks)

    • Expand to all new code.
    • Gradually migrate legacy code (prioritize high-value areas).
  4. Phase 4: Enforcement (Ongoing)

    • Treat violations as CI/CD blockers.
    • Deprecate @access private annotations in favor of protected or public APIs.

Operational Impact

Maintenance

  • Pros:
    • Reduced Technical Debt: Encourages explicit APIs, improving maintainability.
    • Consistent Patterns: Aligns with Laravel’s DI and SOLID principles.
  • Cons:
    • Refactoring Burden: Legacy private properties may require mass updates.
    • Configuration Drift: PHPStan rules may need tuning over time (e.g., new exceptions).

Support

  • Developer Onboarding:
    • Training Needed: Explain why private properties are discouraged (e.g., testability, coupling).
    • Documentation: Add a CONTRIBUTING.md section on PHPStan rules.
  • Common Issues:
    • False Positives: Eloquent models or third-party packages may trigger violations.
    • Tooling Confusion: Developers may not understand @access private vs. @ignore.

Scaling

  • Performance:
    • PHPStan Overhead: Adds ~5–15% to CI/CD time (mitigate with caching).
    • Local Dev: Use phpstan --memory-limit=1G to avoid timeouts.
  • Team Size:
    • Small Teams: Easier to coordinate refactoring.
    • Large Teams: Use feature flags or module-by-module adoption.
  • Monorepos:
    • Configure PHPStan per-package (e.g., exclude vendor/ and node_modules/).

Failure Modes

Failure Mode Impact Mitigation
CI/CD Blockers High Start with --error-level=5 (deprecated) before enforcing errors.
Refactoring Fatigue Medium Limit scope (e.g., new code only).
False Positives Low Use excludePaths or @ignore.
Tooling Rejection High Pilot with a small team first.
PHPStan Version Issues Medium Pin PHPStan version in composer.json.

Ramp-Up

  1. Training:

    • Workshop: 1-hour session on PHPStan Level 10 and the package’s goals.
    • Cheat Sheet: Document common fixes (e.g., private → protected, getters/setters).
  2. Pilot Project:

    • Select a low-risk module (e.g., a new feature
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor