symplify/phpstan-extensions

'stats' and 'ignore' error format, solid Symfony SplFileInfo and other useful extensions for PHPStan.

Fund package maintenance!
tomasvotruba

Installs: 284 817

Dependents: 11

Suggesters: 0

Security: 0

Stars: 17

Watchers: 2

Forks: 1


README

Downloads total

Install

composer require symplify/phpstan-extensions --dev

Update config:

# phpstan.neon
includes:
    - 'vendor/symplify/phpstan-extensions/config/config.neon'

Use

Symplify Error Formatter

Works best with anthraxx/intellij-awesome-console

  • Do you want to click the error and get right to the line in the file it's reported at?
  • Do you want to copy-paste regex escaped error to your ignoreErrors?
vendor/bin/phpstan analyse src --level max --error-format symplify

------------------------------------------------------------------------------------------
src/Command/ReleaseCommand.php:51
------------------------------------------------------------------------------------------
- "Call to an undefined method Symplify\\Command\\ReleaseCommand\:\:nonExistingCall\(\)"
------------------------------------------------------------------------------------------

The config also loads few return type extensions.

Return Type Extensions

Symplify\PHPStanExtensions\Symfony\Type\ContainerGetTypeExtension

With Symfony container and type as an argument, you always know the same type is returned:

<?php

use Symfony\Component\DependencyInjection\Container;

/** @var Container $container */
$container->get(Type::class); // PHPStan: object ❌
$container->get(Type::class); // Reality: Type ✅

// same for in-controller/container-aware context
$this->get(Type::class);

Symplify\PHPStanExtensions\Symfony\Type\KernelGetContainerAfterBootReturnTypeExtension

After Symfony Kernel boot, getContainer() always returns the container:

<?php

use Symfony\Component\HttpKernel\Kernel;

final class AppKernel extends Kernel
{
    // ...
}

$kernel = new AppKernel('prod', false);
$kernel->boot();

$kernel->getContainer(); // PHPStan: null|ContainerInterface ❌
$kernel->getContainer(); // Reality: ContainerInterface ✅

Symplify\PHPStanExtensions\Symfony\Type\SplFileInfoTolerantDynamicMethodReturnTypeExtension

Symfony Finder finds only existing files (obviously), so the getRealPath() always return string:

<?php

use Symfony\Component\Finder\Finder;

$finder = new Finder;

foreach ($finder as $fileInfo) {
    $fileInfo->getRealPath(); // PHPStan: false|string ❌
    $fileInfo->getRealPath(); // Reality: string ✅
}