|
| 1 | +# CLI Reference |
| 2 | + |
| 3 | +PHPantom is a language server, but it also ships CLI tools for batch |
| 4 | +analysis and automated fixing. These run the same engine that powers the |
| 5 | +editor, so results are consistent. |
| 6 | + |
| 7 | +## Modes |
| 8 | + |
| 9 | +| Command | Purpose | |
| 10 | +| ------------------------ | ---------------------------------------------------- | |
| 11 | +| `phpantom_lsp` | Start the LSP server over stdin/stdout (the default) | |
| 12 | +| `phpantom_lsp analyze` | Report diagnostics across the project | |
| 13 | +| `phpantom_lsp fix` | Apply automated code fixes across the project | |
| 14 | +| `phpantom_lsp init` | Generate a default `.phpantom.toml` config file | |
| 15 | + |
| 16 | +Running with no subcommand starts the language server. Editors launch |
| 17 | +this automatically; you never need to run it by hand. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## `analyze` — Batch diagnostics |
| 22 | + |
| 23 | +Scans PHP files and reports PHPantom's own diagnostics (no PHPStan, no |
| 24 | +external tools) in a PHPStan-style table format. Use it to find spots |
| 25 | +where the LSP cannot resolve a symbol, so you can achieve full |
| 26 | +completion coverage. |
| 27 | + |
| 28 | +```sh |
| 29 | +phpantom_lsp analyze # scan entire project |
| 30 | +phpantom_lsp analyze src/ # scan a subdirectory |
| 31 | +phpantom_lsp analyze src/Foo.php # scan a single file |
| 32 | +phpantom_lsp analyze --severity warning # errors and warnings only |
| 33 | +phpantom_lsp analyze --severity error # errors only |
| 34 | +phpantom_lsp analyze --project-root /path/to/app # explicit project root |
| 35 | +phpantom_lsp analyze --no-colour # plain text output |
| 36 | +``` |
| 37 | + |
| 38 | +### Options |
| 39 | + |
| 40 | +| Flag | Description | |
| 41 | +| -------------------------- | ---------------------------------------------------------------- | |
| 42 | +| `[PATH]` | File or directory to analyze. Defaults to the entire project. | |
| 43 | +| `--severity <LEVEL>` | Minimum severity: `all` (default), `warning`, or `error`. | |
| 44 | +| `--project-root <DIR>` | Project root directory. Defaults to the current working directory.| |
| 45 | +| `--no-colour` | Disable ANSI colour output. | |
| 46 | + |
| 47 | +### Exit codes |
| 48 | + |
| 49 | +| Code | Meaning | |
| 50 | +| ---- | ----------------------- | |
| 51 | +| 0 | No diagnostics found | |
| 52 | +| 1 | Diagnostics were found | |
| 53 | + |
| 54 | +### Example output |
| 55 | + |
| 56 | +``` |
| 57 | + ------ ------------------------------------------- |
| 58 | + Line src/Service/UserService.php |
| 59 | + ------ ------------------------------------------- |
| 60 | + 15 Unknown class 'App\Models\LegacyUser'. |
| 61 | + 🪪 unknown_class |
| 62 | + 42 Call to undefined method Post::archive(). |
| 63 | + 🪪 unknown_member |
| 64 | + ------ ------------------------------------------- |
| 65 | +``` |
| 66 | + |
| 67 | +### Reported diagnostics |
| 68 | + |
| 69 | +The analyze command reports the same diagnostics you see in your editor. |
| 70 | +Each has a rule identifier shown below the message. |
| 71 | + |
| 72 | +| Identifier | Severity | Description | |
| 73 | +| ------------------------ | -------- | ---------------------------------------------------- | |
| 74 | +| `syntax_error` | Error | PHP parse errors | |
| 75 | +| `unknown_class` | Warning | Class, interface, trait, or enum not resolvable | |
| 76 | +| `unknown_member` | Warning | Property or method not found on the resolved class | |
| 77 | +| `unknown_function` | Error | Function call not resolvable | |
| 78 | +| `argument_count` | Error | Wrong number of arguments to a function or method | |
| 79 | +| `implementation_error` | Error | Missing required interface or abstract methods | |
| 80 | +| `scalar_member_access` | Error | Member access on a scalar type (int, string, etc.) | |
| 81 | +| `unused_import` | Hint | `use` statement with no references in the file | |
| 82 | +| `deprecated` | Hint | Reference to a `@deprecated` symbol | |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## `fix` — Automated code fixes |
| 87 | + |
| 88 | +Applies code fixes across the project, modeled after |
| 89 | +[php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer). Specify |
| 90 | +which rules to run, or omit `--rule` to run all preferred native fixers. |
| 91 | + |
| 92 | +```sh |
| 93 | +phpantom_lsp fix # apply all preferred fixers |
| 94 | +phpantom_lsp fix --rule unused_import # only remove unused imports |
| 95 | +phpantom_lsp fix --rule unused_import --rule deprecated # multiple rules |
| 96 | +phpantom_lsp fix --dry-run # preview without writing |
| 97 | +phpantom_lsp fix src/ # restrict to a subdirectory |
| 98 | +phpantom_lsp fix src/Foo.php # fix a single file |
| 99 | +phpantom_lsp fix --project-root /path/to/app # explicit project root |
| 100 | +``` |
| 101 | + |
| 102 | +### Options |
| 103 | + |
| 104 | +| Flag | Description | |
| 105 | +| -------------------------- | -------------------------------------------------------------------- | |
| 106 | +| `[PATH]` | File or directory to fix. Defaults to the entire project. | |
| 107 | +| `--rule <RULE>` | Rule to apply (repeatable). Omit to run all preferred native rules. | |
| 108 | +| `--dry-run` | Report what would change without writing files. | |
| 109 | +| `--with-phpstan` | Enable PHPStan-based fixers (runs PHPStan to collect diagnostics). | |
| 110 | +| `--project-root <DIR>` | Project root directory. Defaults to the current working directory. | |
| 111 | +| `--no-colour` | Disable ANSI colour output. | |
| 112 | + |
| 113 | +### Exit codes |
| 114 | + |
| 115 | +| Code | Meaning | |
| 116 | +| ---- | ------------------------------------------------ | |
| 117 | +| 0 | Fixes applied successfully (or nothing to fix) | |
| 118 | +| 1 | Error (bad arguments, write failure, etc.) | |
| 119 | +| 2 | Dry-run found fixable issues (nothing written) | |
| 120 | + |
| 121 | +### Available rules |
| 122 | + |
| 123 | +Rules correspond to diagnostic identifiers. Native rules run without |
| 124 | +external tools. PHPStan rules require `--with-phpstan` and are prefixed |
| 125 | +with `phpstan.`. |
| 126 | + |
| 127 | +**Native rules (shipped):** |
| 128 | + |
| 129 | +| Rule | Description | |
| 130 | +| ------------------ | --------------------------------------------------------- | |
| 131 | +| `unused_import` | Remove unused `use` statements. Handles simple imports, group imports (removes individual members), and collapses leftover blank lines. | |
| 132 | + |
| 133 | +**PHPStan rules (planned, requires `--with-phpstan`):** |
| 134 | + |
| 135 | +These are not yet implemented. See the [fix CLI roadmap](todo/fix-cli.md) |
| 136 | +for details. |
| 137 | + |
| 138 | +| Rule | Description | |
| 139 | +| -------------------------------------------- | --------------------------------------------------- | |
| 140 | +| `phpstan.return.unusedType` | Remove unused type from a return union | |
| 141 | +| `phpstan.missingType.iterableValue` | Add `@return array<mixed>` for untyped iterables | |
| 142 | +| `phpstan.property.unused` | Remove unused property declarations | |
| 143 | +| `phpstan.method.unused` | Remove unused method declarations | |
| 144 | +| `phpstan.generics.callSiteVarianceRedundant` | Remove redundant variance annotations in docblocks | |
| 145 | + |
| 146 | +### Example output |
| 147 | + |
| 148 | +``` |
| 149 | + ------ ------------------------------------------- |
| 150 | + Line src/Service/UserService.php |
| 151 | + ------ ------------------------------------------- |
| 152 | + 5 Unused import 'App\Models\LegacyUser' |
| 153 | + 🔧 unused_import |
| 154 | + 6 Unused import 'App\Support\OldHelper' |
| 155 | + 🔧 unused_import |
| 156 | + ------ ------------------------------------------- |
| 157 | +
|
| 158 | + [FIXED] Applied 2 fixes across 1 file |
| 159 | +``` |
| 160 | + |
| 161 | +### Dry-run example |
| 162 | + |
| 163 | +```sh |
| 164 | +phpantom_lsp fix --dry-run --project-root /path/to/app |
| 165 | +``` |
| 166 | + |
| 167 | +``` |
| 168 | + ------ ------------------------------------------- |
| 169 | + Line src/Service/UserService.php |
| 170 | + ------ ------------------------------------------- |
| 171 | + 5 Unused import 'App\Models\LegacyUser' |
| 172 | + 🔧 unused_import |
| 173 | + ------ ------------------------------------------- |
| 174 | +
|
| 175 | + [DRY RUN] 1 fix in 1 file (not applied) |
| 176 | +``` |
| 177 | + |
| 178 | +Use `--dry-run` in CI to enforce that imports stay clean: |
| 179 | + |
| 180 | +```sh |
| 181 | +phpantom_lsp fix --dry-run --rule unused_import --project-root . |
| 182 | +# Exit code 2 means fixable issues exist → fail the build. |
| 183 | +``` |
| 184 | + |
| 185 | +### Idempotency |
| 186 | + |
| 187 | +Running `fix` twice produces the same result as running it once. If |
| 188 | +all issues are already resolved, the command exits with code 0 and |
| 189 | +writes nothing. |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## `init` — Generate config file |
| 194 | + |
| 195 | +Creates a default `.phpantom.toml` in the current directory with all |
| 196 | +options documented and commented out. Safe to run if the file already |
| 197 | +exists (it will not overwrite). |
| 198 | + |
| 199 | +```sh |
| 200 | +phpantom_lsp init |
| 201 | +``` |
| 202 | + |
| 203 | +See [Project Configuration](SETUP.md#project-configuration) for details |
| 204 | +on available settings. |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +## Common patterns |
| 209 | + |
| 210 | +### CI pipeline: diagnostics gate |
| 211 | + |
| 212 | +Fail the build when PHPantom finds unresolvable symbols: |
| 213 | + |
| 214 | +```sh |
| 215 | +phpantom_lsp analyze --severity warning --project-root . --no-colour |
| 216 | +``` |
| 217 | + |
| 218 | +### CI pipeline: enforce clean imports |
| 219 | + |
| 220 | +Fail the build when unused imports exist: |
| 221 | + |
| 222 | +```sh |
| 223 | +phpantom_lsp fix --dry-run --rule unused_import --project-root . --no-colour |
| 224 | +``` |
| 225 | + |
| 226 | +### Pre-commit hook: auto-fix imports |
| 227 | + |
| 228 | +Clean up imports before every commit: |
| 229 | + |
| 230 | +```sh |
| 231 | +phpantom_lsp fix --rule unused_import --project-root . |
| 232 | +``` |
| 233 | + |
| 234 | +### Combine analyze and fix |
| 235 | + |
| 236 | +Run fixes first, then check what remains: |
| 237 | + |
| 238 | +```sh |
| 239 | +phpantom_lsp fix --project-root . |
| 240 | +phpantom_lsp analyze --project-root . |
| 241 | +``` |
0 commit comments