Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize generated code for Hydrators #2665

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<EOF

/** @AlsoLoad("$name") */
if (!array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) {
// AlsoLoad("$name")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference to doctrine annotations is outdated.

if (! array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) {
\$data['%1\$s'] = \$data['$name'];
}

Expand All @@ -183,7 +183,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<'EOF'

/** @Field(type="date") */
// Field(type: "date")
if (isset($data['%1$s'])) {
$value = $data['%1$s'];
%3$s
Expand All @@ -201,7 +201,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<EOF

/** @Field(type="{$mapping['type']}") */
// Field(type: "{$mapping['type']}")
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) {
\$value = \$data['%1\$s'];
if (\$value !== null) {
Expand All @@ -224,7 +224,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<'EOF'

/** @ReferenceOne */
// ReferenceOne
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
$return = $data['%1$s'];
if ($return !== null) {
Expand Down Expand Up @@ -275,11 +275,11 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']];
$mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'], $mapping['mappedBy']);
$criteria = array_merge(
array($mappedByFieldName => $data['_id']),
isset($this->class->fieldMappings['%2$s']['criteria']) ? $this->class->fieldMappings['%2$s']['criteria'] : array()
[$mappedByFieldName => $data['_id']],
$this->class->fieldMappings['%2$s']['criteria'] ?? []
);
$sort = isset($this->class->fieldMappings['%2$s']['sort']) ? $this->class->fieldMappings['%2$s']['sort'] : array();
$return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, array(), 0, $sort);
$sort = $this->class->fieldMappings['%2$s']['sort'] ?? [];
$return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, [], 0, $sort);
$this->class->reflFields['%2$s']->setValue($document, $return);
$hydratedData['%2$s'] = $return;

Expand All @@ -293,8 +293,8 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<'EOF'

/** @Many */
$mongoData = isset($data['%1$s']) ? $data['%1$s'] : null;
// ReferenceMany & EmbedMany
$mongoData = $data['%1$s'] ?? null;

if ($mongoData !== null && ! is_array($mongoData)) {
throw HydratorException::associationTypeMismatch('%3$s', '%1$s', 'array', gettype($mongoData));
Expand All @@ -320,7 +320,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
$code .= sprintf(
<<<'EOF'

/** @EmbedOne */
// EmbedOne
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
$return = $data['%1$s'];
if ($return !== null) {
Expand Down Expand Up @@ -370,23 +370,20 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
use Doctrine\ODM\MongoDB\Query\Query;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

use function array_key_exists;
use function gettype;
use function is_array;
Comment on lines +373 to +375
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functions are compiler-optimized.


/**
* THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE.
*/
class $hydratorClassName implements HydratorInterface
{
private \$dm;
private \$class;

public function __construct(DocumentManager \$dm, ClassMetadata \$class)
{
\$this->dm = \$dm;
\$this->class = \$class;
}
public function __construct(private DocumentManager \$dm, private ClassMetadata \$class) {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the property type.


public function hydrate(object \$document, array \$data, array \$hints = array()): array
public function hydrate(object \$document, array \$data, array \$hints = []): array
{
\$hydratedData = array();
\$hydratedData = [];
Comment on lines +384 to +386
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short-array syntax.

%s return \$hydratedData;
}
}
Expand Down
Loading