eliashaeussler/phpstan-config

PHPStan 的个人配置

2.5.1 2024-08-24 12:00 UTC

README

PHPStan 配置

Coverage Maintainability CGL Tests Supported PHP Versions

本包包含用于个人项目的 PHPStan 基本配置。它不适用于其他地方。我不会提供支持,也不接受此仓库的拉取请求。

🔥 安装

Packagist Packagist Downloads

composer require eliashaeussler/phpstan-config

⚡ 使用

使用扩展安装程序

如果您已安装 phpstan/extension-installer 包,则无需做任何事情。基本配置将自动包含。

手动包含

创建一个 phpstan.neon 文件,并包含 phpstan.neon.dist 文件

# phpstan.neon

includes:
  - %rootDir%/../../eliashaeussler/phpstan-config/phpstan.neon.dist

PHP API

该包为 PHPStan 提供了 PHP 配置 API。将其添加到您的 phpstan.php 文件中

# phpstan.php

use EliasHaeussler\PHPStanConfig;

$config = PHPStanConfig\Config\Config::create(__DIR__)->in(
    'src',
    'tests',
);

// Exclude specific paths
$config->not(
    'src/lib/*',
    'tests/test-application/vendor/*',
);

// Configure rule level
$config->level(9);
$config->maxLevel();

// Enable bleeding edge
$config->withBleedingEdge();

// Include baseline file
$config->withBaseline();

// Include additional config files
$config->with(
    'phpstan-custom-rules.neon',
    'vendor/foo/baz/optional-phpstan-rules.neon',
);

// Define bootstrap files
$config->bootstrapFiles(
    'tests/build/phpstan-bootstrap.php',
);

// Define stub files
$config->stubFiles(
    'tests/stubs/ThirdPartyClass.stub',
    'tests/stubs/AnotherStubFile.stub',
);

// Override cache path
$config->useCacheDir('var/cache/phpstan');

// Ignore errors
$config->ignoreError('Access to constant EXTENSIONS on an unknown class PHPStan\ExtensionInstaller\GeneratedConfig.');
$config->ignoreError('#^Access to constant EXTENSIONS on an unknown class .+\\.$#');

// Configure unmatched error reporting
$config->reportUnmatchedIgnoredErrors(false);

// Define error formatter
$config->formatAs(PHPStanConfig\Enums\ErrorFormat::Json);

// Treat phpdoc types as certain
$config->treatPhpDocTypesAsCertain();

// Include Doctrine set
$doctrineSet = PHPStanConfig\Set\DoctrineSet::create()
    ->withObjectManagerLoader('tests/object-manager.php')
    ->withOrmRepositoryClass(\MyApp\Doctrine\BetterEntityRepository::class)
    ->withOdmRepositoryClass(\MyApp\Doctrine\BetterDocumentRepository::class)
$config->withSets($doctrineSet);

// Include Symfony set
$symfonySet = PHPStanConfig\Set\SymfonySet::create()
    ->withConsoleApplicationLoader('tests/build/console-application.php')
    ->withContainerXmlPath('var/cache/test-container.xml')
    ->disableConstantHassers();
$config->withSets($symfonySet);

// Include TYPO3 set
$typo3Set = PHPStanConfig\Set\TYPO3Set::create()
    ->withCustomAspect('myCustomAspect', \FlowdGmbh\MyProject\Context\MyCustomAspect::class)
    ->withCustomRequestAttribute('myAttribute', \FlowdGmbh\MyProject\Http\MyAttribute::class)
    ->withCustomSiteAttribute('myArrayAttribute', 'array');
$config->withSets($typo3Set);

// Set custom parameters
$config->parameters->set('tipsOfTheDay', false);

return $config->toArray();

⭐ 许可证

本项目受 GNU 通用公共许可证 3.0(或更高版本) 许可。