https://github.com/rectorphp/argtyper
[WIP] Analyze real method argument types, and add them as type declarations
https://github.com/rectorphp/argtyper
Last synced: about 1 month ago
JSON representation
[WIP] Analyze real method argument types, and add them as type declarations
- Host: GitHub
- URL: https://github.com/rectorphp/argtyper
- Owner: rectorphp
- Created: 2023-08-27T11:23:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-24T15:55:01.000Z (2 months ago)
- Last Synced: 2025-11-30T02:10:11.439Z (about 2 months ago)
- Language: PHP
- Homepage:
- Size: 10.1 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fill Parameter Types based on Passed Values
There are often more known types in your project than meets the eye.
This tool detects the **real** types passed into method and function calls using PHPStan.
```php
$this->hotelOverview->makeRoomAvailable(324);
```
Later in the code...
```php
public function roomDetail(int $roomNumber)
{
$this->hotelOverview->makeRoomAvailable($roomNumber);
}
```
Later in tests...
```php
public function test(int $roomNumber): void
{
$this->hotelOverview->makeRoomAvailable($roomNumber);
}
```
✅ Three times an `int` value is passed into `makeRoomAvailable()`.
Then [Rector](https://getrector.com) runs and fills in the missing type declarations:
```diff
final class HotelOverview
{
- public function makeRoomAvailable($roomNumber)
+ public function makeRoomAvailable(int $roomNumber)
{
}
}
```
✅ An `int` parameter type is added to the `makeRoomAvailable()` method.
That’s it.
## Install
```bash
composer require rector/argtyper --dev
```
## Usage
Run it in your project directory:
```bash
vendor/bin/argtyper add-types .
```
Or on another project:
```bash
vendor/bin/argtyper add-types project
```
To see more details during the process, add the `--debug` option.
## How It Works
At first, a set of custom PHPStan rules scans your code and records the argument types passed to method calls, static calls, `new` expressions, and function calls. It stores this data in temporary `*.json` files in the following format:
```json
[
{
"class": "HotelOverview",
"method": "makeRoomAvailable",
"position": 0,
"type": "PHPStan\\Type\\IntegerType"
}
]
```
Then, custom Rector rules go through the codebase and fill in the known parameter types based on the collected data — but only where they’re missing.
With a few exceptions:
* If multiple types are found → it’s skipped.
* If union or intersection types are found → it’s skipped as ambiguous.
* If a `float` parameter type is declared but only `int` arguments are passed (e.g. `30.0`) → it’s skipped to avoid losing decimal precision.
## Verify the Results
It’s not 100 % perfect, but in our tests it fills in about **95 %** of the data correctly and saves a huge amount of manual work.
You can fix the remaining cases manually based on PHPStan or test feedback.
Happy coding!