Skip to content

PHPLIB-1161: Leverage array_key_first in functions #1101

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

Merged
merged 1 commit into from
Jun 12, 2023
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
14 changes: 3 additions & 11 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@
use function is_array;
use function is_object;
use function is_string;
use function key;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toPHP;
use function reset;
use function substr;

/**
Expand Down Expand Up @@ -319,11 +317,9 @@ function is_last_pipeline_operator_write(array $pipeline): bool
return false;
}

$lastOp = document_to_array($lastOp);
$key = array_key_first(document_to_array($lastOp));
Copy link
Member Author

Choose a reason for hiding this comment

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

I had a doubt whether it's array_key_first or array_key_last. But reset moves the cursor to the first element.

A new test case may be necessary as it doesn't fail when I change to get the first or the last key.

Copy link
Member Author

@GromNaN GromNaN Jun 12, 2023

Choose a reason for hiding this comment

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

I think the $lastOp array must contain exactly 1 item. That's why it doesn't matter. We could even have used key() directly without reset().

Copy link
Member

Choose a reason for hiding this comment

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

I think the $lastOp array must contain exactly 1 item.

That's correct - we're assuming this is an aggregation pipeline stage which only contains one key.


reset($lastOp);

return key($lastOp) === '$merge' || key($lastOp) === '$out';
return $key === '$merge' || $key === '$out';
}

/**
Expand All @@ -341,11 +337,7 @@ function is_mapreduce_output_inline($out): bool
return false;
}

$out = document_to_array($out);

reset($out);

return key($out) === 'inline';
return array_key_first(document_to_array($out)) === 'inline';
}

/**
Expand Down