{"record":{"id":"0ca93b575ee5f8eb","repo":"moodle/moodle","slug":"file-does-not-exist-or-is-unreadable","errorCode":null,"errorMessage":"file does not exist or is unreadable","messagePattern":"file does not exist or is unreadable","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"public/lib/google2-auth/src/Credentials/UserRefreshCredentials.php","lineNumber":82,"sourceCode":"    private bool $isIdTokenRequest = false;\n\n    /**\n     * Create a new UserRefreshCredentials.\n     *\n     * @param string|string[]|null $scope the scope of the access request, expressed\n     *   either as an Array or as a space-delimited String.\n     * @param string|array<mixed> $jsonKey JSON credential file path or JSON credentials\n     *   as an associative array\n     * @param string|null $targetAudience The audience for the ID token.\n     */\n    public function __construct(\n        $scope,\n        $jsonKey,\n        ?string $targetAudience = null\n    ) {\n        if (is_string($jsonKey)) {\n            if (!file_exists($jsonKey)) {\n                throw new InvalidArgumentException('file does not exist or is unreadable');\n            }\n            $json = file_get_contents($jsonKey);\n            if (!$jsonKey = json_decode((string) $json, true)) {\n                throw new LogicException('invalid json for auth config');\n            }\n        }\n        if (!array_key_exists('client_id', $jsonKey)) {\n            throw new InvalidArgumentException(\n                'json key is missing the client_id field'\n            );\n        }\n        if (!array_key_exists('client_secret', $jsonKey)) {\n            throw new InvalidArgumentException(\n                'json key is missing the client_secret field'\n            );\n        }\n        if (!array_key_exists('refresh_token', $jsonKey)) {\n            throw new InvalidArgumentException(","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/google2-auth/src/Credentials/UserRefreshCredentials.php#L64-L100","documentation":"UserRefreshCredentials::__construct accepts the key as a JSON string or a file path. If a string is passed that is treated as a path but no file exists there (file_exists() fails), it throws InvalidArgumentException('file does not exist or is unreadable').","triggerScenarios":"Calling new UserRefreshCredentials($scope, '/path/to/key.json') where the path does not exist or is not readable by the PHP process; note that valid inline JSON containing spaces/braces is NOT accepted as a path here, so passing raw JSON as a string can also hit this check.","commonSituations":"Typo in the credentials path, relative path resolved against the wrong working directory, file deleted in deployment, or confusion between passing a file path vs. a JSON string.","solutions":["Verify the file path exists and is readable: run is_file() and is_readable() on it before constructing.","Use an absolute path or __DIR__-based path to avoid working-directory issues.","If you have inline JSON, pass a decoded array instead of a string."],"exampleFix":"// before\n$creds = new UserRefreshCredentials($scope, 'key.json'); // relative path may not resolve\n// after\n$path = __DIR__ . '/key.json';\nif (!is_file($path) || !is_readable($path)) { throw new RuntimeException(\"missing key file: $path\"); }\n$creds = new UserRefreshCredentials($scope, $path);","handlingStrategy":"validation","validationCode":"$path = 'key.json';\nif (!is_string($path) || !is_file($path) || !is_readable($path)) {\n    throw new RuntimeException(\"Credential file missing or unreadable: $path\");\n}","typeGuard":"function isValidKeyPath(mixed $jsonKey): bool {\n    return is_string($jsonKey)\n        ? is_file($jsonKey) && is_readable($jsonKey)\n        : is_array($jsonKey);\n}","tryCatchPattern":"try {\n    $creds = new UserRefreshCredentials($scope, $jsonKey);\n} catch (\\InvalidArgumentException $e) {\n    error_log('Credential key problem: ' . $e->getMessage());\n    throw new RuntimeException('Provide an existing key file path or a decoded JSON array', 0, $e);\n}","preventionTips":["Use absolute paths (or __DIR__-based) for credential files.","Check is_file()/is_readable() during app bootstrap, before any auth calls.","Remember a string argument is treated as a PATH, not inline JSON — decode JSON to an array first."],"tags":["php","google-auth","file","configuration"],"backgroundTag":"file-not-found","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}