Overview
RHP ships with an integrated code formatter accessible via the unified tooling command:
rhp run lint [PATH] [OPTIONS]
Inspired by Carthage Software Mago, the formatter operates directly on concrete syntax trees and token streams. It normalizes indentation, formats braces, aligns operators, and outputs clean, idiomatic code adhering to the PER Coding Style (PER-CS 2.0) while preserving comments, docblocks, and string literals verbatim.
Formatting Invariants
- 4-Space Indentation: All blocks, class bodies, functions, match arms, and array literals use exactly 4 spaces per nesting level. Tab characters (
\t) are automatically converted. - K&R / 1TBS Brace Style: Opening curly braces stay on the same line for control structures (
if,while,for,match) and functions, while class declarations align according to the PSR-12 standard. - Operator Spacing: Strict single spacing surrounding binary operators (
$a + $b,$x === $y,$x => $y). - Clean Commas: Consistent comma placement across multi-line array literals and function parameter lists for clean git diffs.
- Verbatim Literal Preservation: Single-quoted, double-quoted, heredoc/nowdoc strings, and inline/docblock comments are preserved with byte-exact precision.
Execution Modes
Apply Formatting & Fixes
# Format current directory
rhp run lint
# Format specific directory or file
rhp run lint src/
rhp run lint app/Models/Order.rhp
Dry-Run Check Mode
To check if files comply with the formatter without modifying files on disk:
rhp run lint src/ --check
# or
rhp run lint src/ --no-fix
Returns exit code 0 if all files match formatting standards, or 1 with diagnostics if formatting or type errors exist.
Integration with Linter & Type Inferencer
The formatter runs as the first stage of the rhp run lint pipeline, immediately followed by:
- Automated Type Inference: Inferring scalar types,
list,dict,hashMap, and parameter defaults. - Casing Normalization: Enforcing
snake_casefor variables,PascalCasefor classes,camelCasefor functions, withsnake_casefortest*functions. - Static Typecheck: Emitting machine-readable diagnostics in Mago-compatible JSON format.
For full details on type inference and casing rules, see the Linter and Static Typing documentation.