-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidation_example.php
More file actions
195 lines (148 loc) · 4.61 KB
/
Copy pathvalidation_example.php
File metadata and controls
195 lines (148 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
require_once __DIR__ . '/load-env.php';
use ClaudeAgents\Validation\ValidationCoordinator;
use ClaudeAgents\Validation\Validators\PHPSyntaxValidator;
use ClaudeAgents\Validation\Validators\StaticAnalysisValidator;
use ClaudeAgents\Validation\Validators\CustomScriptValidator;
use ClaudeAgents\Validation\Validators\LLMReviewValidator;
use ClaudePhp\ClaudePhp;
/**
* Example: Validating PHP code with multiple validators.
*
* Demonstrates:
* - Setting up a ValidationCoordinator
* - Using different validator types
* - Handling validation results
* - Validator priorities
*/
$apiKey = getenv('ANTHROPIC_API_KEY');
if (! $apiKey) {
echo "Error: ANTHROPIC_API_KEY environment variable not set\n";
exit(1);
}
$client = new ClaudePhp(apiKey: $apiKey);
echo "=== Validation System Example ===\n\n";
// Setup validation coordinator
$coordinator = new ValidationCoordinator([
'stop_on_first_failure' => false, // Run all validators even if one fails
]);
// Add validators in priority order
$coordinator->addValidator(new PHPSyntaxValidator()); // Priority 10 - runs first
$coordinator->addValidator(new LLMReviewValidator($client)); // Priority 100 - runs last
echo "Registered validators:\n";
foreach ($coordinator->getValidators() as $validator) {
printf(
" - %s (priority: %d)\n",
$validator->getName(),
$validator->getPriority()
);
}
echo "\n";
// Example 1: Valid code
echo "Example 1: Validating VALID code\n";
echo str_repeat('-', 50) . "\n";
$validCode = <<<'PHP'
<?php
declare(strict_types=1);
namespace App;
class Calculator
{
public function add(int $a, int $b): int
{
return $a + $b;
}
public function multiply(int $a, int $b): int
{
return $a * $b;
}
}
PHP;
$result = $coordinator->validate($validCode);
echo "Result: " . ($result->isValid() ? '✅ VALID' : '❌ INVALID') . "\n";
echo "Errors: " . $result->getErrorCount() . "\n";
echo "Warnings: " . $result->getWarningCount() . "\n";
if ($result->hasWarnings()) {
echo "\nWarnings:\n";
foreach ($result->getWarnings() as $warning) {
echo " - {$warning}\n";
}
}
echo "\n\n";
// Example 2: Code with syntax error
echo "Example 2: Validating code with SYNTAX ERROR\n";
echo str_repeat('-', 50) . "\n";
$invalidCode = <<<'PHP'
<?php
namespace App;
class Broken
{
public function test()
{
// Missing semicolon and closing brace
return "broken"
}
PHP;
$result = $coordinator->validate($invalidCode);
echo "Result: " . ($result->isValid() ? '✅ VALID' : '❌ INVALID') . "\n";
echo "Errors: " . $result->getErrorCount() . "\n";
if ($result->hasErrors()) {
echo "\nErrors:\n";
foreach ($result->getErrors() as $error) {
echo " - {$error}\n";
}
}
echo "\n\n";
// Example 3: Individual validator usage
echo "Example 3: Using validators individually\n";
echo str_repeat('-', 50) . "\n";
$syntaxValidator = new PHPSyntaxValidator();
$testCode = <<<'PHP'
<?php
function greet(string $name): string
{
return "Hello, {$name}!";
}
PHP;
echo "Testing with PHPSyntaxValidator...\n";
$result = $syntaxValidator->validate($testCode);
echo "Result: " . $result->getSummary() . "\n";
echo "\n\n";
// Example 4: Validation with context
echo "Example 4: Validation with context\n";
echo str_repeat('-', 50) . "\n";
$context = [
'purpose' => 'API endpoint handler',
'framework' => 'Laravel',
'requirements' => ['validation', 'authorization', 'error handling'],
];
$apiHandlerCode = <<<'PHP'
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
public function store(Request $request)
{
$user = User::create($request->all());
return response()->json($user);
}
}
PHP;
echo "Validating API handler with context...\n";
$result = $coordinator->validate($apiHandlerCode, $context);
echo "Result: " . $result->getSummary() . "\n";
echo "Metadata: " . json_encode($result->getMetadata(), JSON_PRETTY_PRINT) . "\n";
echo "\n\n";
// Example 5: Custom script validator
echo "Example 5: Custom script validator\n";
echo str_repeat('-', 50) . "\n";
// Create a custom validator that runs a PHP script
$customValidator = CustomScriptValidator::phpScript(
__DIR__ . '/../tests/fixtures/custom-validator.php'
);
// You can also create validators for specific test frameworks
// $phpunitValidator = CustomScriptValidator::phpunit('tests/Unit/MyTest.php');
// $pestValidator = CustomScriptValidator::pest('tests/Feature/MyTest.php');
echo "Custom validator: {$customValidator->getName()}\n";
echo "Priority: {$customValidator->getPriority()}\n";
echo "\n=== Example Complete ===\n";