composer require symplify/phpstan-extensions --devUpdate your phpstan.neon config:
parameters:
errorFormat: symplify- 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?
Works best with anthraxx/intellij-awesome-console
vendor/bin/phpstan analyse src↓
------------------------------------------------------------------------------------------
src/Command/ReleaseCommand.php:51
------------------------------------------------------------------------------------------
- "Call to an undefined method Symplify\\Command\\ReleaseCommand\:\:nonExistingCall\(\)"
------------------------------------------------------------------------------------------With Symfony container and type as an argument, you always know the same type is returned:
use Symfony\Component\DependencyInjection\Container;
/** @var Container $container */
// PHPStan: object ❌
$container->get(Type::class);
// Reality: Type ✅
$container->get(Type::class);
// same for in-controller/container-aware context
$this->get(Type::class);After Symfony Kernel boot, getContainer() always returns the container:
use Symfony\Component\HttpKernel\Kernel;
final class AppKernel extends Kernel
{
// ...
}
$kernel = new AppKernel('prod', false);
$kernel->boot();
// PHPStan: null|ContainerInterface ❌
$kernel->getContainer();
// Reality: ContainerInterface ✅
$kernel->getContainer();
// Reality: ContainerInterface ✅Symfony Finder finds only existing files (obviously), so the getRealPath() always return string:
use Symfony\Component\Finder\Finder;
$finder = new Finder();
foreach ($finder as $fileInfo) {
// PHPStan: false|string ❌
$fileInfo->getRealPath();
// Reality: string ✅
$fileInfo->getRealPath();
}Happy coding!