Skip to content

Add findKey() method to return index of first element matching a callable#153

Merged
voku merged 3 commits intomasterfrom
copilot/add-index-of-min-max
Apr 10, 2026
Merged

Add findKey() method to return index of first element matching a callable#153
voku merged 3 commits intomasterfrom
copilot/add-index-of-min-max

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

No method existed to find the index/key of an array element by predicate — only find() (returns value) and searchIndex() (requires exact value match).

Changes

  • src/Arrayy.php: Added findKey(\Closure $closure) — iterates via generator, returns the key of the first element where $closure($value, $key) is truthy, or false if none match.
  • tests/ArrayyTest.php: Added findKeyProvider() data provider + testFindKey() (value-based matching across int/string/bool/float/assoc cases) + testFindKeyWithKeyParameter() (verifies $key is correctly forwarded to the closure).
  • README.md: Regenerated via build/generate_docs.php to include findKey in the method table and docs section.
  • AGENTS.md: Created a knowledge base documenting project architecture, conventions, how to run tests, how to regenerate docs, and findings from this task. Added to .gitattributes as export-ignore so it is not distributed via Composer.

Example

$a = a([3, 1, 4, 1, 5, 9]);

// Index of min value
$minIdx = $a->findKey(fn($v) => $v === min($a->getArray())); // 1

// Index of max value
$maxIdx = $a->findKey(fn($v) => $v === max($a->getArray())); // 5

// Find by key
$a = a(['a' => 10, 'b' => 20, 'c' => 30]);
$a->findKey(fn($v, $k) => $k === 'b'); // 'b'

This change is


This change is Reviewable

Copilot AI assigned Copilot and voku Apr 9, 2026
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 10, 2026

Coverage Status

coverage: 89.36% (+0.02%) from 89.345% — copilot/add-index-of-min-max into master

@voku voku marked this pull request as ready for review April 10, 2026 00:17
@voku
Copy link
Copy Markdown
Owner

voku commented Apr 10, 2026

@copilot run build/generate_docs.php and create a AGENTS.md file afterwards where you put your knowlage about this project and your findings from this task + put the new markdown file into .gitattributes so that we do not distribute it via composer

… update .gitattributes

Agent-Logs-Url: https://github.com/voku/Arrayy/sessions/f018d0bd-6d0f-42fb-bc9f-526de3787dd6

Co-authored-by: voku <264695+voku@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 10, 2026

@copilot run build/generate_docs.php and create a AGENTS.md file afterwards where you put your knowlage about this project and your findings from this task + put the new markdown file into .gitattribu...

Done in commit f13314f:

  • Ran build/generate_docs.php (after installing build deps) — README now includes the findKey entry in the method table and full docs section
  • Created AGENTS.md with project architecture, conventions, how to run tests, how to regenerate docs, and findings from this task
  • Added /AGENTS.md export-ignore to .gitattributes so it won't be distributed via Composer

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/doctrine/deprecations/zipball/d4fe3e6fd9bb9e72557a19674f44d8ac7db4c6ca
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/nikic/PHP-Parser/zipball/51bd93cc741b7fc3d63d20b6bdcd99fdaa359837
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/php-fig/simple-cache/zipball/8707bf3cea6f710bf6ef05491234e3ab06f6432a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/622548b623e81ca6d78b721c5e029f4ce664f170
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/7bae67520aa9f5ecc506d646810bd40d9da54582
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/327a05bbee54120d4786a0dc67aad30226ad4cf9
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/48f445a408c131e38cab1c235aa6d2bb7a0bb20d
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpstan/phpdoc-parser/zipball/82a311fd3690fb2bf7b64d5c98f912b3dd746140
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpstan/phpdoc-parser/zipball/a004701b11273a26cd7955a61d67a7f1e525a45a
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/voku/Php-Readme-Helper/zipball/f1634ca664b3766c8a91856b75c0af0392e926d0
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/voku/Simple-PHP-Code-Parser/zipball/24f71e31d357ed4b80e514658b919a8f0c523af2
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/voku/simple-cache/zipball/d6578b2b1c7a1ed36c4a862cae826b703520e73b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/webmozarts/assert/zipball/1b99650e7ffcad232624a260bc7fbdec2ffc407c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/12qSaa /usr/bin/composer install --no-interaction --no-dev pull.rebase (http block)
  • https://api.github.com/repos/webmozarts/assert/zipball/9be6926d8b485f55b9229203f962b51ed377ba68
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/1vm8IG /usr/bin/composer install --no-interaction (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@sonarqubecloud
Copy link
Copy Markdown

Copilot AI requested a review from voku April 10, 2026 00:25
Copilot finished work on behalf of voku April 10, 2026 00:25
@voku voku merged commit 38ab634 into master Apr 10, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants