From be4b70ce79b34762acf1647e63108fdcca7f758b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 1 Apr 2022 16:42:23 +0200 Subject: [PATCH] Fix phpstan issues --- composer.lock | 14 +++++++------- src/Composer/Autoload/AutoloadGenerator.php | 12 ++++++------ src/Composer/Config.php | 6 +++--- src/Composer/Factory.php | 13 ++++++++----- src/Composer/Json/JsonFile.php | 13 ++++++++++++- src/Composer/Util/ConfigValidator.php | 15 +++++++++------ 6 files changed, 45 insertions(+), 28 deletions(-) diff --git a/composer.lock b/composer.lock index 3bb659722..57da6d750 100644 --- a/composer.lock +++ b/composer.lock @@ -695,23 +695,23 @@ }, { "name": "seld/jsonlint", - "version": "1.8.9", + "version": "1.9.0", "source": { "type": "git", "url": "https://github.com/Seldaek/jsonlint.git", - "reference": "d9a308b84277a7dd651ba89bf5ed37b88497b171" + "reference": "4211420d25eba80712bff236a98960ef68b866b7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/d9a308b84277a7dd651ba89bf5ed37b88497b171", - "reference": "d9a308b84277a7dd651ba89bf5ed37b88497b171", + "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/4211420d25eba80712bff236a98960ef68b866b7", + "reference": "4211420d25eba80712bff236a98960ef68b866b7", "shasum": "" }, "require": { "php": "^5.3 || ^7.0 || ^8.0" }, "require-dev": { - "phpstan/phpstan": "^0.12.59", + "phpstan/phpstan": "^1.5", "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^8.5.13" }, "bin": [ @@ -743,7 +743,7 @@ ], "support": { "issues": "https://github.com/Seldaek/jsonlint/issues", - "source": "https://github.com/Seldaek/jsonlint/tree/1.8.9" + "source": "https://github.com/Seldaek/jsonlint/tree/1.9.0" }, "funding": [ { @@ -755,7 +755,7 @@ "type": "tidelift" } ], - "time": "2022-03-31T11:30:35+00:00" + "time": "2022-04-01T13:37:23+00:00" }, { "name": "seld/phar-utils", diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index cdd1f0875..f617f0d8c 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -91,7 +91,7 @@ class AutoloadGenerator */ public function setDevMode(bool $devMode = true) { - $this->devMode = (bool) $devMode; + $this->devMode = $devMode; } /** @@ -102,7 +102,7 @@ class AutoloadGenerator */ public function setClassMapAuthoritative(bool $classMapAuthoritative) { - $this->classMapAuthoritative = (bool) $classMapAuthoritative; + $this->classMapAuthoritative = $classMapAuthoritative; } /** @@ -114,8 +114,8 @@ class AutoloadGenerator */ public function setApcu(bool $apcu, ?string $apcuPrefix = null) { - $this->apcu = (bool) $apcu; - $this->apcuPrefix = $apcuPrefix !== null ? (string) $apcuPrefix : $apcuPrefix; + $this->apcu = $apcu; + $this->apcuPrefix = $apcuPrefix !== null ? $apcuPrefix : $apcuPrefix; } /** @@ -126,7 +126,7 @@ class AutoloadGenerator */ public function setRunScripts(bool $runScripts = true) { - $this->runScripts = (bool) $runScripts; + $this->runScripts = $runScripts; } /** @@ -191,7 +191,7 @@ class AutoloadGenerator } $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode, array(), array( - 'optimize' => (bool) $scanPsrPackages, + 'optimize' => $scanPsrPackages, )); } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 374dce585..74db60bb9 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -505,10 +505,10 @@ class Config /** * Replaces {$refs} inside a config string * - * @param string|int|null $value a config string that can contain {$refs-to-other-config} - * @param int $flags Options (see class constants) + * @param string|mixed $value a config string that can contain {$refs-to-other-config} + * @param int $flags Options (see class constants) * - * @return string|int|null + * @return string|mixed */ private function process($value, int $flags) { diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index acf142ad9..097027348 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -182,7 +182,7 @@ class Factory // load global config $file = new JsonFile($config->get('home').'/config.json'); if ($file->exists()) { - if ($io) { + if ($io instanceof IOInterface) { $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); } self::validateJsonSchema($io, $file); @@ -209,7 +209,7 @@ class Factory // load global auth file $file = new JsonFile($config->get('home').'/auth.json'); if ($file->exists()) { - if ($io) { + if ($io instanceof IOInterface) { $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); } self::validateJsonSchema($io, $file, JsonFile::AUTH_SCHEMA); @@ -221,11 +221,11 @@ class Factory if ($composerAuthEnv = Platform::getEnv('COMPOSER_AUTH')) { $authData = json_decode($composerAuthEnv); if (null === $authData) { - if ($io) { + if ($io instanceof IOInterface) { $io->writeError('COMPOSER_AUTH environment variable is malformed, should be a valid JSON object'); } } else { - if ($io) { + if ($io instanceof IOInterface) { $io->writeError('Loading auth config from COMPOSER_AUTH', true, IOInterface::DEBUG); } self::validateJsonSchema($io, $authData, JsonFile::AUTH_SCHEMA, 'COMPOSER_AUTH'); @@ -702,11 +702,14 @@ class Factory if ($fileOrData instanceof JsonFile) { $fileOrData->validateSchema($schema); } else { + if (null === $source) { + throw new \InvalidArgumentException('$source is required to be provided if $fileOrData is arbitrary data'); + } JsonFile::validateJsonSchema($source, $fileOrData, $schema); } } catch (JsonValidationException $e) { $msg = $e->getMessage().', this may result in errors and should be resolved:'.PHP_EOL.' - '.implode(PHP_EOL.' - ', $e->getErrors()); - if ($io) { + if ($io instanceof IOInterface) { $io->writeError(''.$msg.''); } else { throw new UnexpectedValueException($msg); diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 49f27f6a9..44e37932f 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -203,7 +203,18 @@ class JsonFile return self::validateJsonSchema($this->path, $data, $schema, $schemaFile); } - public static function validateJsonSchema($source, $data, int $schema, ?string $schemaFile = null): bool + /** + * Validates the schema of the current json file according to composer-schema.json rules + * + * @param mixed $data Decoded JSON data to validate + * @param int $schema a JsonFile::*_SCHEMA constant + * @param string|null $schemaFile a path to the schema file + * @throws JsonValidationException + * @return true true on success + * + * @phpstan-param self::*_SCHEMA $schema + */ + public static function validateJsonSchema(string $source, $data, int $schema, ?string $schemaFile = null): bool { $isComposerSchemaFile = false; if (null === $schemaFile) { diff --git a/src/Composer/Util/ConfigValidator.php b/src/Composer/Util/ConfigValidator.php index 4b21f9090..b3a3346db 100644 --- a/src/Composer/Util/ConfigValidator.php +++ b/src/Composer/Util/ConfigValidator.php @@ -58,6 +58,7 @@ class ConfigValidator // validate json schema $laxValid = false; + $manifest = null; try { $json = new JsonFile($file, null, $this->io); $manifest = $json->read(); @@ -79,12 +80,14 @@ class ConfigValidator return array($errors, $publishErrors, $warnings); } - $jsonParser = new JsonParser(); - try { - $jsonParser->parse(file_get_contents($file), JsonParser::DETECT_KEY_CONFLICTS); - } catch (DuplicateKeyException $e) { - $details = $e->getDetails(); - $warnings[] = 'Key '.$details['key'].' is a duplicate in '.$file.' at line '.$details['line']; + if (is_array($manifest)) { + $jsonParser = new JsonParser(); + try { + $jsonParser->parse((string) file_get_contents($file), JsonParser::DETECT_KEY_CONFLICTS); + } catch (DuplicateKeyException $e) { + $details = $e->getDetails(); + $warnings[] = 'Key '.$details['key'].' is a duplicate in '.$file.' at line '.$details['line']; + } } // validate actual data