Generate PHPStan type annotations from var_dump output
A CLI tool that analyzes PHP var_dump() output and generates PHPStan-compatible type annotations, helping you add type hints to legacy code or complex data structures.
π Try hell2shape in your browser - No installation required. Works locally in your browser, no data transferred -- thanks to php-wasm.
Download the latest hell2shape.phar from the releases page and use it directly:
# Download the PHAR
curl -L https://github.com/Feolius/hell2shape/releases/latest/download/hell2shape.phar -o hell2shape.phar
# Make it executable
chmod +x hell2shape.phar
# Use it
php -r 'var_dump($myArray);' | ./hell2shape.pharcomposer require feolius/hell2shapeRequirements:
- PHP 8.3 or higher
# If installed via Composer
php -r 'var_dump($myArray);' | vendor/bin/hell2shape
# If using PHAR
php -r 'var_dump($myArray);' | ./hell2shape.pharOption 3: Use cpx
php -r 'var_dump($myArray);' | cpx feolius/hell2shapePipe any var_dump() output to hell2shape:
# From a PHP script
php script.php | vendor/bin/hell2shape
# From a one-liner
php -r 'var_dump(["name" => "John", "age" => 30]);' | vendor/bin/hell2shapeInput (var_dump):
array(2) {
["name"]=>
string(4) "John"
["age"]=>
int(30)
}
Output (PHPStan type):
array{
name: string,
age: int
}Control indentation for multi-line output (default: 4 spaces)
# Single-line output
hell2shape --indent=0
# 2-space indentation
hell2shape -i 2
# Default 4-space indentation
hell2shapeControl key quoting style in array shapes
# No quotes (default)
hell2shape --quotes=none
# Output: array{name: string}
# Single quotes
hell2shape --quotes=single
# Output: array{'name': string}
# Double quotes
hell2shape --quotes=double
# Output: array{"name": string}Control how class names are formatted (default: unqualified)
# Unqualified - just the class name (default)
hell2shape --class=uqn
# Output: User
# Qualified - with namespace
hell2shape -c qn
# Output: App\Models\User
# Fully qualified - with leading backslash
hell2shape --class=fqn
# Output: \App\Models\User- β Scalar types (int, string, bool, float, null)
- β Arrays and nested arrays
- β Array shapes with optional keys
- β Objects (class names)
- β stdClass objects as object shapes
- β Lists with union types
- β Complex nested structures
- β Configurable formatting
php -r 'var_dump(["id" => 1, "name" => "Alice"]);' | hell2shapeOutput:
array{
id: int,
name: string
}php -r 'var_dump(["user" => ["name" => "Bob", "roles" => ["admin", "user"]]]);' | hell2shapeOutput:
array{
user: array{
name: string,
roles: list<string>
}
}php -r 'var_dump(["id" => 1, "active" => true]);' | hell2shape --indent=0Output:
array{id: int, active: bool}When arrays have different structures, hell2shape marks missing keys as optional:
array{
id: int,
name: string,
email?: string
}php -r 'namespace App\Models; class User {} var_dump(new User());' | hell2shapeOutput (default - unqualified):
UserWith qualified names:
php -r 'namespace App\Models; class User {} var_dump(new User());' | hell2shape -c qnOutput:
App\Models\UserWith fully qualified names:
php -r 'namespace App\Models; class User {} var_dump(new User());' | hell2shape --class=fqnOutput:
\App\Models\User- var_dump doesn't show all possible values, only the current state
- Union types are inferred from visible data only
- You should review and refine the generated types based on your actual usage
- Empty arrays are typed as
array(notlist<mixed>)
# Run tests
vendor/bin/phpunit
# Run PHPStan
vendor/bin/phpstan analyse
# Run code style checks
vendor/bin/ecs checkMIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.