larastrict/conventions

LaraStrict conventions for your Laravel apps - PHPStan, Easy Coding Standard, RectorPHP and appropriate rules.

v1.0.3 2024-10-21 18:00 UTC

This package is auto-updated.

Last update: 2024-12-21 18:30:07 UTC


README

.github/banner.webp

Enhance the robustness and consistency of your Laravel applications with LaraStrict conventions. This package integrates essential tools like PHPStan, Easy Coding Standard, RectorPHP, and tailored rules to streamline your development process.

This package extends the StrictPHP conventions.

Key Features

This package bundles a selection of powerful tools and configurations to support your development workflow:

  • PHPStan Configuration and Custom Rules: Integrates PHPStan, with pre-defined configurations and rules tailored for our projects.
  • Easy Coding Standard Configuration: Utilizes Easy Coding Standard for enforcing consistent coding styles and practices.
  • PHPUnit 10/11 Support: Ensures compatibility with PHPUnit to facilitate comprehensive testing.
  • RectorPHP Configuration: Provides configurations for RectorPHP for better code quality.
  • Extended PHPStan Packages: Includes additional packages to augment PHPStan's capabilities. For more details, see the included composer.json file.

Prerequisites

Before installation, ensure your environment meets these requirements:

  • PHP version 8.1 or higher (based on your Laravel version).
  • Composer
  • Laravel 10+
  • Enabled Extension installer for PHPStan, which is automatically installed by our package to allow extension discovery.

Only latest major version of this package is maintained.

Installation

To integrate LaraStrict Conventions into your project, run the following command in your terminal:

composer require larastrict/conventions --dev

This installs the package as a development dependency. During installation, you'll be prompted to confirm the installation of the plugin:

Do you trust "phpstan/extension-installer" to execute code and wish to enable it now? (yes/no) [y]:

Type y to use all provided extensions.

Getting Started

After installation, you can tailor the provided configurations to meet your project's requirements.

Setting Up Easy Coding Standard

To use Easy Coding Standard, create or update an ecs.php file at your project's root with the following setup:

<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withRootFiles()
    ->withPaths(\LaraStrict\Conventions\ExtensionFiles::paths(baseDir: __DIR__, laravel11Structure: true))
    // This is required to include the StrictPHP Conventions
    ->withSets([\LaraStrict\Conventions\ExtensionFiles::Ecs]);

Check extension-ecs.php to see what is included:

  • Do not run ecs on blade files.

Configuring RectorPHP

For integrating RectorPHP, add or update a rector.php file in your project's root with the following configuration:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withRootFiles()
    ->withPaths(\LaraStrict\Conventions\ExtensionFiles::paths(baseDir: __DIR__, laravel11Structure: true))
    // This is required to include the StrictPHP Conventions
    ->withSets([\LaraStrict\Conventions\ExtensionFiles::Rector]);

Check extension-rector.php to see what is included:

  • Prevent rector changing callable in routes.
  • Do not run rector on blade files.

Integrating PHPStan

Ensure you use the PHPStan extension installer to install the required extensions.

PHPStan Configuration

Your phpstan should look like this:

parameters:

    level: max

    paths:
        - app
        - config
        - database/migrations
        - database/seeders
        - routes
        - tests
        - ecs.php
        - rector.php
        # Laravel 11 structure (comment if different)
        - bootstrap/app.php
        - bootstrap/providers.php

    #ignoreErrors:
    #    - identifier: missingType.iterableValue

Rules

The package includes the following rules:

UsableInContainerRule

Customization does not work yet. Currently disabled

The package includes rules for ensuring DI classes/contracts are used appropriately:

  • Ensures Contracts are Injectable: Checks if an implementation is registered for the contract.
  • Ensures Classes are Injectable: Checks if a registered class has injectable dependencies.

You can customize these settings as needed. Example configuration:

services:
    -
        class: LaraStrict\Conventions\PHPStan\UsableInContainerRule
        tags: [phpstan.rules.rule]
        arguments:
            enabled: true
            # Ensure that classes that extends these classes are injectable
            extends:
                - Illuminate\Console\Command
                - Illuminate\Database\Seeder
            # Do not check if classes that has these suffixes are injectable
            excludeSuffixes:
                - MySuffix
            # Check classes in these namespaces
            namespaces:
                - \DIClasses\
            # Do not check if classes file contains these folders
            excludeFolders:
                - my_folder

Default settings can be found in ./extension.neon file

Acknowledgement

This project owes its existence to the generous support of several other impactful projects:

Explore more of our open-source initiatives:

  • Larastrict - Enhancing the Laravel Framework with a suite of convenient tools and packages.
  • StrictPHP - Enabling strictness in PHP projects through a curated set of packages and conventions, fostering the development of robust production-grade applications.
  • WrkFlow - Streamlining development workflows with a comprehensive set of tools designed to boost efficiency.

License

Open-source software licensed under the MIT License. Feel free to use and modify it according to your needs.