Fix phpstan issues

main
Jordi Boggiano 2 years ago
parent 27a449eaf7
commit be4b70ce79
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

14
composer.lock generated

@ -695,23 +695,23 @@
}, },
{ {
"name": "seld/jsonlint", "name": "seld/jsonlint",
"version": "1.8.9", "version": "1.9.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Seldaek/jsonlint.git", "url": "https://github.com/Seldaek/jsonlint.git",
"reference": "d9a308b84277a7dd651ba89bf5ed37b88497b171" "reference": "4211420d25eba80712bff236a98960ef68b866b7"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/d9a308b84277a7dd651ba89bf5ed37b88497b171", "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/4211420d25eba80712bff236a98960ef68b866b7",
"reference": "d9a308b84277a7dd651ba89bf5ed37b88497b171", "reference": "4211420d25eba80712bff236a98960ef68b866b7",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": "^5.3 || ^7.0 || ^8.0" "php": "^5.3 || ^7.0 || ^8.0"
}, },
"require-dev": { "require-dev": {
"phpstan/phpstan": "^0.12.59", "phpstan/phpstan": "^1.5",
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^8.5.13" "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0 || ^8.5.13"
}, },
"bin": [ "bin": [
@ -743,7 +743,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/Seldaek/jsonlint/issues", "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": [ "funding": [
{ {
@ -755,7 +755,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2022-03-31T11:30:35+00:00" "time": "2022-04-01T13:37:23+00:00"
}, },
{ {
"name": "seld/phar-utils", "name": "seld/phar-utils",

@ -91,7 +91,7 @@ class AutoloadGenerator
*/ */
public function setDevMode(bool $devMode = true) public function setDevMode(bool $devMode = true)
{ {
$this->devMode = (bool) $devMode; $this->devMode = $devMode;
} }
/** /**
@ -102,7 +102,7 @@ class AutoloadGenerator
*/ */
public function setClassMapAuthoritative(bool $classMapAuthoritative) 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) public function setApcu(bool $apcu, ?string $apcuPrefix = null)
{ {
$this->apcu = (bool) $apcu; $this->apcu = $apcu;
$this->apcuPrefix = $apcuPrefix !== null ? (string) $apcuPrefix : $apcuPrefix; $this->apcuPrefix = $apcuPrefix !== null ? $apcuPrefix : $apcuPrefix;
} }
/** /**
@ -126,7 +126,7 @@ class AutoloadGenerator
*/ */
public function setRunScripts(bool $runScripts = true) 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( $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode, array(), array(
'optimize' => (bool) $scanPsrPackages, 'optimize' => $scanPsrPackages,
)); ));
} }

@ -505,10 +505,10 @@ class Config
/** /**
* Replaces {$refs} inside a config string * Replaces {$refs} inside a config string
* *
* @param string|int|null $value a config string that can contain {$refs-to-other-config} * @param string|mixed $value a config string that can contain {$refs-to-other-config}
* @param int $flags Options (see class constants) * @param int $flags Options (see class constants)
* *
* @return string|int|null * @return string|mixed
*/ */
private function process($value, int $flags) private function process($value, int $flags)
{ {

@ -182,7 +182,7 @@ class Factory
// load global config // load global config
$file = new JsonFile($config->get('home').'/config.json'); $file = new JsonFile($config->get('home').'/config.json');
if ($file->exists()) { if ($file->exists()) {
if ($io) { if ($io instanceof IOInterface) {
$io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG);
} }
self::validateJsonSchema($io, $file); self::validateJsonSchema($io, $file);
@ -209,7 +209,7 @@ class Factory
// load global auth file // load global auth file
$file = new JsonFile($config->get('home').'/auth.json'); $file = new JsonFile($config->get('home').'/auth.json');
if ($file->exists()) { if ($file->exists()) {
if ($io) { if ($io instanceof IOInterface) {
$io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG);
} }
self::validateJsonSchema($io, $file, JsonFile::AUTH_SCHEMA); self::validateJsonSchema($io, $file, JsonFile::AUTH_SCHEMA);
@ -221,11 +221,11 @@ class Factory
if ($composerAuthEnv = Platform::getEnv('COMPOSER_AUTH')) { if ($composerAuthEnv = Platform::getEnv('COMPOSER_AUTH')) {
$authData = json_decode($composerAuthEnv); $authData = json_decode($composerAuthEnv);
if (null === $authData) { if (null === $authData) {
if ($io) { if ($io instanceof IOInterface) {
$io->writeError('<error>COMPOSER_AUTH environment variable is malformed, should be a valid JSON object</error>'); $io->writeError('<error>COMPOSER_AUTH environment variable is malformed, should be a valid JSON object</error>');
} }
} else { } else {
if ($io) { if ($io instanceof IOInterface) {
$io->writeError('Loading auth config from COMPOSER_AUTH', true, IOInterface::DEBUG); $io->writeError('Loading auth config from COMPOSER_AUTH', true, IOInterface::DEBUG);
} }
self::validateJsonSchema($io, $authData, JsonFile::AUTH_SCHEMA, 'COMPOSER_AUTH'); self::validateJsonSchema($io, $authData, JsonFile::AUTH_SCHEMA, 'COMPOSER_AUTH');
@ -702,11 +702,14 @@ class Factory
if ($fileOrData instanceof JsonFile) { if ($fileOrData instanceof JsonFile) {
$fileOrData->validateSchema($schema); $fileOrData->validateSchema($schema);
} else { } else {
if (null === $source) {
throw new \InvalidArgumentException('$source is required to be provided if $fileOrData is arbitrary data');
}
JsonFile::validateJsonSchema($source, $fileOrData, $schema); JsonFile::validateJsonSchema($source, $fileOrData, $schema);
} }
} catch (JsonValidationException $e) { } catch (JsonValidationException $e) {
$msg = $e->getMessage().', this may result in errors and should be resolved:'.PHP_EOL.' - '.implode(PHP_EOL.' - ', $e->getErrors()); $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('<warning>'.$msg.'</>'); $io->writeError('<warning>'.$msg.'</>');
} else { } else {
throw new UnexpectedValueException($msg); throw new UnexpectedValueException($msg);

@ -203,7 +203,18 @@ class JsonFile
return self::validateJsonSchema($this->path, $data, $schema, $schemaFile); 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; $isComposerSchemaFile = false;
if (null === $schemaFile) { if (null === $schemaFile) {

@ -58,6 +58,7 @@ class ConfigValidator
// validate json schema // validate json schema
$laxValid = false; $laxValid = false;
$manifest = null;
try { try {
$json = new JsonFile($file, null, $this->io); $json = new JsonFile($file, null, $this->io);
$manifest = $json->read(); $manifest = $json->read();
@ -79,12 +80,14 @@ class ConfigValidator
return array($errors, $publishErrors, $warnings); return array($errors, $publishErrors, $warnings);
} }
$jsonParser = new JsonParser(); if (is_array($manifest)) {
try { $jsonParser = new JsonParser();
$jsonParser->parse(file_get_contents($file), JsonParser::DETECT_KEY_CONFLICTS); try {
} catch (DuplicateKeyException $e) { $jsonParser->parse((string) file_get_contents($file), JsonParser::DETECT_KEY_CONFLICTS);
$details = $e->getDetails(); } catch (DuplicateKeyException $e) {
$warnings[] = 'Key '.$details['key'].' is a duplicate in '.$file.' at line '.$details['line']; $details = $e->getDetails();
$warnings[] = 'Key '.$details['key'].' is a duplicate in '.$file.' at line '.$details['line'];
}
} }
// validate actual data // validate actual data

Loading…
Cancel
Save