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

Refactor ActiveQuery::removeDuplicatedModels() to separate ArrayAccess implementation #345

Merged
merged 4 commits into from
May 25, 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
75 changes: 40 additions & 35 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
use Yiisoft\Definitions\Exception\NotInstantiableException;
use Yiisoft\Factory\NotFoundException;

use function array_column;
use function array_combine;
use function array_flip;
use function array_intersect_key;
use function array_key_first;
use function array_map;
use function array_merge;
use function array_values;
use function count;
Expand Down Expand Up @@ -289,53 +295,52 @@
*/
private function removeDuplicatedModels(array $models): array
{
$hash = [];
$model = reset($models);

$pks = $this->getARInstance()->primaryKey();
if ($this->asArray) {
$pks = $this->getARInstance()->primaryKey();

if (count($pks) > 1) {
// Composite primary key.
foreach ($models as $i => $model) {
$key = [];
foreach ($pks as $pk) {
if (!isset($model[$pk])) {
// Don't continue if the primary key isn't part of the result set.
break 2;
}
$key[] = $model[$pk];
}

$key = serialize($key);

if (isset($hash[$key])) {
unset($models[$i]);
} else {
$hash[$key] = true;
}
if (empty($pks)) {
throw new InvalidConfigException("Primary key of '$this->arClass' can not be empty.");

Check warning on line 304 in src/ActiveQuery.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveQuery.php#L304

Added line #L304 was not covered by tests
}
} elseif (empty($pks)) {
throw new InvalidConfigException("Primary key of '$this->arClass' can not be empty.");
} else {
// Single column primary key.
$pk = reset($pks);

foreach ($models as $i => $model) {
foreach ($pks as $pk) {
if (!isset($model[$pk])) {
// Don't continue if the primary key isn't part of the result set.
break;
return $models;
}
}

$key = $model[$pk];
if (count($pks) === 1) {
$hash = array_column($models, reset($pks));

Check warning on line 314 in src/ActiveQuery.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveQuery.php#L313-L314

Added lines #L313 - L314 were not covered by tests
} else {
$flippedPks = array_flip($pks);
$hash = array_map(
static fn ($model): string => serialize(array_intersect_key($model, $flippedPks)),
$models
);

Check warning on line 320 in src/ActiveQuery.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveQuery.php#L316-L320

Added lines #L316 - L320 were not covered by tests
}
} else {
$pks = $model->getPrimaryKey(true);

if (isset($hash[$key])) {
unset($models[$i]);
} else {
$hash[$key] = true;
if (empty($pks)) {
throw new InvalidConfigException("Primary key of '$this->arClass' can not be empty.");

Check warning on line 326 in src/ActiveQuery.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveQuery.php#L326

Added line #L326 was not covered by tests
}

foreach ($pks as $pk) {
if ($pk === null) {
return $models;
}
}

if (count($pks) === 1) {
$key = array_key_first($pks);
$hash = array_map(static fn ($model): string => (string) $model->getAttribute($key), $models);
} else {
$hash = array_map(static fn ($model): string => serialize($model->getPrimaryKey(true)), $models);
}
}

return array_values($models);
return array_values(array_combine($hash, $models));
}

/**
Expand Down
Loading