From 25d427d11b9381422038e37009dadab7e42cfb96 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 18 May 2023 22:41:39 +0100 Subject: [PATCH] Improve internal type inference for configuration and refresh baseline Signed-off-by: George Steel --- psalm-baseline.xml | 106 +++++------------- src/MezzioInstaller/OptionalPackages.php | 63 +++++++++-- .../PromptForOptionalPackagesTest.php | 10 +- 3 files changed, 90 insertions(+), 89 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7898888..ec345e7 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + $container @@ -25,108 +25,70 @@ $this->stabilityFlags - + + require __DIR__ . '/config.php' + + $answer $constraint - $defaultOption - $defaultText - $defaultText $filename $filename - $force - $key - $option['name'] $package - $package - $packageData['version'] - $packageData['whitelist'] ?? [] - $packageName - $question - $questionName - $question['custom-package-warning'] - $question['question'] - $resource - $resource - $target $target $this->composerDefinition['autoload'] $this->composerDefinition['autoload-dev'] - $this->composerDefinition['extra'] ?? [] - $this->composerDefinition['extra']['laminas']['component-whitelist'] - $this->config['require-dev'] $word - - $option['name'] - $option['name'] - $packageData['version'] - $packageData['whitelist'] - $question['options'][$answer][$this->installType] - $question['options'][$answer]['packages'] + + $package + + $this->composerDefinition['autoload']['psr-4'] $this->composerDefinition['autoload']['psr-4']['MezzioInstaller\\'] $this->composerDefinition['autoload-dev']['psr-4'] $this->composerDefinition['autoload-dev']['psr-4']['MezzioInstallerTest\\'] - $this->composerDefinition['extra']['branch-alias'] - $this->composerDefinition['extra']['laminas'] - $this->composerDefinition['extra']['laminas']['component-whitelist'] - $this->composerDefinition['extra']['optional-packages'] - $this->composerDefinition['require'][$packageName] - $this->composerDefinition['require-dev'][$devDependency] - $this->composerDefinition['require-dev'][$packageName] $this->composerDefinition['scripts']['pre-install-cmd'] $this->composerDefinition['scripts']['pre-update-cmd'] - $this->config['application'][$this->installType] - $this->config['application'][$this->installType] - $this->config['application'][$this->installType]['packages'] - $this->config['application'][$this->installType]['resources'] - $this->config['packages'][$packageName] - + $this->composerDefinition['autoload']['psr-4'] $this->composerDefinition['autoload']['psr-4']['App\\'] - $this->composerDefinition['extra']['laminas'] - $this->composerDefinition['extra']['laminas']['component-whitelist'] - $this->composerDefinition['extra']['optional-packages'] - $this->composerDefinition['extra']['optional-packages'][$questionName] $this->composerDefinition['require'][$packageName] $this->composerDefinition['require-dev'][$packageName] $this->composerDefinition['scripts']['cs-check'] $this->composerDefinition['scripts']['cs-fix'] - - $this->config['packages'][$packageName] - - + $answer $answer $constraint - $defaultOption - $defaultText $fileInfo $filename - $force - $key - $option $package - $package - $packageData - $packageName - $question - $questionName - $resource - $resource - $target $target $this->composerDefinition isDir + + $this->installType + + + $resource + + + $question['options'][$answer][$this->installType] + $content $phpunitConfig + + $this->composerDefinition + $this->composerDefinition + $this->composerDefinition + @@ -286,27 +248,13 @@ - - testPromptForOptionalPackage - testPromptForOptionalPackage - - - $package - $question['question'] + $written $written[] - - $package - $package - $target - $toWrite[] - $toWrite[] - $written[] - @@ -326,6 +274,7 @@ $config['dependencies'] $config['dependencies'][$dependencyKey] + $config['dependencies'][$dependencyKey][RouterInterface::class] $config['questions']['container'] $config['questions']['router'] @@ -345,6 +294,7 @@ $config['dependencies'] $config['dependencies']['factories'] + $config['dependencies']['factories'][ErrorHandler::class] $config['questions']['container'] $config['questions']['router'] $config['questions']['template-engine'] diff --git a/src/MezzioInstaller/OptionalPackages.php b/src/MezzioInstaller/OptionalPackages.php index 09ef974..66cb71f 100644 --- a/src/MezzioInstaller/OptionalPackages.php +++ b/src/MezzioInstaller/OptionalPackages.php @@ -52,6 +52,23 @@ * "pre-update-cmd": "MezzioInstaller\\OptionalPackages::install", * "pre-install-cmd": "MezzioInstaller\\OptionalPackages::install" * }, + * + * @psalm-type OptionalPackageSpec = array{ + * name: string, + * packages: list, + * flat: array, + * modular: array, + * minimal: array, + * } + * @psalm-type QuestionSpec = array{ + * question: string, + * default: int, + * required: bool, + * force?: bool, + * custom-package: bool, + * custom-package-warning?: string, + * options: array, + * } */ class OptionalPackages { @@ -90,7 +107,7 @@ class OptionalPackages /** * Assets to remove during cleanup. * - * @var string[] + * @var list */ private array $assetsToRemove = [ '.coveralls.yml', @@ -105,11 +122,36 @@ class OptionalPackages 'src/App/templates/.gitkeep', ]; - /** @var array */ - private $config = []; - - /** @var array */ - private $composerDefinition = []; + /** + * @var array{ + * packages: array, + * }>, + * questions: array, + * require-dev: list, + * application: array, + * }>, + * } + */ + private array $config; + /** + * @var array{ + * require: array, + * require-dev: array, + * extra: array{ + * optional-packages: array, + * branch-alias?: mixed, + * laminas: array{ + * component-whitelist: list, + * } + * }, + * ... + * } + */ + private array $composerDefinition; private JsonFile $composerJson; @@ -142,7 +184,7 @@ class OptionalPackages /** @var string Path to this file. */ private string $installerSource; - /** @var string Installation type selected. */ + /** @var self::INSTALL_* Installation type selected. */ private string $installType = self::INSTALL_FLAT; private string $projectRoot; @@ -231,7 +273,7 @@ public function removeDevDependencies(): void /** * Prompt for the installation type. * - * @return string One of the INSTALL_ constants. + * @return self::INSTALL_* One of the INSTALL_ constants. */ public function requestInstallType(): string { @@ -338,7 +380,7 @@ public function promptForOptionalPackages(): void * Prompt for a single optional installation package. * * @param string $questionName Name of question - * @param array $question Question details from configuration + * @param QuestionSpec $question Question details from configuration */ public function promptForOptionalPackage(string $questionName, array $question): void { @@ -420,6 +462,8 @@ public function finalizePackage(): void /** * Process the answer of a question + * + * @param QuestionSpec $question */ public function processAnswer(array $question, bool|int|string $answer): bool { @@ -588,6 +632,7 @@ private function preparePhpunitConfig(): void /** * Prepare and ask questions and return the answer * + * @param QuestionSpec $question * @codeCoverageIgnore */ private function askQuestion(array $question, int|string $defaultOption): string|int|bool diff --git a/test/MezzioInstallerTest/PromptForOptionalPackagesTest.php b/test/MezzioInstallerTest/PromptForOptionalPackagesTest.php index fac1b99..f2c5629 100644 --- a/test/MezzioInstallerTest/PromptForOptionalPackagesTest.php +++ b/test/MezzioInstallerTest/PromptForOptionalPackagesTest.php @@ -14,6 +14,10 @@ use function sprintf; use function str_contains; +/** + * @psalm-import-type QuestionSpec from OptionalPackages + * @psalm-import-type OptionalPackageSpec from OptionalPackages + */ class PromptForOptionalPackagesTest extends OptionalPackagesTestCase { use ProjectSandboxTrait; @@ -44,9 +48,9 @@ protected function tearDown(): void /** * @psalm-return Generator */ public function promptCombinations(): Generator @@ -62,6 +66,8 @@ public function promptCombinations(): Generator /** * @dataProvider promptCombinations + * @param QuestionSpec $question + * @param OptionalPackageSpec $expectedPackage */ public function testPromptForOptionalPackage( string $questionName,