styleci/sdk
Official PHP SDK for StyleCI: authenticate and interact with the API to manage repositories, fetch analyses, view fix results, and trigger or monitor code style checks from your Laravel or PHP applications.
Installation
composer require styleci/sdk
Add the SDK to your composer.json autoload if not using PSR-4:
"autoload": {
"psr-4": {
"App\\": "app/",
"StyleCI\\SDK\\": "vendor/styleci/sdk/src/"
}
}
First Use Case: Authenticating with StyleCI
use StyleCI\SDK\StyleCI;
$styleCI = new StyleCI([
'token' => env('STYLECI_TOKEN'),
'base_url' => env('STYLECI_BASE_URL', 'https://styleci.io/api/v1'),
]);
Key Classes to Explore
StyleCI (main client)StyleCI\SDK\Exceptions\StyleCIException (error handling)StyleCI\SDK\Resources\Project (project operations)StyleCI\SDK\Resources\Check (check status)First API Call
$projects = $styleCI->projects()->all();
Trigger Checks on Push
// In a GitHub Actions/Laravel Forge hook
$styleCI->checks()->create([
'project_id' => $projectId,
'branch' => 'main',
'target' => 'full',
]);
Polling for Results
$check = $styleCI->checks()->find($checkId);
while ($check->status === 'queued') {
sleep(5);
$check = $styleCI->checks()->find($checkId);
}
Syncing Local Projects
$styleCI->projects()->create([
'name' => 'My Laravel App',
'git_url' => 'https://github.com/user/repo.git',
'branch' => 'main',
'style_preset' => 'laravel',
]);
Batch Updates
$projects = $styleCI->projects()->all();
foreach ($projects as $project) {
$styleCI->projects()->update($project->id, [
'style_preset' => 'psr12',
]);
}
Service Provider Binding
// app/Providers/StyleCIServiceProvider.php
public function register()
{
$this->app->singleton(StyleCI::class, function ($app) {
return new StyleCI([
'token' => config('services.styleci.token'),
'base_url' => config('services.styleci.base_url'),
]);
});
}
Artisan Command for Checks
// app/Console/Commands/RunStyleCI.php
public function handle()
{
$styleCI = app(StyleCI::class);
$check = $styleCI->checks()->create([
'project_id' => $this->option('project'),
'branch' => $this->option('branch'),
]);
$this->info("Triggered check: {$check->id}");
}
Event Listeners for Git Hooks
// Listen to repo:push (Laravel Forge)
public function handle()
{
$styleCI = app(StyleCI::class);
$styleCI->checks()->create([
'project_id' => config('styleci.project_id'),
'branch' => request()->input('branch'),
]);
}
Enable Debug Mode
$styleCI = new StyleCI([
'token' => env('STYLECI_TOKEN'),
'debug' => true, // Enable debug logging
]);
storage/logs/laravel.log.Handling Rate Limits
StyleCI\SDK\Exceptions\RateLimitException.try {
$styleCI->projects()->all();
} catch (RateLimitException $e) {
sleep($e->getRetryAfter());
retry();
}
Base URL Overrides
STYLECI_BASE_URL for self-hosted instances:
STYLECI_BASE_URL=https://your-styleci.example.com/api/v1
Token Scopes
read:projects, write:checks scopes if needed.Custom HTTP Client
$styleCI = new StyleCI([
'token' => env('STYLECI_TOKEN'),
'http_client' => new CustomGuzzleClient(),
]);
Response Transformers
StyleCI\SDK\Resources\Resource to modify responses:
class CustomProject extends \StyleCI\SDK\Resources\Project
{
public function transform($data)
{
$data['formatted_name'] = strtolower($data['name']);
return parent::transform($data);
}
}
Mocking for Tests
StyleCI\SDK\Mock\StyleCIMock:
$mock = new StyleCIMock();
$mock->shouldReceive('projects->all')->andReturn([...]);
Deprecated Endpoints
/checks exist in your StyleCI version.Pagination Handling
$resource->getNextPageUrl() for paginated results:
$projects = $styleCI->projects()->all();
while ($nextPage = $projects->getNextPageUrl()) {
$projects = $styleCI->getHttpClient()->get($nextPage);
}
Webhook Validation
X-StyleCI-Signature. Validate in Laravel middleware:
public function handle($request, Closure $next)
{
$styleCI = app(StyleCI::class);
if ($styleCI->validateWebhook($request)) {
return $next($request);
}
abort(403);
}
How can I help you explore Laravel packages today?