adachsoft/phpstan-tool
AI Tool-Call SPI tool for running PHPStan static analysis with normalized base_path semantics.
This library exposes a single SPI tool, phpstan, that can be discovered and called through
adachsoft/ai-tool-call. It runs PHPStan via
adachsoft/command-executor-lib and resolves
all user-provided paths relative to a configured base_path using
adachsoft/normalized-safe-path.
Installation
Install via Composer:
composer require adachsoft/phpstan-tool
The exact version constraints for dependencies are defined in this package's composer.json.
High-level architecture
The package follows the AI Tool-Call architecture described in
.docs/AI_TOOL_CALL_BASE_PATH_SEMANTICS_AND_TOOL_ARCHITECTURE_PL.md:
Factory configuration (ConfigMap)
PhpStanToolFactory reads configuration from AdachSoft\AiToolCall\SPI\Collection\ConfigMap.
All keys are in snake_case and validated by ConfigMap:
-
base_path (string, optional)
- Absolute path that acts as the root for resolving user paths.
- Also used as the working directory (
cwd) for command execution.
- Default:
getcwd() ?: '.'.
-
phpstan_path (string, optional)
- Path to the PHPStan executable, relative to
base_path or absolute.
- Default:
vendor/bin/phpstan.
-
default_timeout (int|null, optional)
- Default timeout (seconds) for PHPStan execution when no per-call override is provided.
null means "no explicit timeout".
-
max_errors (int, optional)
- Default maximum number of errors to include in the processed output.
0 means "no limit" (only trimming by total output size applies).
- Default:
30.
Invalid types or empty strings for required config values cause ToolConfigurationException.
Tool contract: phpstan
Tool name
Parameters (per call)
All parameters are provided as entries in KeyValueMap (snake_case keys) and validated by
PhpStanTool. When validation fails, InvalidToolCallException is thrown.
-
path (string, required)
- Path to file or directory to analyse.
- If relative, it is resolved against
base_path using SafePathHelper::normalize().
-
error_type (string|null, optional)
- Simple substring filter applied to error sections.
- When set, only errors whose text contains this substring are kept.
-
timeout (int|null, optional)
- Per-call timeout in seconds.
null → falls back to default_timeout from configuration.
Result shape
The tool always returns a ToolCallResultDto with a KeyValueMap result containing exactly
these keys:
stdout (string)
stderr (string)
exit_code (int)
success (bool)
The stdout/stderr values may be trimmed and/or filtered by OutputProcessor according to the
rules described above, but the structural shape of the result is stable.
If the runner fails (invalid path, execution error, timeout, etc.), PhpStanExecutionException is
thrown in the runner layer and mapped to SpiToolExecutionException in the SPI tool layer.
base_path semantics
base_path follows the common semantics for AI tools built with adachsoft/ai-tool-call:
- All relative user paths are resolved against
base_path using SafePathHelper::normalize().
- All CLI processes are executed with
cwd = base_path.
- Where appropriate, absolute occurrences of
base_path in stdout/stderr are removed to keep
outputs portable and avoid leaking environment details.
- Unsafe paths (e.g. attempts to traverse outside
base_path) result in a domain exception rather
than direct command execution.
For full details, see .docs/AI_TOOL_CALL_BASE_PATH_SEMANTICS_AND_TOOL_ARCHITECTURE_PL.md.
Example usage (pseudo-code)
Below is a high-level example of how this tool can be wired with adachsoft/ai-tool-call. Exact
wiring depends on your application and how you register tool factories.
use AdachSoft\AiToolCall\SPI\Collection\ConfigMap;
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto;
use AdachSoft\PhpstanTool\Tool\PhpStanToolFactory;
$configMap = new ConfigMap([
'base_path' => '/app/project',
'phpstan_path' => 'vendor/bin/phpstan',
'default_timeout' => 60,
'max_errors' => 30,
]);
$factory = new PhpStanToolFactory();
$tool = $factory->create($configMap);
$request = new ToolCallRequestDto(
toolName: 'phpstan',
parameters: new KeyValueMap([
'path' => 'src',
]),
);
$result = $tool->callTool($request);
$stdout = $result->result->get('stdout');
$stderr = $result->result->get('stderr');
$exitCode = $result->result->get('exit_code');
$success = $result->result->get('success');
In a real application, the PhpStanToolFactory is typically registered in your AI tool registry
so that agents can discover and invoke phpstan dynamically.
License
This package is open-sourced software licensed under the MIT license.