Using inspections and auto-completion productively
.env files look like plain text files at first glance, but PhpStorm treats them as a dedicated file type with syntax highlighting, inspections, and auto-completion. Used correctly, this prevents exactly the errors that would otherwise only surface at runtime inside the container.
Table of Contents
- 1. Why .env files deserve more attention than plain text
- 2. Syntax highlighting and structure recognition in detail
- 3. Inspections: detecting missing and unused variables
- 4. How .env.example works as a living reference
- 5. Auto-completion for variable names across the project
- 6. Handling multiple .env files within one project
- 7. Protecting sensitive data in .env files from accidental commits
- 8. Typical failure patterns caught early by inspections
- 9. Practical workflow for new environment variables
- 10. Summary
- 11. FAQ
1. Why .env files deserve more attention than plain text
In Docker-based Magento projects, as commonly used with Mark Shust's docker-magento, .env files control central aspects of the environment: database credentials, PHP version, enabled Xdebug modes, domain names, and numerous other settings that make the difference between a working and a broken local setup. A single typo in a variable name, or a forgotten entry, often does not cause an immediate error but a silent fallback to a default value that only causes confusion much later.
PhpStorm does not treat .env files as generic text but as a distinct, structured file type with a dedicated language plugin. Concretely, that means color-coded distinction of keys, values, and comments, context-aware inspections that flag inconsistencies, and auto-completion based on variable names already known within the project. For a project with several environment files, such as .env, .env.local, and .env.example, that is a noticeable productivity gain over a plain text editor.
2. Syntax highlighting and structure recognition in detail
PhpStorm recognizes the typical KEY=VALUE structure line by line in .env files and highlights keys, equals signs, values, and comments in different colors, which makes quickly scanning a long configuration file noticeably easier. Values in quotes are recognized as strings, while unquoted numeric or boolean values are displayed differently, so it is visible at a glance whether a value is interpreted as a string or as a literal value.
The structure view, reachable via the File Structure popup, lists all variables defined in an .env file compactly one below the other and allows quick keyboard navigation to a specific variable without scrolling through the entire file. For .env files with several dozen entries, common in grown Magento projects with many modules, that noticeably saves time compared to a manual full-text search.
# .env of a Mark Shust docker-magento setup (excerpt)
COMPOSE_PROJECT_NAME=mironsoft
DB_ROOT_PASSWORD=magento2
DB_NAME=magento
DB_USER=magento
DB_PASSWORD=magento
# PHP version for the FPM container
PHP_VERSION=8.4
# Xdebug modes, comma-separated (no space after the comma)
XDEBUG_MODE=develop,debug,coverage
3. Inspections: detecting missing and unused variables
The real advantage over a text editor shows up in the inspections. PhpStorm can compare an .env file against a file marked as a reference, .env.example, and flags variables that exist in .env.example but are missing from the actual .env. That catches exactly the failure case where a new team member clones a repository, copies .env.example, but overlooks a newly added variable simply because it does not yet exist in their own, older .env.
Conversely, PhpStorm also flags variables that are defined in .env but referenced nowhere else in the project as potentially unused. That helps identify configuration cruft that accumulates over time, for example variables for a long-removed integration that were never deleted out of caution. Both inspections can be enabled individually in the settings under Editor, Inspections, Environment Variables, and their severity level adjusted from hint to error.
4. How .env.example works as a living reference
For the inspections to work at all, PhpStorm needs to know which file acts as the reference schema. By default, PhpStorm automatically recognizes an .env.example or .env.dist in the same directory as a template; if needed, the mapping can also be set manually via the file's context menu under Mark File As Template. This reference file should be maintained consistently across the team, since its value depends entirely on it actually containing every relevant variable name, albeit with placeholder values instead of real credentials.
A proven workflow is to check .env.example into the repository while the actual .env stays excluded via .gitignore, so no real credentials get versioned. For every new environment variable a developer adds to their local .env, a corresponding entry with a placeholder value should be added to .env.example in the same commit. PhpStorm's inspection makes this discipline immediately visible, since any forgotten addition to .env.example shows up right away as a flagged missing variable for every other team member.
# .env.example: reference schema with placeholder values, versioned in git
COMPOSE_PROJECT_NAME=change-me
DB_ROOT_PASSWORD=change-me
DB_NAME=magento
DB_USER=magento
DB_PASSWORD=change-me
PHP_VERSION=8.4
XDEBUG_MODE=off
# New variable added as soon as it shows up in the local .env:
NEW_PAYMENT_GATEWAY_API_KEY=change-me
5. Auto-completion for variable names across the project
PhpStorm offers auto-completion for variable names within the .env file itself, based on entries already present in related files such as .env.example, which noticeably reduces typos in similarly named variables. For variable names that differ by only a single character, such as DB_PASSWORD and DB_ROOT_PASSWORD, auto-completion reliably prevents accidentally recreating the wrong variable instead of updating the existing one.
Auto-completion becomes even more useful when PHP or shell code accesses environment variables, for example via getenv() in PHP or $VARIABLE_NAME in shell scripts. With the .env plugin installed, PhpStorm suggests the variable names actually defined in the .env file at such spots, preventing another common failure case: accessing a variable that was simply never defined because it was mistyped in the PHP code.
<?php
// PhpStorm only suggests names actually defined in .env here,
// making typos like DB_PASSWROD immediately obvious
declare(strict_types=1);
$dbHost = getenv('DB_HOST') ?: 'db';
$dbName = getenv('DB_NAME') ?: 'magento';
$dbUser = getenv('DB_USER') ?: 'magento';
$dbPassword = getenv('DB_PASSWORD') ?: '';
6. Handling multiple .env files within one project
Many projects maintain more than one .env file, for example a base .env for Docker Compose, an .env.local for personal overrides, and separate .env files for test or staging environments. PhpStorm treats each of these files individually according to the same rules but does not automatically recognize the override logic between them, meaning it does not know on its own that a variable in .env.local overrides a same-named variable in .env at runtime.
To avoid confusion here, a consistent naming convention for the files themselves helps, along with a short comment block at the top of each .env file documenting the load order and priority. For more complex setups with several environment tiers, it is additionally worth maintaining a central README that clearly describes the meaning of each individual .env variant, since PhpStorm's inspections check consistency within a single file but do not model project-wide override logic across multiple files.
7. Protecting sensitive data in .env files from accidental commits
Besides inspections for missing variables, PhpStorm also integrates with git ignore rules: if an .env file is excluded from version control via .gitignore, the project tree shows it visually dimmed, which makes accidentally adding it to a commit harder. Additionally, PhpStorm explicitly warns in the commit dialog when attempting to add a file marked as ignored anyway, serving as a second safety net against accidentally versioned credentials.
For especially sensitive values such as API keys or production database passwords, it is additionally recommended to only enter the variable name with a speaking placeholder like change-me or replace-with-real-value in .env.example, never a real, even if expired, value. That prevents a seemingly harmless reference file from accidentally leaking real, reusable credentials into version control.
8. Typical failure patterns caught early by inspections
A common failure pattern is a variable written in camelCase instead of the project-wide, consistent SCREAMING_SNAKE_CASE convention, which is often hard to spot from the outside but causes code that accesses the correctly spelled name to silently fall back to an empty default value. PhpStorm's auto-completion makes such deviations visible, since the incorrectly spelled name simply does not appear in the suggestion list.
A second, more subtle failure pattern is leading or trailing whitespace around the equals sign, which different .env parsers interpret differently, some parsers treat KEY = VALUE differently from KEY=VALUE. PhpStorm's syntax highlighting makes such deviations from the expected formatting visible through a slightly different rendering, before they turn into a hard-to-trace runtime issue inside the container.
9. Practical workflow for new environment variables
A robust workflow starts by first entering every new environment variable in .env.example with a speaking placeholder value, before filling in the real value in one's own local .env. That way the reference file always stays the leading source of truth, and PhpStorm's missing-variable inspection immediately kicks in for every other team member the next time they open their .env file, without anyone needing to actively ask which new variable is now required.
Before every merge into the main branch, it is worth taking a brief, deliberate look at the inspection warnings on your own .env file, to make sure no variable was forgotten and no obviously misspelled names were overlooked. This check, which takes only a few seconds, reliably prevents the far more common case where a configuration error only surfaces at the next deploy, or for a new team member, when the root cause is already considerably harder to reconstruct.
| Feature | What it shows | Where configurable | Practical benefit |
|---|---|---|---|
| Syntax highlighting | Keys, values, comments separated by color | Active automatically | Quickly scanning long .env files |
| Missing-variable inspection | Present in .env.example, missing from .env | Editor, Inspections, Environment Variables | Prevents silent defaults after cloning a repository |
| Unused-variable inspection | Defined in .env, not referenced in the project | Editor, Inspections, Environment Variables | Uncovers configuration cruft |
| Auto-completion | Known variable names from .env/.env.example | Active automatically | Prevents typos in similarly named variables |
| Git-ignore display | Dimmed rendering of ignored .env files | Automatic, based on .gitignore | Protects against accidental commits of sensitive data |
Mironsoft
PhpStorm setup, Docker integration, and team productivity
PhpStorm that actually runs optimally for Magento and PHP projects?
We review existing PhpStorm setups for slow indexing, unused Docker integration, and missing team conventions, then set up a configuration that is productive from the first second.
Setup Review
Optimizing indexing, interpreter, and memory settings for large Magento projects.
Docker Integration
Cleanly connecting Xdebug, PHPUnit, and database tools to the Docker setup.
Team Conventions
Standardizing inspection profiles, code style, and live templates project-wide.
10. Summary
.env Files in PhpStorm: The Essentials at a Glance
Dedicated file type
PhpStorm recognizes .env files with dedicated highlighting instead of treating them as plain text.
Inspection against a reference
Missing and unused variables are automatically compared against .env.example.
Maintain .env.example
Only a consistently up-to-date reference file makes the inspections genuinely useful.
Protection against leaks
Ignored .env files are visually flagged and additionally guarded in the commit dialog.