[Feature Request] Create generator for matrix values.

Open Beginner friendly
#497 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
backend

Research direction

Start by reading MatrixInterface and the Matrix implementation, then check how getBlockCount() and getBlockValue() are used and tested. Add a generator that yields row and column indexes for blocks whose value is 1; done means callers can iterate with foreach as shown in the issue and the behavior is covered by a test.

Written by the indexing model from the issue text.

Description

When using MatrixInterface, you often write code like this:

$matrix = ...
$blockCount = $matrix->getBlockCount();
for ($rowIndex = 0; $rowIndex < $blockCount; ++$rowIndex) {
    for ($columnIndex = 0; $columnIndex < $blockCount; ++$columnIndex) {
        if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
            // ...
        }
    }
}

It would be nice to create a new function (a generator) that returns the row and column indices only for valid values:

interface MatrixInterface {

    /**
     * @return \Generator<array{0: int, 1: int}>
     */ 
    public function getIndexes(): \Generator;

}

And the implementation:

final readonly class Matrix implements MatrixInterface {

    #[\Override]
    public function getIndexes(): \Generator {
        $blockCount = $this->getBlockCount();
        for ($rowIndex = 0; $rowIndex < $blockCount; ++$rowIndex) {
            for ($columnIndex = 0; $columnIndex < $blockCount; ++$columnIndex) {
                if (1 === $this->getBlockValue($rowIndex, $columnIndex)) {
                   yield [$rowIndex, $columnIndex];
                }
            }
        }
    }
}

Then use as follow:

$matrix = ...
foreach ($matrix->getIndexes() as [$rowIndex, $columnIndex]) {
    // ...
}
Dominant language
PHP
Stars
4.8k
Forks
766
Avg merge
10h 51m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from endroid/qr-code

All issues in endroid/qr-code

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.