From 46370804d7f74672c727f9b2859e5eba91b82885 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 17 Aug 2023 08:44:53 +0200 Subject: [PATCH 1/2] Inform RectorConfig containerCacheDirectory() is no longer by Rector container, only by PHPStan --- config/config.php | 1 - packages/Config/RectorConfig.php | 4 +++ .../PHPStanServicesFactory.php | 2 +- phpstan.neon | 33 +------------------ scoper.php | 5 ++- .../LazyContainerFactory.php | 2 ++ 6 files changed, 10 insertions(+), 37 deletions(-) diff --git a/config/config.php b/config/config.php index 5f2da879ad7..c8fe35b5caa 100644 --- a/config/config.php +++ b/config/config.php @@ -25,7 +25,6 @@ $rectorConfig->fileExtensions(['php']); $rectorConfig->cacheDirectory(sys_get_temp_dir() . '/rector_cached_files'); - $rectorConfig->containerCacheDirectory(sys_get_temp_dir()); // use faster in-memory cache in CI. // CI always starts from scratch, therefore IO intensive caching is not worth it diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index 5d60b6c639f..71b2413973c 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -279,6 +279,10 @@ public function cacheDirectory(string $directoryPath): void SimpleParameterProvider::setParameter(Option::CACHE_DIR, $directoryPath); } + /** + * @info Rector no longer user compiled container, but Laravel lazy one. + * This option is for PHPStan container in case the sys_get_temp_dir() is not available for the current user. + */ public function containerCacheDirectory(string $directoryPath): void { // container cache directory path must be a directory on the first place diff --git a/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php b/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php index fa06a5cf7e3..0ed3e19c6ff 100644 --- a/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php +++ b/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php @@ -49,7 +49,7 @@ public function __construct( $containerFactory = new ContainerFactory(getcwd()); $this->container = $containerFactory->create( - SimpleParameterProvider::provideStringParameter(Option::CONTAINER_CACHE_DIRECTORY), + sys_get_temp_dir() . '/phpstan-container-for-rector', $additionalConfigFiles, [] ); diff --git a/phpstan.neon b/phpstan.neon index bb3c01dee5b..0a917cc336e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -213,7 +213,6 @@ parameters: - '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)Rector\:\:refactor\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#' # generics - - '#Method Rector\\Core\\PhpParser\\NodeTraverser\\RectorNodeTraverser\:\:traverse\(\) should return array but returns array#' - '#Parameter \#1 \$stmts of class Rector\\Core\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace constructor expects array, array given#' # strict - resolve later @@ -424,9 +423,6 @@ parameters: message: '#Instead of "instanceof/is_a\(\)" use ReflectionProvider service or "\(new ObjectType\(\)\)\->isSuperTypeOf\(\)" for static reflection to work#' path: packages/Skipper/Skipper/SkipSkipper.php - # the local instanceof for known types - - '#Instead of "instanceof/is_a\(\)" use ReflectionProvider service or "\(new ObjectType\(\)\)\->isSuperTypeOf\(\)" for static reflection to work#' - # required for reflection - message: '#Function "(.*?)\(\)" cannot be used/left in the code#' @@ -518,9 +514,6 @@ parameters: # useless - '#Parameter \#1 \$suffix of method PHPUnit\\Framework\\Assert\:\:assertStringEndsWith\(\) expects non\-empty\-string, string given#' - # reported in configs - - '#Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\service not found#' - - message: '#Function "function_exists\(\)" cannot be used/left in the code#' path: src/functions/node_helper.php @@ -537,9 +530,6 @@ parameters: - '#Call to deprecated method getDirectClassNames\(\) of class PHPStan\\Type\\TypeUtils.*#' - '#Parameter 3 should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs" type as the only type passed to this method#' - # various usage - - '#Parameters should use "PHPStan\\DependencyInjection\\Container" types as the only types passed to this method#' - # actually used in global scope - message: '#Anonymous function has an unused use \$container#' @@ -598,9 +588,6 @@ parameters: message: '#Parameters should use "array" types as the only types passed to this method#' path: packages/VersionBonding/PhpVersionedFilter.php - # for symfony configs check - - '#Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\tagged_iterator not found#' - - message: '#Do not use static property#' path: src/Configuration/Parameter/SimpleParameterProvider.php @@ -633,26 +620,11 @@ parameters: message: '#Parameters should use "Symfony\\Component\\Console\\Application\|string\|callable" types as the only types passed to this method#' path: src/Util/Reflection/PrivatesAccessor.php - # false positives - - '#Property Rector\\PhpAttribute\\AnnotationToAttributeMapper\:\:\$annotationToAttributeMappers \(array\) does not accept iterable#' - # avoid circular dependency - message: '#\$this as argument is not allowed\. Refactor method to service composition#' path: packages/NodeTypeResolver/NodeTypeResolver.php - - '#Call to an undefined method Rector\\Config\\RectorConfig\:\:(.*?)\(\)#' - - '#Call to an undefined method Illuminate\\Container\\Container\:\:(.*?)\(\)#' - - '#Parameter 1 should use "Illuminate\\Container\\Container" type as the only type passed to this method#' - - '#Parameter \#1 \$container of method Rector\\Core\\DependencyInjection\\LazyContainerFactory\:\:createPHPStanServices\(\) expects Rector\\Config\\RectorConfig, Illuminate\\Container\\Container given#' - - - '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array\) does not accept iterable#' - - # false positive - - '#Parameter \#1 \$commands of method Symfony\\Component\\Console\\Application\:\:addCommands\(\) expects array, iterable given#' - - - '#Property Rector\\Core\\Console\\Command\\ListRulesCommand\:\:\$rectors \(array\) does not accept array\|iterable#' - # reflection property change - message: '#\$this as argument is not allowed\. Refactor method to service composition#' @@ -665,9 +637,6 @@ parameters: - '#Class "Rector\\Renaming\\Rector\\FileWithoutNamespace\\PseudoNamespaceToNamespaceRector" is missing @see annotation with test case class reference#' - # iterable mix - - '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array\) does not accept array\|iterable#' - # chicken/egg - message: '#Function "(d|dd)\(\)" cannot be used/left in the code#' @@ -691,4 +660,4 @@ parameters: message: '#Make callable type explicit\. Here is how\: https\://phpstan\.org/writing\-php\-code/phpdoc\-types\#callables#' path: packages/Config/RectorConfig.php - - '#Method Rector\\Core\\PhpParser\\NodeTraverser\\RectorNodeTraverser\:\:__construct\(\) has parameter \$phpRectors with no value type specified in iterable type iterable#' + - '#Method Rector\\Core\\PhpParser\\NodeTraverser\\RectorNodeTraverser\:\:traverse\(\) should return array but returns array#' diff --git a/scoper.php b/scoper.php index 2a714f73a04..0aae2641cc6 100644 --- a/scoper.php +++ b/scoper.php @@ -72,10 +72,9 @@ $content ), - static function (string $filePath, string $prefix, string $content): string { + static fn(string $filePath, string $prefix, string $content): string => // comment out - return str_replace('\\' . $prefix . '\trigger_deprecation(', '// \trigger_deprecation(', $content); - }, + str_replace('\\' . $prefix . '\trigger_deprecation(', '// \trigger_deprecation(', $content), static function (string $filePath, string $prefix, string $content): string { if (! \str_ends_with($filePath, 'src/Application/VersionResolver.php')) { diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 9c01011d583..0dbbc66b0db 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -398,6 +398,8 @@ public function create(): RectorConfig $rectorConfig->fileExtensions(['php']); $rectorConfig->cacheDirectory(sys_get_temp_dir() . '/rector_cached_files'); + + // cache directory for PHPStan container $rectorConfig->containerCacheDirectory(sys_get_temp_dir()); // make use of https://github.com/symplify/easy-parallel From 84d4fe50a305aab60af81b1a5fc1a6d445eb77a4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 17 Aug 2023 10:43:32 +0200 Subject: [PATCH 2/2] make sure PHPStan cache directory is configurable and created --- packages/Config/RectorConfig.php | 2 +- .../DependencyInjection/PHPStanServicesFactory.php | 11 ++++++----- src/DependencyInjection/LazyContainerFactory.php | 3 --- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index 71b2413973c..c761f229a81 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -280,7 +280,7 @@ public function cacheDirectory(string $directoryPath): void } /** - * @info Rector no longer user compiled container, but Laravel lazy one. + * @info Rector no longer uses compiled container, but Laravel lazy one. * This option is for PHPStan container in case the sys_get_temp_dir() is not available for the current user. */ public function containerCacheDirectory(string $directoryPath): void diff --git a/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php b/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php index 0ed3e19c6ff..c14b94e13b4 100644 --- a/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php +++ b/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php @@ -47,12 +47,13 @@ public function __construct( $purifiedConfigFiles[] = $purifiedConfigFile; } + $cacheDirectory = SimpleParameterProvider::provideStringParameter(Option::CACHE_DIR); + + // make sure directory exists as PHPStan Nette container will not create it and crash + \Nette\Utils\FileSystem::createDir($cacheDirectory); + $containerFactory = new ContainerFactory(getcwd()); - $this->container = $containerFactory->create( - sys_get_temp_dir() . '/phpstan-container-for-rector', - $additionalConfigFiles, - [] - ); + $this->container = $containerFactory->create($cacheDirectory, $additionalConfigFiles, []); // clear temporary files, after container is created $filesystem = new Filesystem(); diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 0dbbc66b0db..bb3114247e8 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -399,9 +399,6 @@ public function create(): RectorConfig $rectorConfig->cacheDirectory(sys_get_temp_dir() . '/rector_cached_files'); - // cache directory for PHPStan container - $rectorConfig->containerCacheDirectory(sys_get_temp_dir()); - // make use of https://github.com/symplify/easy-parallel $rectorConfig->singleton(Application::class, static function (): Application { $application = new Application();