Skip to content

Commit

Permalink
Assert types is a list of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 2, 2023
1 parent 55f8cce commit ace7f60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion generator/src/Definition/ArgumentDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace MongoDB\CodeGenerator\Definition;

use function array_is_list;
use function assert;
use function get_debug_type;
use function is_array;
use function is_string;
use function sprintf;

final readonly class ArgumentDefinition
{
Expand All @@ -20,8 +23,9 @@ public function __construct(
?int $variadicMin = null,
) {
if (is_array($type)) {
assert(array_is_list($type), 'Type must be a list or a single string');
foreach ($type as $t) {
assert(is_string($t), json_encode($type));
assert(is_string($t), sprintf('Type must be a list of strings. Got %s', get_debug_type($type)));
}
}

Expand Down
15 changes: 12 additions & 3 deletions generator/src/Definition/GeneratorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use MongoDB\CodeGenerator\OperatorGenerator;

use function array_is_list;
use function assert;
use function class_exists;
use function is_string;
use function is_subclass_of;
use function sprintf;
Expand All @@ -23,9 +25,16 @@ public function __construct(
public array $interfaces = [],
public ?string $parentClass = null,
) {
assert(str_starts_with($this->namespace, 'MongoDB\\'), sprintf('Namespace must start with "MongoDB\\". Got "%s"', $this->namespace));
assert(! str_ends_with($this->namespace, '\\'), sprintf('Namespace must not end with "\\". Got "%s"', $this->namespace));
foreach ($this->generators as $class) {
assert(str_starts_with($namespace, 'MongoDB\\'), sprintf('Namespace must start with "MongoDB\\". Got "%s"', $namespace));
assert(! str_ends_with($namespace, '\\'), sprintf('Namespace must not end with "\\". Got "%s"', $namespace));

assert(array_is_list($interfaces), 'Generators must be a list of class names');
foreach ($interfaces as $interface) {
assert(is_string($interface) && class_exists($interface), sprintf('Interface "%s" does not exist', $interface));
}

assert(array_is_list($generators), 'Generators must be a list of class names');
foreach ($generators as $class) {
assert(is_string($class) && is_subclass_of($class, OperatorGenerator::class), sprintf('Generator class "%s" must extend "%s"', $class, OperatorGenerator::class));
}
}
Expand Down

0 comments on commit ace7f60

Please sign in to comment.