adachsoft / code-operations-php
PHP implementation for validating, testing and running Composer projects with configurable PHP runtimes.
Requires
- php: ^8.3
- adachsoft/code-operations-contracts: ^0.1 || ^0.2
- adachsoft/command-executor-lib: ^2.4
- adachsoft/php-interpreter-resolver: ^0.1
- adachsoft/phpstan-runner-lib: ^0.2
- adachsoft/phpunit-runner-lib: ^0.2
- composer/semver: ^3.4
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP 8.3+ implementation of adachsoft/code-operations-contracts for validating, testing and running Composer projects.
The implementation delegates runtime discovery to adachsoft/php-interpreter-resolver, PHPUnit execution and issue parsing to adachsoft/phpunit-runner-lib, and PHPStan execution and issue parsing to adachsoft/phpstan-runner-lib.
Requirements
- PHP 8.3 or later.
- Composer.
Installation
composer require adachsoft/code-operations-php
Usage
Create the library runners with their factories and inject them into the PHP implementation:
<?php
declare(strict_types=1);
use AdachSoft\CodeOperationsContracts\ExecutionContextDto;
use AdachSoft\CodeOperationsContracts\RuntimeVo;
use AdachSoft\CodeOperationsPhp\PhpCodeOperations;
use AdachSoft\CodeOperationsPhp\PhpRuntimeResolver;
use AdachSoft\CommandExecutorLib\SimpleCommandExecutor;
use AdachSoft\CommandExecutorLib\SimpleCommandRunner;
use AdachSoft\PhpInterpreterResolver\PhpInterpreterResolverFactory;
use AdachSoft\PhpStanRunnerLib\Config\PhpStanRunnerConfigDto;
use AdachSoft\PhpStanRunnerLib\PhpStanRunnerFactory;
use AdachSoft\PhpUnitRunnerLib\Config\PhpUnitRunnerConfigDto;
use AdachSoft\PhpUnitRunnerLib\PhpUnitRunnerFactory;
$projectPath = getcwd();
$commandExecutor = new SimpleCommandExecutor();
$commandRunner = new SimpleCommandRunner();
$interpreterResolver = PhpInterpreterResolverFactory::create($commandExecutor, $projectPath);
$runtimeResolver = new PhpRuntimeResolver($interpreterResolver);
$phpUnitRunner = PhpUnitRunnerFactory::create(
new PhpUnitRunnerConfigDto($projectPath),
$commandExecutor,
);
$phpStanRunner = PhpStanRunnerFactory::create(
new PhpStanRunnerConfigDto($projectPath),
$commandExecutor,
);
$operations = new PhpCodeOperations($runtimeResolver, $commandRunner, $phpUnitRunner, $phpStanRunner);
$context = new ExecutionContextDto(
projectPath: $projectPath,
runtime: new RuntimeVo('php', '8.3'),
);
$result = $operations->validate($context);
Runtime selection
- An explicit runtime version selects the highest available logical version beginning with the requested prefix.
- Without an explicit version, the resolver reads
require.phpfrom the project'scomposer.jsonand selects the highest available version satisfying that Composer constraint, usingcomposer/semver. - Without an explicit version or Composer PHP constraint, the resolver selects the version marked as current by
php-interpreter-resolver. - If interpreter discovery returns no versions or cannot query the system, the resolver uses
PHP_BINARYandPHP_VERSIONas its only available runtime.
Validation
validate() runs PHP's syntax checker for PHP files under the requested paths, or under src/ and tests/ when no paths are provided. It excludes vendor/ from linting.
When the project has phpstan.neon or phpstan.neon.dist and vendor/bin/phpstan exists, validation also runs PHPStan. Requested paths are analyzed individually; without requested paths, PHPStan analyzes the project root. Validation succeeds only when linting succeeds and PHPStan reports no errors.
PHPUnit tests
test() uses phpunit-runner-lib for execution and structured diagnostics. It accepts an optional PHPUnit filter and forwards the selected PHP version and timeout. Since the library request accepts one path, only the first requested path is passed to PHPUnit; if no path is provided, PHPUnit runs its configured suite.
Running a target
run() executes the requested target with the resolved PHP interpreter and the supplied arguments.