soyhuce/phpstan-extension

Extra rules for phpstan analysis

Fund package maintenance!
soyhuce

1.1.0 2024-04-19 06:58 UTC

This package is auto-updated.

Last update: 2024-11-08 07:49:42 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

Strict rules for PHPStan and helpers for Laravel

Installation

You can install the package via composer:

composer require --dev soyhuce/phpstan-extension

If you use phpstan/extension-installer, the extension is automatically installed and you're all set.

Otherwise, you need to add the extension in your phpstan.neon file:

includes:
  - ./vendor/soyhuce/phpstan-extension/extension.neon

If you want only a subset of the rules, check the /vendor/soyhuce/phpstan-extension/extension.neon file and copy the rules you want to use.

Rules

CarbonCopyRule

Forbids usage of \Carbon\CarbonInterface::copy() because it is probably linked to usage of a mutable DateTime.

<?php

$datetime->copy()->addDay(); // ko
$datetime->addDay(); // ok

NoAliasUseRule

Forbids usage of Laravel aliases and suggests to use the real class name instead.

<?php

use Auth; // ko
use Illuminate\Support\Facades\Auth; // ok

NoMutableDateTimeStaticCallRule

Forbids usage of static methods on \DateTime and its child classes.

<?php

\Illuminate\Support\Carbon::create($year, $month, $day); // ko
\Illuminate\Support\Facade\Date::create($year, $month, $day); // ok
\Carbon\CarbonImmutable::create($year, $month, $day); // ok

NoMutableDateTimeUseRule

Forbids import of \DateTime and its child classes.

<?php

use Illuminate\Support\Carbon; // ko
use Carbon\Carbon; // ko
use Carbon\CarbonInterface; // ok
use Carbon\ImmutableCarbon; // ok

NoNewMutableDateTimeRule

Forbids usage of new \DateTime() and its child classes.

<?php

$dateTime = new Illuminate\Support\Carbon($date); // ko
$dateTime = new Carbon\Carbon($date); // ko
$dateTime = new Carbon\ImmutableCarbon($date); // ok

Extensions

RequestDateExtension

Provides return type for \Illuminate\Support\Request::date() method.

use Carbon\FactoryImmutable;
use Illuminate\Support\Facades\Date;

Date::use(FactoryImmutable::class);
$request->date('published_at'); // CarbonImmutable|null

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.