Skip to content

Commit e6e3018

Browse files
committed
Take care of reserved keywoards and empty webhook event classes
1 parent 6fce10f commit e6e3018

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Generator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function generate(string $namespace, string $destinationPath)
3737

3838
private function className(string $className): string
3939
{
40-
return str_replace(['{', '}', '-', '$', '_'], ['Cb', 'Rcb', 'Dash', '_', '\\'], (new Convert($className))->toPascal());
40+
return str_replace(['{', '}', '-', '$', '_'], ['Cb', 'Rcb', 'Dash', '_', '\\'], (new Convert($className))->toPascal()) . ($this->isKeyword($className) ? '_' : '');
4141
}
4242

4343
private function cleanUpNamespace(string $namespace): string
@@ -164,4 +164,9 @@ private function basename(string $fqcn): string
164164

165165
return $this->cleanUpNamespace(basename($fqcn));
166166
}
167+
168+
private function isKeyword(string $name): bool
169+
{
170+
return in_array($name, array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor'), false);
171+
}
167172
}

src/Generator/WebHook.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ public static function generate(string $path, string $namespace, string $baseNam
3838
);
3939
if ($pathItem->post->requestBody->content !== null) {
4040
$content = current($pathItem->post->requestBody->content);
41+
$tmts = [];
4142
if ($content->schema->oneOf !== null && count($content->schema->oneOf) > 0) {
42-
$method->addStmt(new Node\Expr\Assign(new Node\Expr\Variable('schemaValidator'), new Node\Expr\New_(
43+
$tmts[] = new Node\Expr\Assign(new Node\Expr\Variable('schemaValidator'), new Node\Expr\New_(
4344
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
4445
[
4546
new Node\Arg(new Node\Expr\ClassConstFetch(
4647
new Node\Name('\League\OpenAPIValidation\Schema\SchemaValidator'),
4748
new Node\Name('VALIDATE_AS_REQUEST'),
4849
))
4950
]
50-
)));
51+
));
5152
$gotoLabels = 'a';
5253
foreach ($content->schema->oneOf as $oneOfSchema) {
53-
$method->addStmt(new Node\Stmt\Label($gotoLabels));
54+
$tmts[] = new Node\Stmt\Label($gotoLabels);
5455
$gotoLabels++;
55-
$method->addStmt(new Node\Stmt\TryCatch([
56+
$tmts[] = new Node\Stmt\TryCatch([
5657
new Node\Stmt\Expression(new Node\Expr\MethodCall(
5758
new Node\Expr\Variable('schemaValidator'),
5859
new Node\Name('validate'),
@@ -70,11 +71,17 @@ public static function generate(string $path, string $namespace, string $baseNam
7071
new Node\Stmt\Goto_($gotoLabels),
7172
]
7273
),
73-
]));
74+
]);
7475
}
75-
$method->addStmt(new Node\Stmt\Label($gotoLabels));
76-
$method->addStmt(new Node\Stmt\Throw_(new Node\Expr\Variable($gotoLabels)));
76+
$tmts[] = new Node\Stmt\Label($gotoLabels);
77+
$tmts[] = new Node\Stmt\Throw_(new Node\Expr\Variable($gotoLabels));
7778
}
79+
80+
if (count($tmts) === 0) {
81+
$tmts[] = new Node\Stmt\Return_(new Node\Scalar\String_('TODO: Implement this'));
82+
}
83+
84+
$method->addStmts($tmts);
7885
}
7986
$class->addStmt($method);
8087

0 commit comments

Comments
 (0)