# Add PHPStan to a CodeIgniter 4 Application

Yes. An agent adds PHPStan with the CodeIgniter extension, picks the highest level your app passes, wires it into Composer and CI, and opens a PR with what it found.

[Run this on Rush](https://cloud.getrush.ai/run/add-phpstan/codeigniter/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the application and its checks before choosing a level. In an app like codeigniter4/appstarter, `composer.json` requires PHP 8.2 and `codeigniter4/framework` 4.7, has `fakerphp/faker`, `mikey179/vfsstream` and `phpunit/phpunit` 10.5 as development dependencies, autoloads `App\` from `app/` and `Config\` from `app/Config/`, excludes `**/Database/Migrations/**` from the classmap, and defines one script, `test`, which runs `phpunit`. `phpunit.dist.xml` boots from `vendor/codeigniter4/framework/system/Test/bootstrap.php`, measures coverage over `app/` minus `app/Views` and `app/Config/Routes.php`, and sets `failOnRisky` and `failOnWarning`. `app/` holds about fifty `Config` classes, a `BaseController`, a `Home` controller whose `index()` returns `view('welcome_message')`, a single route in `Routes.php`, and empty `Models`, `Filters`, `Libraries` and `Helpers` directories. `.github/workflows/phpunit.yml` runs on pull requests to `develop` on PHP 8.2 and 8.5 through `shivammathur/setup-php` with the `intl`, `mbstring`, `xdebug` and `sqlite3` extensions and runs `vendor/bin/phpunit --coverage-text`. Nothing runs static analysis.

Then it installs the analyser the way the framework's own extension expects. `composer require --dev phpstan/phpstan codeigniter/phpstan-codeigniter phpstan/extension-installer`, with `phpstan/extension-installer` allowed under `config.allow-plugins` so `extension.neon` loads without an `includes` line. `phpstan.neon.dist` sets `paths` to `app` and `tests`, excludes `app/Views` to match the coverage source, and names `vendor/codeigniter4/framework/system/Test/bootstrap.php` under `bootstrapFiles`, because the CodeIgniter extension boots the framework during analysis to read services and configuration, and its model type inference runs migrations against a temporary SQLite database, which is why the `sqlite3` extension the workflow already installs matters. The cache directory the extension writes under `tmp/` goes into `.gitignore`.

The level is measured, not guessed. The agent runs `vendor/bin/phpstan analyse` at level 5, reads the result, and raises the level one step at a time until something is reported; the highest clean level goes into `phpstan.neon.dist`, and the errors the next level reports go into the pull request with file and line, so the team can see what raising it would cost. A starter with one controller is often clean at a high level and the report is short; an application with models and filters gets a longer list, and if the team's chosen level cannot be reached cleanly, the agent generates `phpstan-baseline.neon` with `--generate-baseline` and says how many entries it holds rather than lowering the level to hide them.

It wires the command where the other checks live. A `phpstan` entry joins `scripts` in `composer.json` beside `test`, and a `PHPStan` job joins `.github/workflows/phpunit.yml` on the newest PHP in the matrix, with the same `setup-php` action and Composer cache, running `composer phpstan` with `--error-format=github` so findings annotate the pull request. The agent runs `composer phpstan`, `composer test` and `actionlint` in its sandbox before opening the pull request and quotes all three results.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add PHPStan to this CodeIgniter 4 application. Read composer.json, phpunit.dist.xml, .github/workflows/phpunit.yml, app/Config/Routes.php, the controllers and anything under app/Models and app/Filters first. Install phpstan/phpstan, codeigniter/phpstan-codeigniter and phpstan/extension-installer as dev dependencies and allow the installer plugin in composer.json. Write phpstan.neon.dist with paths app and tests, app/Views excluded, and the framework's system/Test/bootstrap.php under bootstrapFiles; add the extension's tmp cache directory to .gitignore. Run the analysis starting at level 5 and raise the level one step at a time; set the highest level that passes cleanly and record what the next level reports with file and line. If the clean level is below 6, generate a baseline instead of lowering further and report its entry count. Add a phpstan script to composer.json and a PHPStan job to the existing workflow on the newest PHP in its matrix using the github error format. Run composer phpstan, composer test and actionlint, then open a pull request that states the level chosen, the findings at the next level and any fixes you made to reach the clean level, each as its own commit.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `phpstan.neon.dist`, three development dependencies and an `allow-plugins` entry in `composer.json` with the matching `composer.lock`, a `phpstan` script, a line in `.gitignore`, and a `PHPStan` job in `.github/workflows/phpunit.yml`. The description states the level that passes cleanly, lists the findings the next level reports with file and line, and, if a baseline was needed, how many entries it holds and which files they fall in. Any code change the agent made to reach the clean level is its own commit so a reviewer can read it apart from the configuration.

The run's record keeps every `phpstan analyse` run at every level the agent tried, the `composer test` output showing the suite still passes with the new dependencies installed, and the `actionlint` result.

Before merging, a reviewer reads the next-level findings and decides whether to raise the level now or later, checks that the baseline, if there is one, holds only entries the team is willing to carry, and confirms the workflow job runs on the pull request itself.

## Run it on every CodeIgniter service without static analysis

A team with several CodeIgniter applications sends this body once per repository to `POST /o/{org}/sessions`, each with its own repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started rather than a second pull request. Each run measures its own application's level, so a service with models and filters lands at a different level from a near-empty starter, and each pull request says which.

With a `webhook_url` on the key, every `run.completed` carries the pull request in `result`, and the chosen level is in the first line of each description, so a spreadsheet of where every service stands falls out of the webhook payloads without opening the pull requests.

## Why run it on Rush?

Choosing a PHPStan level by hand means running the analyser six or seven times, reading each report and deciding, which is exactly the loop a person skips by picking level 5 and moving on. The agent runs the loop in its own sandbox with the framework booted the way the CodeIgniter extension needs, and the pull request states the measured level and what the next one costs.

Every analysis run stays with the record, so a reviewer who doubts the level can read the output at level 7 or 8 directly rather than rerunning it, and the test suite output proves the new development dependencies broke nothing.

## Frequently asked questions

### Why the CodeIgniter extension and not plain PHPStan?

Plain PHPStan does not know that `service('validation')` returns a validation object or what a model's `find()` returns. The `codeigniter/phpstan-codeigniter` extension boots the framework during analysis, resolves services and configuration, and infers model and entity types from your migrations, which removes most false positives at higher levels.

### What level will it choose?

The highest one that passes cleanly on `app/` and `tests/`, found by running the analyser one level at a time from 5. The pull request names the level and lists what the next level reports, so raising it later is a decision made with the list in hand.

### Will the analysis need a database?

Only a temporary one. The extension's model type inference runs your migrations against a throwaway SQLite database, so the `sqlite3` PHP extension must be present; the existing workflow already installs it, and the cache the extension writes under `tmp/` is ignored by git.

### Does it fix the findings?

Small, clear ones that stand between the application and the next clean level, each as a separate commit so they can be reviewed or dropped. Anything larger stays in the report or the baseline for the team to handle.

## More CodeIgniter jobs

- [Add Rate Limiting to a CodeIgniter 4 App](https://getrush.ai/run/add-rate-limiting/codeigniter/)
- [Add Request ID Logging to a CodeIgniter 4 App](https://getrush.ai/run/add-request-id-logging/codeigniter/)
- [Add a REST API to a CodeIgniter 4 App](https://getrush.ai/run/add-rest-api/codeigniter/)

## The same job on other stacks

- [Add PHPStan Static Analysis to a PHP Library](https://getrush.ai/run/add-phpstan/php/)

## More jobs like this: Hand off a long task

- [Post Terraform Plan Comments on Pull Requests](https://getrush.ai/run/add-plan-comments-to-pr/terraform/)
- [Add PMD Static Analysis to a Maven Build](https://getrush.ai/run/add-pmd/maven/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
