2022-04-08 18:51:16 +00:00
< ? php
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\Config ;
2022-04-08 18:51:16 +00:00
2022-06-06 17:12:56 +00:00
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface ;
use Rector\Core\Configuration\Option ;
use Rector\Core\Configuration\ValueObjectInliner ;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
use Rector\Core\Contract\Rector\RectorInterface ;
use Rector\Core\ValueObject\PhpVersion ;
2023-06-01 08:56:46 +00:00
use RectorPrefix202306\Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator ;
use RectorPrefix202306\Webmozart\Assert\Assert ;
2022-04-08 18:51:16 +00:00
/**
* @ api
* Same as Symfony container configurator , with patched return type for " set() " method for easier DX .
* It is an alias for internal class that is prefixed during build , so it ' s basically for keeping stable public API .
*/
2022-06-07 08:22:29 +00:00
final class RectorConfig extends ContainerConfigurator
2022-04-08 18:51:16 +00:00
{
/**
2022-04-10 13:02:53 +00:00
* @ param string [] $paths
2022-04-08 18:51:16 +00:00
*/
public function paths ( array $paths ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $paths );
2022-04-08 18:51:16 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PATHS , $paths );
2022-04-08 18:51:16 +00:00
}
2022-04-10 13:02:53 +00:00
/**
* @ param string [] $sets
*/
public function sets ( array $sets ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $sets );
2022-04-10 13:02:53 +00:00
foreach ( $sets as $set ) {
2022-06-07 08:22:29 +00:00
Assert :: fileExists ( $set );
2022-04-10 13:02:53 +00:00
$this -> import ( $set );
}
}
2022-04-12 14:41:09 +00:00
public function disableParallel () : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PARALLEL , \false );
2022-04-12 14:41:09 +00:00
}
2022-04-28 18:36:02 +00:00
public function parallel ( int $seconds = 120 , int $maxNumberOfProcess = 16 , int $jobSize = 20 ) : void
2022-04-10 13:02:53 +00:00
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PARALLEL , \true );
2023-04-17 12:58:24 +00:00
$parameters -> set ( Option :: PARALLEL_JOB_TIMEOUT_IN_SECONDS , $seconds );
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PARALLEL_MAX_NUMBER_OF_PROCESSES , $maxNumberOfProcess );
$parameters -> set ( Option :: PARALLEL_JOB_SIZE , $jobSize );
2022-04-10 13:02:53 +00:00
}
2023-04-10 13:55:06 +00:00
public function noDiffs () : void
{
$parameters = $this -> parameters ();
$parameters -> set ( Option :: NO_DIFFS , \true );
}
2023-04-10 14:35:48 +00:00
public function memoryLimit ( string $memoryLimit ) : void
{
$parameters = $this -> parameters ();
$parameters -> set ( Option :: MEMORY_LIMIT , $memoryLimit );
}
2022-04-10 13:02:53 +00:00
/**
* @ param array < int | string , mixed > $criteria
*/
public function skip ( array $criteria ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: SKIP , $criteria );
2022-04-10 13:02:53 +00:00
}
2023-02-08 15:30:12 +00:00
public function removeUnusedImports ( bool $removeUnusedImports = \true ) : void
{
$parameters = $this -> parameters ();
$parameters -> set ( Option :: REMOVE_UNUSED_IMPORTS , $removeUnusedImports );
}
2022-06-18 22:29:58 +00:00
public function importNames ( bool $importNames = \true , bool $importDocBlockNames = \true ) : void
2022-04-10 17:44:48 +00:00
{
$parameters = $this -> parameters ();
2022-06-18 22:29:58 +00:00
$parameters -> set ( Option :: AUTO_IMPORT_NAMES , $importNames );
$parameters -> set ( Option :: AUTO_IMPORT_DOC_BLOCK_NAMES , $importDocBlockNames );
2022-04-10 17:44:48 +00:00
}
2022-06-18 22:29:58 +00:00
public function importShortClasses ( bool $importShortClasses = \true ) : void
2022-04-26 23:02:07 +00:00
{
$parameters = $this -> parameters ();
2022-06-18 22:29:58 +00:00
$parameters -> set ( Option :: IMPORT_SHORT_CLASSES , $importShortClasses );
2022-04-26 23:02:07 +00:00
}
2022-04-10 17:44:48 +00:00
/**
* Set PHPStan custom config to load extensions and custom configuration to Rector .
* By default , the " phpstan.neon " path is used .
*/
public function phpstanConfig ( string $filePath ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: fileExists ( $filePath );
2022-04-10 17:44:48 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PHPSTAN_FOR_RECTOR_PATH , $filePath );
2022-04-10 17:44:48 +00:00
}
/**
* @ param class - string < ConfigurableRectorInterface & RectorInterface > $rectorClass
* @ param mixed [] $configuration
*/
public function ruleWithConfiguration ( string $rectorClass , array $configuration ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: classExists ( $rectorClass );
Assert :: isAOf ( $rectorClass , RectorInterface :: class );
Assert :: isAOf ( $rectorClass , ConfigurableRectorInterface :: class );
2022-04-10 17:44:48 +00:00
$services = $this -> services ();
2022-05-27 11:00:42 +00:00
// decorate with value object inliner so Symfony understands, see https://getrector.org/blog/2020/09/07/how-to-inline-value-object-in-symfony-php-config
2022-06-21 15:38:46 +00:00
\array_walk_recursive ( $configuration , static function ( & $value ) {
2022-05-27 11:00:42 +00:00
if ( \is_object ( $value )) {
2022-06-07 08:22:29 +00:00
$value = ValueObjectInliner :: inline ( $value );
2022-05-27 11:00:42 +00:00
}
return $value ;
});
$services -> set ( $rectorClass ) -> call ( 'configure' , [ $configuration ]);
2022-04-10 17:44:48 +00:00
}
2022-04-12 08:58:57 +00:00
/**
* @ param class - string < RectorInterface > $rectorClass
*/
public function rule ( string $rectorClass ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: classExists ( $rectorClass );
Assert :: isAOf ( $rectorClass , RectorInterface :: class );
2022-04-12 08:58:57 +00:00
$services = $this -> services ();
$services -> set ( $rectorClass );
}
2022-05-11 16:53:20 +00:00
/**
* @ param array < class - string < RectorInterface >> $rectorClasses
*/
public function rules ( array $rectorClasses ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $rectorClasses );
2022-05-11 16:53:20 +00:00
foreach ( $rectorClasses as $rectorClass ) {
$this -> rule ( $rectorClass );
}
}
2022-04-12 14:41:09 +00:00
/**
* @ param PhpVersion ::* $phpVersion
*/
public function phpVersion ( int $phpVersion ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: PHP_VERSION_FEATURES , $phpVersion );
2022-04-12 14:41:09 +00:00
}
/**
* @ param string [] $autoloadPaths
*/
public function autoloadPaths ( array $autoloadPaths ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $autoloadPaths );
2022-04-12 14:41:09 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: AUTOLOAD_PATHS , $autoloadPaths );
2022-04-12 14:41:09 +00:00
}
/**
* @ param string [] $bootstrapFiles
*/
public function bootstrapFiles ( array $bootstrapFiles ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $bootstrapFiles );
2022-04-12 14:41:09 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: BOOTSTRAP_FILES , $bootstrapFiles );
2022-04-12 14:41:09 +00:00
}
2022-04-20 19:05:00 +00:00
public function symfonyContainerXml ( string $filePath ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: SYMFONY_CONTAINER_XML_PATH_PARAMETER , $filePath );
2022-04-20 19:05:00 +00:00
}
2022-05-21 08:48:33 +00:00
public function symfonyContainerPhp ( string $filePath ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: SYMFONY_CONTAINER_PHP_PATH_PARAMETER , $filePath );
2022-05-21 08:48:33 +00:00
}
2022-05-27 13:38:07 +00:00
/**
* @ param string [] $extensions
*/
public function fileExtensions ( array $extensions ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: allString ( $extensions );
2022-05-27 13:38:07 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: FILE_EXTENSIONS , $extensions );
2022-05-27 13:38:07 +00:00
}
public function nestedChainMethodCallLimit ( int $limit ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: NESTED_CHAIN_METHOD_CALL_LIMIT , $limit );
2022-05-27 13:38:07 +00:00
}
public function cacheDirectory ( string $directoryPath ) : void
{
2023-03-19 20:28:32 +00:00
// cache directory path is created via mkdir in CacheFactory
// when not exists, so no need to validate $directoryPath is a directory
2022-05-27 13:38:07 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: CACHE_DIR , $directoryPath );
2022-05-27 13:38:07 +00:00
}
2023-03-19 20:28:32 +00:00
public function containerCacheDirectory ( string $directoryPath ) : void
{
// container cache directory path must be a directory on the first place
Assert :: directory ( $directoryPath );
$parameters = $this -> parameters ();
$parameters -> set ( Option :: CONTAINER_CACHE_DIRECTORY , $directoryPath );
}
2022-05-27 13:38:07 +00:00
/**
* @ param class - string < CacheStorageInterface > $cacheClass
*/
public function cacheClass ( string $cacheClass ) : void
{
2022-06-07 08:22:29 +00:00
Assert :: isAOf ( $cacheClass , CacheStorageInterface :: class );
2022-05-27 13:38:07 +00:00
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: CACHE_CLASS , $cacheClass );
2022-05-27 13:38:07 +00:00
}
2022-06-06 09:59:48 +00:00
/**
* @ see https :// github . com / nikic / PHP - Parser / issues / 723 #issuecomment-712401963
*/
public function indent ( string $character , int $count ) : void
{
$parameters = $this -> parameters ();
2022-06-07 08:22:29 +00:00
$parameters -> set ( Option :: INDENT_CHAR , $character );
$parameters -> set ( Option :: INDENT_SIZE , $count );
2022-06-06 09:59:48 +00:00
}
2022-04-08 18:51:16 +00:00
}