Skip to content

Commit

Permalink
Improve internal type inference for configuration and refresh baseline
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed May 18, 2023
1 parent 8688ec8 commit 25d427d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 89 deletions.
106 changes: 28 additions & 78 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.1.0@4defa177c89397c5e14737a80fe4896584130674">
<files psalm-version="5.4.0@62db5d4f6a7ae0a20f7cc5a4952d730272fc0863">
<file src="config/pipeline.php">
<UnusedClosureParam occurrences="2">
<code>$container</code>
Expand All @@ -25,108 +25,70 @@
<ArgumentTypeCoercion occurrences="1">
<code>$this-&gt;stabilityFlags</code>
</ArgumentTypeCoercion>
<MixedArgument occurrences="29">
<InvalidPropertyAssignmentValue occurrences="1">
<code>require __DIR__ . '/config.php'</code>
</InvalidPropertyAssignmentValue>
<MixedArgument occurrences="9">
<code>$answer</code>
<code>$constraint</code>
<code>$defaultOption</code>
<code>$defaultText</code>
<code>$defaultText</code>
<code>$filename</code>
<code>$filename</code>
<code>$force</code>
<code>$key</code>
<code>$option['name']</code>
<code>$package</code>
<code>$package</code>
<code>$packageData['version']</code>
<code>$packageData['whitelist'] ?? []</code>
<code>$packageName</code>
<code>$question</code>
<code>$questionName</code>
<code>$question['custom-package-warning']</code>
<code>$question['question']</code>
<code>$resource</code>
<code>$resource</code>
<code>$target</code>
<code>$target</code>
<code>$this-&gt;composerDefinition['autoload']</code>
<code>$this-&gt;composerDefinition['autoload-dev']</code>
<code>$this-&gt;composerDefinition['extra'] ?? []</code>
<code>$this-&gt;composerDefinition['extra']['laminas']['component-whitelist']</code>
<code>$this-&gt;config['require-dev']</code>
<code>$word</code>
</MixedArgument>
<MixedArrayAccess occurrences="24">
<code>$option['name']</code>
<code>$option['name']</code>
<code>$packageData['version']</code>
<code>$packageData['whitelist']</code>
<code>$question['options'][$answer][$this-&gt;installType]</code>
<code>$question['options'][$answer]['packages']</code>
<MixedArgumentTypeCoercion occurrences="1">
<code>$package</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess occurrences="6">
<code>$this-&gt;composerDefinition['autoload']['psr-4']</code>
<code>$this-&gt;composerDefinition['autoload']['psr-4']['MezzioInstaller\\']</code>
<code>$this-&gt;composerDefinition['autoload-dev']['psr-4']</code>
<code>$this-&gt;composerDefinition['autoload-dev']['psr-4']['MezzioInstallerTest\\']</code>
<code>$this-&gt;composerDefinition['extra']['branch-alias']</code>
<code>$this-&gt;composerDefinition['extra']['laminas']</code>
<code>$this-&gt;composerDefinition['extra']['laminas']['component-whitelist']</code>
<code>$this-&gt;composerDefinition['extra']['optional-packages']</code>
<code>$this-&gt;composerDefinition['require'][$packageName]</code>
<code>$this-&gt;composerDefinition['require-dev'][$devDependency]</code>
<code>$this-&gt;composerDefinition['require-dev'][$packageName]</code>
<code>$this-&gt;composerDefinition['scripts']['pre-install-cmd']</code>
<code>$this-&gt;composerDefinition['scripts']['pre-update-cmd']</code>
<code>$this-&gt;config['application'][$this-&gt;installType]</code>
<code>$this-&gt;config['application'][$this-&gt;installType]</code>
<code>$this-&gt;config['application'][$this-&gt;installType]['packages']</code>
<code>$this-&gt;config['application'][$this-&gt;installType]['resources']</code>
<code>$this-&gt;config['packages'][$packageName]</code>
</MixedArrayAccess>
<MixedArrayAssignment occurrences="10">
<MixedArrayAssignment occurrences="6">
<code>$this-&gt;composerDefinition['autoload']['psr-4']</code>
<code>$this-&gt;composerDefinition['autoload']['psr-4']['App\\']</code>
<code>$this-&gt;composerDefinition['extra']['laminas']</code>
<code>$this-&gt;composerDefinition['extra']['laminas']['component-whitelist']</code>
<code>$this-&gt;composerDefinition['extra']['optional-packages']</code>
<code>$this-&gt;composerDefinition['extra']['optional-packages'][$questionName]</code>
<code>$this-&gt;composerDefinition['require'][$packageName]</code>
<code>$this-&gt;composerDefinition['require-dev'][$packageName]</code>
<code>$this-&gt;composerDefinition['scripts']['cs-check']</code>
<code>$this-&gt;composerDefinition['scripts']['cs-fix']</code>
</MixedArrayAssignment>
<MixedArrayOffset occurrences="1">
<code>$this-&gt;config['packages'][$packageName]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="21">
<MixedAssignment occurrences="8">
<code>$answer</code>
<code>$answer</code>
<code>$constraint</code>
<code>$defaultOption</code>
<code>$defaultText</code>
<code>$fileInfo</code>
<code>$filename</code>
<code>$force</code>
<code>$key</code>
<code>$option</code>
<code>$package</code>
<code>$package</code>
<code>$packageData</code>
<code>$packageName</code>
<code>$question</code>
<code>$questionName</code>
<code>$resource</code>
<code>$resource</code>
<code>$target</code>
<code>$target</code>
<code>$this-&gt;composerDefinition</code>
</MixedAssignment>
<MixedMethodCall occurrences="1">
<code>isDir</code>
</MixedMethodCall>
<NoValue occurrences="1">
<code>$this-&gt;installType</code>
</NoValue>
<PossiblyInvalidArgument occurrences="1">
<code>$resource</code>
</PossiblyInvalidArgument>
<PossiblyInvalidIterator occurrences="1">
<code>$question['options'][$answer][$this-&gt;installType]</code>
</PossiblyInvalidIterator>
<PossiblyNullArgument occurrences="2">
<code>$content</code>
<code>$phpunitConfig</code>
</PossiblyNullArgument>
<PropertyTypeCoercion occurrences="3">
<code>$this-&gt;composerDefinition</code>
<code>$this-&gt;composerDefinition</code>
<code>$this-&gt;composerDefinition</code>
</PropertyTypeCoercion>
</file>
<file src="test/AppTest/Handler/PingHandlerTest.php">
<MixedArgument occurrences="1">
Expand Down Expand Up @@ -286,27 +248,13 @@
</UnresolvableInclude>
</file>
<file src="test/MezzioInstallerTest/PromptForOptionalPackagesTest.php">
<InvalidArgument occurrences="2">
<code>testPromptForOptionalPackage</code>
<code>testPromptForOptionalPackage</code>
</InvalidArgument>
<InvalidReturnType occurrences="1"/>
<MixedArgument occurrences="3">
<code>$package</code>
<code>$question['question']</code>
<MixedArgument occurrences="1">
<code>$written</code>
</MixedArgument>
<MixedArrayAssignment occurrences="1">
<code>$written[]</code>
</MixedArrayAssignment>
<MixedAssignment occurrences="6">
<code>$package</code>
<code>$package</code>
<code>$target</code>
<code>$toWrite[]</code>
<code>$toWrite[]</code>
<code>$written[]</code>
</MixedAssignment>
</file>
<file src="test/MezzioInstallerTest/RemoveDevDependenciesTest.php">
<MixedAssignment occurrences="1">
Expand All @@ -326,6 +274,7 @@
<MixedArrayAccess occurrences="5">
<code>$config['dependencies']</code>
<code>$config['dependencies'][$dependencyKey]</code>
<code>$config['dependencies'][$dependencyKey][RouterInterface::class]</code>
<code>$config['questions']['container']</code>
<code>$config['questions']['router']</code>
</MixedArrayAccess>
Expand All @@ -345,6 +294,7 @@
<MixedArrayAccess occurrences="6">
<code>$config['dependencies']</code>
<code>$config['dependencies']['factories']</code>
<code>$config['dependencies']['factories'][ErrorHandler::class]</code>
<code>$config['questions']['container']</code>
<code>$config['questions']['router']</code>
<code>$config['questions']['template-engine']</code>
Expand Down
63 changes: 54 additions & 9 deletions src/MezzioInstaller/OptionalPackages.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@
* "pre-update-cmd": "MezzioInstaller\\OptionalPackages::install",
* "pre-install-cmd": "MezzioInstaller\\OptionalPackages::install"
* },
*
* @psalm-type OptionalPackageSpec = array{
* name: string,
* packages: list<string>,
* flat: array<string, string>,
* modular: array<string, string>,
* minimal: array<string, string>,
* }
* @psalm-type QuestionSpec = array{
* question: string,
* default: int,
* required: bool,
* force?: bool,
* custom-package: bool,
* custom-package-warning?: string,
* options: array<int, OptionalPackageSpec>,
* }
*/
class OptionalPackages
{
Expand Down Expand Up @@ -90,7 +107,7 @@ class OptionalPackages
/**
* Assets to remove during cleanup.
*
* @var string[]
* @var list<string>
*/
private array $assetsToRemove = [
'.coveralls.yml',
Expand All @@ -105,11 +122,36 @@ class OptionalPackages
'src/App/templates/.gitkeep',
];

/** @var array */
private $config = [];

/** @var array */
private $composerDefinition = [];
/**
* @var array{
* packages: array<string, array{
* version: string,
* whitelist?: list<string>,
* }>,
* questions: array<string, QuestionSpec>,
* require-dev: list<string>,
* application: array<string, array{
* packages: array,
* resources: array<string, string>,
* }>,
* }
*/
private array $config;
/**
* @var array{
* require: array<string, string>,
* require-dev: array<string, string>,
* extra: array{
* optional-packages: array,
* branch-alias?: mixed,
* laminas: array{
* component-whitelist: list<string>,
* }
* },
* ...
* }
*/
private array $composerDefinition;

private JsonFile $composerJson;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions test/MezzioInstallerTest/PromptForOptionalPackagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,9 +48,9 @@ protected function tearDown(): void
/**
* @psalm-return Generator<string, array{
* 0: string,
* 1: string,
* 1: QuestionSpec,
* 2: int,
* 3: string
* 3: OptionalPackageSpec
* }>
*/
public function promptCombinations(): Generator
Expand All @@ -62,6 +66,8 @@ public function promptCombinations(): Generator

/**
* @dataProvider promptCombinations
* @param QuestionSpec $question
* @param OptionalPackageSpec $expectedPackage
*/
public function testPromptForOptionalPackage(
string $questionName,
Expand Down

0 comments on commit 25d427d

Please sign in to comment.