| Total Complexity | 8 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class TerrainGenerator |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @param int $rows |
||
| 11 | * @param int $columns |
||
| 12 | * @param int | null $seed |
||
| 13 | * @return TerrainCost |
||
| 14 | */ |
||
| 15 | 22 | public function generate($rows, $columns, $seed = null) |
|
| 16 | { |
||
| 17 | 22 | $this->validatePositiveInteger($rows); |
|
| 18 | 16 | $this->validatePositiveInteger($columns); |
|
| 19 | 10 | $this->validateOptionalInteger($seed); |
|
| 20 | |||
| 21 | 3 | mt_srand($seed); |
|
|
|
|||
| 22 | |||
| 23 | 3 | $terrainCost = array(); |
|
| 24 | |||
| 25 | 3 | foreach (range(0, $rows - 1) as $row) { |
|
| 26 | 3 | foreach (range(0, $columns - 1) as $column) { |
|
| 27 | 3 | $terrainCost[$row][$column] = mt_rand(1, 10); |
|
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | 3 | return new TerrainCost($terrainCost); |
|
| 32 | } |
||
| 33 | |||
| 34 | 22 | private function validatePositiveInteger($number) |
|
| 35 | { |
||
| 36 | 22 | $positiveInteger = filter_var($number, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1))); |
|
| 37 | |||
| 38 | 22 | if ($positiveInteger === false) { |
|
| 39 | 12 | throw new \InvalidArgumentException('Invalid positive integer: ' . print_r($number, true)); |
|
| 40 | } |
||
| 41 | 16 | } |
|
| 42 | |||
| 43 | 10 | private function validateOptionalInteger($number) |
|
| 49 | } |
||
| 50 | 3 | } |
|
| 51 | } |
||
| 52 |