eddiriarte /
console-select
| 1 | <?php |
||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 2 | namespace EddIriarte\Console\Inputs\Traits; |
||||
|
0 ignored issues
–
show
|
|||||
| 3 | |||||
| 4 | use EddIriarte\Console\Inputs\Exceptions\IndexOutOfRange; |
||||
| 5 | |||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * Trait ChunkableOptions |
||||
| 9 | * @package EddIriarte\Console\Inputs\Traits |
||||
|
0 ignored issues
–
show
|
|||||
| 10 | * @author Eduardo Iriarte <eddiriarte[at]gmail[dot]com> |
||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
| 11 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 12 | trait ChunkableOptions |
||||
| 13 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 14 | protected $chunks; |
||||
|
0 ignored issues
–
show
|
|||||
| 15 | |||||
| 16 | protected $chunkSize = 3; |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | |||||
| 18 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 19 | * @return array |
||||
| 20 | */ |
||||
| 21 | 19 | public function getChunks(int $chunkSize = null): array |
|||
|
0 ignored issues
–
show
|
|||||
| 22 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 23 | 19 | if (!is_null($chunkSize)) { |
|||
|
0 ignored issues
–
show
|
|||||
| 24 | 9 | $this->chunkSize = $chunkSize; |
|||
| 25 | } |
||||
| 26 | |||||
| 27 | 19 | if (!isset($this->chunks)) { |
|||
|
0 ignored issues
–
show
|
|||||
| 28 | 15 | $this->chunks = array_chunk($this->getOptions(), $this->chunkSize); |
|||
|
0 ignored issues
–
show
It seems like
getOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 29 | } |
||||
| 30 | |||||
| 31 | 19 | return $this->chunks; |
|||
| 32 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 33 | |||||
| 34 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 35 | * @param int $index |
||||
|
0 ignored issues
–
show
|
|||||
| 36 | * @return array |
||||
| 37 | * @throws EddIriarte\Console\Inputs\Exceptions\IndexOutOfRange |
||||
|
0 ignored issues
–
show
|
|||||
| 38 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 39 | 2 | public function getChunkAt(int $index): array |
|||
| 40 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 41 | 2 | if (!empty($this->getChunks()[$index])) { |
|||
|
0 ignored issues
–
show
|
|||||
| 42 | 1 | return $this->getChunks()[$index]; |
|||
| 43 | } |
||||
| 44 | |||||
| 45 | 1 | throw new IndexOutOfRange($index); |
|||
| 46 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 47 | |||||
| 48 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 49 | * @return int |
||||
|
0 ignored issues
–
show
|
|||||
| 50 | */ |
||||
| 51 | 8 | public function getChunksCount(): int |
|||
| 52 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | 8 | return count($this->getChunks()); |
|||
| 54 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 55 | |||||
| 56 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 57 | * @param int $rowIndex |
||||
|
0 ignored issues
–
show
|
|||||
| 58 | * @param int $colIndex |
||||
|
0 ignored issues
–
show
|
|||||
| 59 | * @return bool |
||||
|
0 ignored issues
–
show
|
|||||
| 60 | */ |
||||
| 61 | 12 | public function hasEntryAt(int $rowIndex, int $colIndex): bool |
|||
| 62 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 63 | 12 | $chunks = $this->getChunks(); |
|||
| 64 | 12 | return array_key_exists($rowIndex, $chunks) && array_key_exists($colIndex, $chunks[$rowIndex]); |
|||
|
0 ignored issues
–
show
|
|||||
| 65 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 66 | |||||
| 67 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 68 | * @param int $rowIndex |
||||
|
0 ignored issues
–
show
|
|||||
| 69 | * @param int $colIndex |
||||
|
0 ignored issues
–
show
|
|||||
| 70 | * @return string |
||||
| 71 | * @throws EddIriarte\Console\Inputs\Exceptions\IndexOutOfRange |
||||
|
0 ignored issues
–
show
|
|||||
| 72 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 73 | 7 | public function getEntryAt(int $rowIndex, int $colIndex): string |
|||
| 74 | { |
||||
|
0 ignored issues
–
show
|
|||||
| 75 | 7 | if ($this->hasEntryAt($rowIndex, $colIndex)) { |
|||
| 76 | 6 | return $this->getChunks()[$rowIndex][$colIndex]; |
|||
| 77 | } |
||||
| 78 | |||||
| 79 | 1 | throw new IndexOutOfRange("{$rowIndex}:{$colIndex}"); |
|||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $rowIndex instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
As per coding-style, please use concatenation or
sprintf for the variable $colIndex instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||||
| 80 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 81 | } |
||||
|
0 ignored issues
–
show
|
|||||
| 82 |