{"record":{"id":"ed45a001afb3b8e8","repo":"vlucas/phpdotenv","slug":"expected-name-to-be-a-non-empty-string","errorCode":null,"errorMessage":"Expected name to be a non-empty string.","messagePattern":"Expected name to be a non-empty string\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Repository/AdapterRepository.php","lineNumber":65,"sourceCode":"     */\n    public function has(string $name)\n    {\n        return '' !== $name && $this->reader->read($name)->isDefined();\n    }\n\n    /**\n     * Get an environment variable.\n     *\n     * @param string $name\n     *\n     * @throws \\InvalidArgumentException\n     *\n     * @return string|null\n     */\n    public function get(string $name)\n    {\n        if ('' === $name) {\n            throw new InvalidArgumentException('Expected name to be a non-empty string.');\n        }\n\n        return $this->reader->read($name)->getOrElse(null);\n    }\n\n    /**\n     * Set an environment variable.\n     *\n     * @param string $name\n     * @param string $value\n     *\n     * @throws \\InvalidArgumentException\n     *\n     * @return bool\n     */\n    public function set(string $name, string $value)\n    {\n        if ('' === $name) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/vlucas/phpdotenv/blob/416df702837983f8d5ff48c9c3fee4f5f57b980b/src/Repository/AdapterRepository.php#L47-L83","documentation":"AdapterRepository::get() (src/Repository/AdapterRepository.php:65) rejects the empty string as a variable name before any adapter is consulted, throwing InvalidArgumentException. The repository API treats '' as a programming error in the caller, not as 'variable not found' — a missing variable returns null, an empty name never reaches the readers. It is a hard precondition guard on the name argument only; the value is not involved.","triggerScenarios":"$repository->get('') — almost always a computed name: a config key that is absent or mistyped and coalesces to ''; explode(',', $csv) producing '' from a trailing comma; str_replace/preg_replace stripping the whole name; a loop over array keys that contain an empty element.","commonSituations":"Iterating a configurable list of variable names (feature flags, cache purgers); names built as $prefix.$suffix where one part is empty; tests seeding/fetching variables from fixtures; a typo'd config key returning null that strict coercion turns into ''.","solutions":["Log or dump the name at the call site to find which computation produced ''.","Filter and trim the list before use: array_filter(array_map('trim', $names), fn ($n) => $n !== '').","Fix the upstream source: remove trailing commas, correct the config key, skip empty entries when building names.","Add a guard that skips (or explicitly handles) empty names before calling get()."],"exampleFix":"// before\nforeach (explode(',', $config['copy_vars']) as $name) {\n    $value = $repository->get($name);\n}\n\n// after\nforeach (array_filter(array_map('trim', explode(',', $config['copy_vars'] ?? ''))) as $name) {\n    if ($name !== '') {\n        $value = $repository->get($name);\n    }\n}","handlingStrategy":"validation","validationCode":"// Guard a dynamically built name before lookup:\n$name = trim((string) ($config['var_name'] ?? ''));\nif ($name === '') {\n    return null; // or log and skip this entry\n}\n$value = $repository->get($name);","typeGuard":"function isNonEmptyEnvName(mixed $name): bool\n{\n    return is_string($name) && $name !== '';\n}","tryCatchPattern":null,"preventionTips":["Filter configurable name lists: array_filter(array_map('trim', $names), fn ($n) => $n !== '').","Treat an empty computed name as an upstream config bug — log which source produced it.","Add unit fixtures that assert no empty keys ever reach repository calls."],"tags":["validation","invalid-argument","env-var","php","dotenv"],"backgroundTag":"invalid-argument-exception","analyzedSha":"416df702837983f8d5ff48c9c3fee4f5f57b980b","analyzedAt":"2026-08-21T01:19:52.946Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}