Skip to content

Commit 5773272

Browse files
RahulHereRahulHere
authored andcommitted
Add PHP-CS-Fixer for code formatting
Set up PHP code formatting with PHP-CS-Fixer: - Add friendsofphp/php-cs-fixer as dev dependency - Create .php-cs-fixer.php configuration (PSR-12 based) - Add composer scripts: cs-fix, cs-check - Update .gitignore with .php-cs-fixer.cache - Add Code Formatting section to README - Apply formatting fixes to all PHP files Changes: - composer.json: Add php-cs-fixer dependency and scripts - .php-cs-fixer.php: Configuration file - .gitignore: Add cache file - README.md: Add Code Formatting section - src/*.php: Apply PSR-12 formatting - tests/*.php: Apply formatting, remove unused imports - examples/*.php: Apply formatting
1 parent 5ca7ca4 commit 5773272

11 files changed

Lines changed: 64 additions & 6 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ composer.lock
88
phpunit.xml
99
coverage/
1010

11+
# PHP-CS-Fixer
12+
.php-cs-fixer.cache
13+
1114
# IDE - PhpStorm
1215
.idea/
1316
*.iws

.php-cs-fixer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__ . '/src')
5+
->in(__DIR__ . '/tests')
6+
->in(__DIR__ . '/examples')
7+
->name('*.php')
8+
->ignoreDotFiles(true)
9+
->ignoreVCS(true);
10+
11+
return (new PhpCsFixer\Config())
12+
->setRules([
13+
'@PSR12' => true,
14+
'array_syntax' => ['syntax' => 'short'],
15+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
16+
'no_unused_imports' => true,
17+
'single_quote' => true,
18+
'trailing_comma_in_multiline' => true,
19+
'blank_line_before_statement' => [
20+
'statements' => ['return', 'throw', 'try'],
21+
],
22+
])
23+
->setFinder($finder)
24+
->setRiskyAllowed(false);

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,23 @@ ANTHROPIC_API_KEY=your-key php examples/client_example_json.php
491491

492492
## Development
493493

494+
### Code Formatting
495+
496+
```bash
497+
# Format code
498+
composer cs-fix
499+
500+
# Check formatting without making changes
501+
composer cs-check
502+
503+
# Or run directly
504+
./vendor/bin/php-cs-fixer fix
505+
./vendor/bin/php-cs-fixer fix --dry-run --diff
506+
507+
# Run tests
508+
./vendor/bin/phpunit --testdox
509+
```
510+
494511
### Project Structure
495512

496513
```

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ext-ffi": "*"
1717
},
1818
"require-dev": {
19+
"friendsofphp/php-cs-fixer": "^3.93",
1920
"phpunit/phpunit": "^9.5"
2021
},
2122
"autoload": {
@@ -30,7 +31,9 @@
3031
},
3132
"scripts": {
3233
"test": "phpunit --testdox",
33-
"test-coverage": "phpunit --coverage-html coverage"
34+
"test-coverage": "phpunit --coverage-html coverage",
35+
"cs-fix": "php-cs-fixer fix",
36+
"cs-check": "php-cs-fixer fix --dry-run --diff"
3437
},
3538
"config": {
3639
"sort-packages": true

examples/client_example_json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@
7171
$agent->dispose();
7272

7373
} catch (Exception $e) {
74-
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
74+
fwrite(STDERR, 'Error: ' . $e->getMessage() . "\n");
7575
exit(1);
7676
}

src/ConfigBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static function create(): self
3131
public function withProvider(string $provider): self
3232
{
3333
$this->provider = $provider;
34+
3435
return $this;
3536
}
3637

@@ -43,6 +44,7 @@ public function withProvider(string $provider): self
4344
public function withModel(string $model): self
4445
{
4546
$this->model = $model;
47+
4648
return $this;
4749
}
4850

@@ -56,6 +58,7 @@ public function withModel(string $model): self
5658
public function withApiKey(string $apiKey): self
5759
{
5860
$this->apiKey = $apiKey;
61+
5962
return $this;
6063
}
6164

@@ -69,6 +72,7 @@ public function withApiKey(string $apiKey): self
6972
public function withServerConfig(string $serverConfig): self
7073
{
7174
$this->serverConfig = $serverConfig;
75+
7276
return $this;
7377
}
7478

src/GopherAgent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static function create(Config $config): self
6464
$errorMsg = GopherOrch::getLastError();
6565
GopherOrch::clearError();
6666
$message = $errorMsg !== '' ? $errorMsg : 'Failed to create agent';
67+
6768
throw new AgentException($message);
6869
}
6970

@@ -165,6 +166,7 @@ public function runDetailedWithTimeout(string $query, int $timeoutMs): AgentResu
165166
{
166167
try {
167168
$response = $this->runWithTimeout($query, $timeoutMs);
169+
168170
return AgentResult::success($response);
169171
} catch (DisposedException $e) {
170172
return AgentResult::error($e->getMessage());
@@ -173,6 +175,7 @@ public function runDetailedWithTimeout(string $query, int $timeoutMs): AgentResu
173175
if (stripos($e->getMessage(), 'timeout') !== false) {
174176
return AgentResult::timeout($e->getMessage());
175177
}
178+
176179
return AgentResult::error($e->getMessage());
177180
}
178181
}

src/GopherOrch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public static function isAvailable(): bool
8585
{
8686
try {
8787
self::init();
88+
8889
return true;
8990
} catch (LibraryException $e) {
9091
return false;
@@ -149,6 +150,7 @@ public static function getFFI(): FFI
149150
public static function agentCreateByJson(string $provider, string $model, string $serverJson)
150151
{
151152
$ffi = self::getFFI();
153+
152154
return $ffi->gopher_orch_agent_create_by_json($provider, $model, $serverJson);
153155
}
154156

@@ -163,6 +165,7 @@ public static function agentCreateByJson(string $provider, string $model, string
163165
public static function agentCreateByApiKey(string $provider, string $model, string $apiKey)
164166
{
165167
$ffi = self::getFFI();
168+
166169
return $ffi->gopher_orch_agent_create_by_api_key($provider, $model, $apiKey);
167170
}
168171

tests/ConfigBuilderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace GopherSecurity\Orch\Tests;
66

7-
use GopherSecurity\Orch\Config;
87
use GopherSecurity\Orch\ConfigBuilder;
98
use PHPUnit\Framework\TestCase;
109

tests/GopherAgentIntegrationTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace GopherSecurity\Orch\Tests;
66

77
use GopherSecurity\Orch\ConfigBuilder;
8-
use GopherSecurity\Orch\GopherAgent;
9-
use GopherSecurity\Orch\GopherOrch;
108
use GopherSecurity\Orch\Exception\AgentException;
119
use GopherSecurity\Orch\Exception\DisposedException;
10+
use GopherSecurity\Orch\GopherAgent;
11+
use GopherSecurity\Orch\GopherOrch;
1212
use PHPUnit\Framework\TestCase;
1313

1414
/**
@@ -95,6 +95,7 @@ public function testCreateAgentWithServerConfig(): void
9595
->build();
9696

9797
$agent = null;
98+
9899
try {
99100
$agent = GopherAgent::create($config);
100101
$this->assertNotNull($agent, 'Agent should be created');
@@ -115,6 +116,7 @@ public function testCreateAgentWithHelperMethod(): void
115116
$this->skipIfNativeLibraryNotAvailable();
116117

117118
$agent = $this->tryCreateAgent();
119+
118120
try {
119121
$this->assertNotNull($agent);
120122
} finally {
@@ -153,6 +155,7 @@ public function testRunDetailedReturnsResult(): void
153155
$this->skipIfNativeLibraryNotAvailable();
154156

155157
$agent = $this->tryCreateAgent();
158+
156159
try {
157160
// Run with very short timeout to get quick response (likely timeout or error)
158161
$result = $agent->runDetailedWithTimeout('test query', 100);

0 commit comments

Comments
 (0)