Skip to content

Commit

Permalink
refactor: deprecate option possibly being null
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Nov 8, 2024
1 parent 0dcdc7d commit ccf991e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
11 changes: 8 additions & 3 deletions src/Attribute/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Model extends Attachable
public ?array $groups;

/**
* @var mixed[]|null
* @var mixed[]
*/
public ?array $options;
public array $options;

/**
* @var array<string, mixed>
Expand All @@ -60,9 +60,14 @@ public function __construct(
array $properties = [],
string $type = Generator::UNDEFINED,
?array $groups = null,
?array $options = null,
?array $options = [],
array $serializationContext = []
) {
if (null === $options) {
trigger_deprecation('nelmio/api-doc-bundle', '4.33.4', 'Passing null to the "$options" argument of "%s()" is deprecated, pass an empty array instead.', __METHOD__);
$options = [];
}

parent::__construct($properties + [
'type' => $type,
'groups' => $groups,
Expand Down
15 changes: 10 additions & 5 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ final class Model
private Type $type;

/**
* @var mixed[]|null
* @var mixed[]
*/
private ?array $options;
private array $options;

/**
* @var mixed[]
Expand All @@ -32,8 +32,13 @@ final class Model
* @param mixed[]|null $options
* @param mixed[] $serializationContext
*/
public function __construct(Type $type, ?array $groups = null, ?array $options = null, array $serializationContext = [])
public function __construct(Type $type, ?array $groups = null, ?array $options = [], array $serializationContext = [])
{
if (null === $options) {
trigger_deprecation('nelmio/api-doc-bundle', '4.33.4', 'Passing null to the "$options" argument of "%s()" is deprecated, pass an empty array instead.', __METHOD__);
$options = [];
}

$this->type = $type;
$this->options = $options;
$this->serializationContext = $serializationContext;
Expand Down Expand Up @@ -69,9 +74,9 @@ public function getHash(): string
}

/**
* @return mixed[]|null
* @return mixed[]
*/
public function getOptions(): ?array
public function getOptions(): array
{
return $this->options;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct($modelDescribers, OA\OpenApi $api, array $alternativ
$this->alternativeNames[] = $model = new Model(
new Type('object', false, $criteria['type']),
$criteria['groups'],
$criteria['options'] ?? null,
$criteria['options'] ?? [],
$criteria['serializationContext'] ?? [],
);
$this->names[$model->getHash()] = $alternativeName;
Expand Down
2 changes: 1 addition & 1 deletion src/ModelDescriber/FormModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function describe(Model $model, OA\Schema $schema): void

$this->setContextFromReflection($schema->_context, new \ReflectionClass($class));

$form = $this->formFactory->create($class, null, $model->getOptions() ?? []);
$form = $this->formFactory->create($class, null, $model->getOptions());
$this->parseForm($schema, $form);

$this->setContext(null);
Expand Down
2 changes: 1 addition & 1 deletion src/ModelDescriber/JMSModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function describeItem(array $type, OA\Schema $property, Context $context,
$groups = $this->computeGroups($context, $type);
unset($serializationContext['groups']);

$model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type['name']), $groups, null, $serializationContext);
$model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type['name']), $groups, [], $serializationContext);
$modelRef = $this->modelRegistry->register($model);

$customFields = (array) $property->jsonSerialize();
Expand Down
4 changes: 2 additions & 2 deletions src/PropertyDescriber/ObjectPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul

if ($types[0]->isNullable()) {
$weakContext = Util::createWeakContext($property->_context);
$schemas = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups, null, $context)), '_context' => $weakContext])];
$schemas = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups, [], $context)), '_context' => $weakContext])];
$property->oneOf = $schemas;

return;
}

$property->ref = $this->modelRegistry->register(new Model($type, $groups, null, $context));
$property->ref = $this->modelRegistry->register(new Model($type, $groups, [], $context));
}

public function supports(array $types): bool
Expand Down
8 changes: 4 additions & 4 deletions tests/Model/ModelRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public function testNameCollisionsAreLogged(Type $type, array $arrayType): void
'Can not assign a name for the model, the name "ModelRegistryTest" has already been taken.', [
'model' => [
'type' => $arrayType,
'options' => null,
'options' => [],
'groups' => ['group2'],
'serialization_context' => [
'groups' => ['group2'],
],
],
'taken_by' => [
'type' => $arrayType,
'options' => null,
'options' => [],
'groups' => ['group1'],
'serialization_context' => [
'groups' => ['group1'],
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testNameCollisionsAreLoggedWithAlternativeNames(): void
'collection_key_types' => null,
'collection_value_types' => null,
],
'options' => null,
'options' => [],
'groups' => ['group2'],
'serialization_context' => ['groups' => ['group2']],
],
Expand All @@ -147,7 +147,7 @@ public function testNameCollisionsAreLoggedWithAlternativeNames(): void
'collection_key_types' => null,
'collection_value_types' => null,
],
'options' => null,
'options' => [],
'groups' => ['group1'],
'serialization_context' => ['groups' => ['group1']],
],
Expand Down

0 comments on commit ccf991e

Please sign in to comment.