Skip to content

Commit

Permalink
refactor: add $context to PropertyDescriberInterface::supports() (#2393)
Browse files Browse the repository at this point in the history
## Description


#2388 (comment)

## What type of PR is this? (check all applicable)
- [ ] Bug Fix
- [ ] Feature
- [x] Refactor
- [ ] Deprecation
- [ ] Breaking Change
- [ ] Documentation Update
- [ ] CI

## Checklist
- [ ] I have made corresponding changes to the documentation (`docs/`)
- [ ] I have made corresponding changes to the changelog
(`CHANGELOG.md`)
  • Loading branch information
DjordyKoert authored Nov 12, 2024
1 parent c431718 commit 63345ae
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 4.33.5
* Added new optional parameter `$context` to` PropertyDescriberInterface::supports()`

## 4.33.4
* Deprecated `null` type from `$options` in `Nelmio\ApiDocBundle\Attribute\Model::__construct()`. Pass an empty array (`[]`) instead.
* Deprecated `null` type from `$options` in `NNelmio\ApiDocBundle\Attribute\Model::__construct()`. Pass an empty array (`[]`) instead.
Expand Down
72 changes: 71 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,89 @@ parameters:
count: 1
path: src/Describer/ExternalDocDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\ArrayPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/ArrayPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/PropertyDescriber/ArrayPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\BooleanPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/BooleanPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\CompoundPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/CompoundPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/PropertyDescriber/CompoundPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\DateTimePropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/DateTimePropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\DictionaryPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/DictionaryPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/PropertyDescriber/DictionaryPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\FloatPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/FloatPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\IntegerPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/IntegerPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\NullablePropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/NullablePropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/PropertyDescriber/NullablePropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\ObjectPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/ObjectPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/PropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
path: src/PropertyDescriber/PropertyDescriber.php

-
message: "#^PHPDoc tag @param references unknown parameter\\: \\$context$#"
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:supports\\(\\) invoked with 2 parameters, 1 required\\.$#"
count: 1
path: src/PropertyDescriber/PropertyDescriber.php

-
message: "#^PHPDoc tag @param references unknown parameter\\: \\$context$#"
count: 2
path: src/PropertyDescriber/PropertyDescriberInterface.php

-
Expand All @@ -65,6 +120,21 @@ parameters:
count: 1
path: src/PropertyDescriber/RequiredPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\RequiredPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/RequiredPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\StringPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/StringPropertyDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\UuidPropertyDescriber\\:\\:supports\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/PropertyDescriber/UuidPropertyDescriber.php

-
message: "#^Call to method render\\(\\) on an unknown class Twig_Environment\\.$#"
count: 2
Expand Down
2 changes: 1 addition & 1 deletion src/ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private function describeProperty(array $types, Model $model, OA\Schema $propert
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber->supports($types)) {
if ($propertyDescriber->supports($types, $model->getSerializationContext())) {
$propertyDescriber->describe($types, $property, $model->getGroups(), $schema, $model->getSerializationContext());

return;
Expand Down
4 changes: 2 additions & 2 deletions src/PropertyDescriber/ArrayPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
foreach ($types[0]->getCollectionValueTypes() as $type) {
// Handle list pseudo type
// https://symfony.com/doc/current/components/property_info.html#type-getcollectionkeytypes-type-getcollectionvaluetypes
if ($this->supports([$type]) && [] === $type->getCollectionValueTypes()) {
if ($this->supports([$type], $context) && [] === $type->getCollectionValueTypes()) {
continue;
}

$this->propertyDescriber->describe([$type], $property, $groups, $schema, $context);
}
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
if (1 !== count($types) || !$types[0]->isCollection()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/BooleanPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->type = 'boolean';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_BOOL === $types[0]->getBuiltinType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/CompoundPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
}
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return count($types) >= 2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/DateTimePropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->format = 'date-time';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType()
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/DictionaryPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$this->propertyDescriber->describe($types[0]->getCollectionValueTypes(), $additionalProperties, $groups, $schema, $context);
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types)
&& $types[0]->isCollection()
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/FloatPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->format = 'float';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_FLOAT === $types[0]->getBuiltinType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/IntegerPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->type = 'integer';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_INT === $types[0]->getBuiltinType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/NullablePropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$this->propertyDescriber->describe($types, $property, $groups, $schema, $context);
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
foreach ($types as $type) {
if ($type->isNullable()) {
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/ObjectPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->ref = $this->modelRegistry->register(new Model($type, $groups, [], $context));
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType();
Expand Down
13 changes: 7 additions & 6 deletions src/PropertyDescriber/PropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
);
}

if (null === $propertyDescriber = $this->getPropertyDescriber($types)) {
if (null === $propertyDescriber = $this->getPropertyDescriber($types, $context)) {
return;
}

Expand All @@ -69,9 +69,9 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$this->called = []; // Reset recursion helper
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return null !== $this->getPropertyDescriber($types);
return null !== $this->getPropertyDescriber($types, $context);
}

/**
Expand All @@ -83,9 +83,10 @@ private function getHash(array $types): string
}

/**
* @param Type[] $types
* @param Type[] $types
* @param array<string, mixed> $context
*/
private function getPropertyDescriber(array $types): ?PropertyDescriberInterface
private function getPropertyDescriber(array $types, array $context): ?PropertyDescriberInterface
{
foreach ($this->propertyDescribers as $propertyDescriber) {
/* BC layer for Symfony < 6.3 @see https://symfony.com/doc/6.3/service_container/tags.html#reference-tagged-services */
Expand All @@ -108,7 +109,7 @@ private function getPropertyDescriber(array $types): ?PropertyDescriberInterface
$propertyDescriber->setPropertyDescriber($this);
}

if ($propertyDescriber->supports($types)) {
if ($propertyDescriber->supports($types, $context)) {
return $propertyDescriber;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/PropertyDescriber/PropertyDescriberInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ interface PropertyDescriberInterface
public function describe(array $types, Schema $property, ?array $groups = null /* , ?Schema $schema = null */ /* , array $context = [] */);

/**
* @param Type[] $types
* @param Type[] $types
* @param array<string, mixed> $context Context options for describing the property
*/
public function supports(array $types): bool;
public function supports(array $types /* , array $context = [] */): bool;
}
2 changes: 1 addition & 1 deletion src/PropertyDescriber/RequiredPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$schema->required = array_values(array_unique($existingRequiredFields));
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/StringPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->type = 'string';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_STRING === $types[0]->getBuiltinType();
}
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyDescriber/UuidPropertyDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function describe(array $types, OA\Schema $property, ?array $groups = nul
$property->format = 'uuid';
}

public function supports(array $types): bool
public function supports(array $types, array $context = []): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType()
Expand Down

0 comments on commit 63345ae

Please sign in to comment.