Skip to content

Commit

Permalink
fix(helpers): Correctly handle unsupported Exprs
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Jan 15, 2024
1 parent f3b8f7f commit e2cfc4b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/ControllerMethodParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public function __construct(string $context, array $definitions, public string $
$this->type = OpenApiType::resolve($context, $definitions, $methodParameter->type);
}
if ($methodParameter->default != null) {
$this->type->hasDefaultValue = true;
$this->type->defaultValue = Helpers::exprToValue($context, $methodParameter->default);
try {
$this->type->defaultValue = Helpers::exprToValue($context, $methodParameter->default);
$this->type->hasDefaultValue = true;
} catch (UnsupportedExprException $e) {
Logger::debug($context, $e);
}
}
}
}
24 changes: 14 additions & 10 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public static function cleanSchemaName(string $name): string {
return substr($name, strlen($readableAppID));
}

/**
* @throws LoggerException
* @throws UnsupportedExprException
*/
public static function exprToValue(string $context, Expr $expr): mixed {
if ($expr instanceof ConstFetch) {
$value = $expr->name->getLast();
Expand All @@ -185,20 +189,20 @@ public static function exprToValue(string $context, Expr $expr): mixed {
if ($expr instanceof Array_) {
$array = [];
foreach ($expr->items as $item) {
$value = self::exprToValue($context, $item->value);
if ($item->key !== null) {
$array[self::exprToValue($context, $item->key)] = $value;
} else {
$array[] = $value;
try {
$value = self::exprToValue($context, $item->value);
if ($item->key !== null) {
$array[self::exprToValue($context, $item->key)] = $value;
} else {
$array[] = $value;
}
} catch (UnsupportedExprException $e) {
Logger::debug($context, $e);
}
}
return $array;
}
if ($expr instanceof Expr\ClassConstFetch || $expr instanceof Expr\BinaryOp) {
// Not supported
return null;
}

Logger::panic($context, "Unable to evaluate expression '" . get_class($expr) . "'");
throw new UnsupportedExprException($expr, $context);
}
}
15 changes: 15 additions & 0 deletions src/UnsupportedExprException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OpenAPIExtractor;

use Exception;
use PhpParser\Node\Expr;

class UnsupportedExprException extends Exception {
public function __construct(
public Expr $expr,
public string $context,
) {
parent::__construct($this->context . ": Unable to parse Expr: " . get_class($this->expr));
}
}

0 comments on commit e2cfc4b

Please sign in to comment.