| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | * |
| 7 | 7 | * @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix) |
| 8 | 8 | * @license https://opensource.org/licenses/MIT MIT |
| 9 | 9 | */ |
| 10 | + | |
| 10 | 11 | namespace Matrix; |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Matrix object. |
| @@ -43,9 +44,9 @@ | ||
| 43 | 44 | * Create a new Matrix object from an array of values |
| 44 | 45 | * |
| 45 | 46 | * @param array $grid |
| 46 | 47 | */ |
| 47 | - public function __construct(array $grid) | |
| 48 | + final public function __construct(array $grid) | |
| 48 | 49 | { |
| 49 | 50 | $this->buildFromArray(array_values($grid)); |
| 50 | 51 | } |
| 51 | 52 | |
| @@ -60,10 +61,9 @@ | ||
| 60 | 61 | $columns = array_reduce( |
| 61 | 62 | $grid, |
| 62 | 63 | function ($carry, $value) { |
| 63 | 64 | return max($carry, is_array($value) ? count($value) : 1); |
| 64 | - }, | |
| 65 | - 0 | |
| 65 | + } | |
| 66 | 66 | ); |
| 67 | 67 | $this->columns = $columns; |
| 68 | 68 | |
| 69 | 69 | array_walk( |
| @@ -81,9 +81,9 @@ | ||
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Validate that a row number is a positive integer |
| 84 | 84 | * |
| 85 | - * @param $row | |
| 85 | + * @param int $row | |
| 86 | 86 | * @return int |
| 87 | 87 | * @throws Exception |
| 88 | 88 | */ |
| 89 | 89 | public static function validateRow($row) |
| @@ -91,15 +91,15 @@ | ||
| 91 | 91 | if ((!is_numeric($row)) || (intval($row) < 1)) { |
| 92 | 92 | throw new Exception('Invalid Row'); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - return (int) $row; | |
| 95 | + return (int)$row; | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * Validate that a column number is a positive integer |
| 100 | 100 | * |
| 101 | - * @param $column | |
| 101 | + * @param int $column | |
| 102 | 102 | * @return int |
| 103 | 103 | * @throws Exception |
| 104 | 104 | */ |
| 105 | 105 | public static function validateColumn($column) |
| @@ -107,15 +107,15 @@ | ||
| 107 | 107 | if ((!is_numeric($column)) || (intval($column) < 1)) { |
| 108 | 108 | throw new Exception('Invalid Column'); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - return (int) $column; | |
| 111 | + return (int)$column; | |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
| 115 | 115 | * Validate that a row number falls within the set of rows for this matrix |
| 116 | 116 | * |
| 117 | - * @param $row | |
| 117 | + * @param int $row | |
| 118 | 118 | * @return int |
| 119 | 119 | * @throws Exception |
| 120 | 120 | */ |
| 121 | 121 | protected function validateRowInRange($row) |
| @@ -130,9 +130,9 @@ | ||
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * Validate that a column number falls within the set of columns for this matrix |
| 133 | 133 | * |
| 134 | - * @param $column | |
| 134 | + * @param int $column | |
| 135 | 135 | * @return int |
| 136 | 136 | * @throws Exception |
| 137 | 137 | */ |
| 138 | 138 | protected function validateColumnInRange($column) |
| @@ -151,9 +151,9 @@ | ||
| 151 | 151 | * A negative $rowCount value will return rows until that many rows from the end of the matrix |
| 152 | 152 | * |
| 153 | 153 | * Note that row numbers start from 1, not from 0 |
| 154 | 154 | * |
| 155 | - * @param $row | |
| 155 | + * @param int $row | |
| 156 | 156 | * @param int $rowCount |
| 157 | 157 | * @return static |
| 158 | 158 | * @throws Exception |
| 159 | 159 | */ |
| @@ -159,13 +159,13 @@ | ||
| 159 | 159 | */ |
| 160 | 160 | public function getRows($row, $rowCount = 1) |
| 161 | 161 | { |
| 162 | 162 | $row = $this->validateRowInRange($row); |
| 163 | - if ($rowCount == 0) { | |
| 163 | + if ($rowCount === 0) { | |
| 164 | 164 | $rowCount = $this->rows - $row + 1; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - return new static(array_slice($this->grid, $row - 1, $rowCount)); | |
| 167 | + return new static(array_slice($this->grid, $row - 1, (int)$rowCount)); | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | 171 | * Return a new matrix as a subset of columns from this matrix, starting at column number $column, and $columnCount columns |
| @@ -173,11 +173,11 @@ | ||
| 173 | 173 | * A negative $columnCount value will return columns until that many columns from the end of the matrix |
| 174 | 174 | * |
| 175 | 175 | * Note that column numbers start from 1, not from 0 |
| 176 | 176 | * |
| 177 | - * @param $column | |
| 177 | + * @param int $column | |
| 178 | 178 | * @param int $columnCount |
| 179 | - * @return static | |
| 179 | + * @return Matrix | |
| 180 | 180 | * @throws Exception |
| 181 | 181 | */ |
| 182 | 182 | public function getColumns($column, $columnCount = 1) |
| 183 | 183 | { |
| @@ -201,9 +201,9 @@ | ||
| 201 | 201 | * A $rowCount value of 0 will remove all rows of the matrix from $row |
| 202 | 202 | * |
| 203 | 203 | * Note that row numbers start from 1, not from 0 |
| 204 | 204 | * |
| 205 | - * @param $row | |
| 205 | + * @param int $row | |
| 206 | 206 | * @param int $rowCount |
| 207 | 207 | * @return static |
| 208 | 208 | * @throws Exception |
| 209 | 209 | */ |
| @@ -209,14 +209,14 @@ | ||
| 209 | 209 | */ |
| 210 | 210 | public function dropRows($row, $rowCount = 1) |
| 211 | 211 | { |
| 212 | 212 | $this->validateRowInRange($row); |
| 213 | - if ($rowCount == 0) { | |
| 213 | + if ($rowCount === 0) { | |
| 214 | 214 | $rowCount = $this->rows - $row + 1; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | $grid = $this->grid; |
| 218 | - array_splice($grid, $row - 1, $rowCount); | |
| 218 | + array_splice($grid, $row - 1, (int)$rowCount); | |
| 219 | 219 | |
| 220 | 220 | return new static($grid); |
| 221 | 221 | } |
| 222 | 222 | |
| @@ -227,9 +227,9 @@ | ||
| 227 | 227 | * A $columnCount value of 0 will remove all columns of the matrix from $column |
| 228 | 228 | * |
| 229 | 229 | * Note that column numbers start from 1, not from 0 |
| 230 | 230 | * |
| 231 | - * @param $column | |
| 231 | + * @param int $column | |
| 232 | 232 | * @param int $columnCount |
| 233 | 233 | * @return static |
| 234 | 234 | * @throws Exception |
| 235 | 235 | */ |
| @@ -238,14 +238,14 @@ | ||
| 238 | 238 | $this->validateColumnInRange($column); |
| 239 | 239 | if ($columnCount < 1) { |
| 240 | 240 | $columnCount = $this->columns + $columnCount - $column + 1; |
| 241 | 241 | } |
| 242 | - | |
| 242 | + | |
| 243 | 243 | $grid = $this->grid; |
| 244 | 244 | array_walk( |
| 245 | 245 | $grid, |
| 246 | 246 | function (&$row) use ($column, $columnCount) { |
| 247 | - array_splice($row, $column - 1, $columnCount); | |
| 247 | + array_splice($row, $column - 1, (int)$columnCount); | |
| 248 | 248 | } |
| 249 | 249 | ); |
| 250 | 250 | |
| 251 | 251 | return new static($grid); |
| @@ -254,11 +254,11 @@ | ||
| 254 | 254 | /** |
| 255 | 255 | * Return a value from this matrix, from the "cell" identified by the row and column numbers |
| 256 | 256 | * Note that row and column numbers start from 1, not from 0 |
| 257 | 257 | * |
| 258 | - * @param $row | |
| 259 | - * @param $column | |
| 260 | - * @return static | |
| 258 | + * @param int $row | |
| 259 | + * @param int $column | |
| 260 | + * @return mixed | |
| 261 | 261 | * @throws Exception |
| 262 | 262 | */ |
| 263 | 263 | public function getValue($row, $column) |
| 264 | 264 | { |
| @@ -337,11 +337,11 @@ | ||
| 337 | 337 | |
| 338 | 338 | /** |
| 339 | 339 | * Access specific properties as read-only (no setters) |
| 340 | 340 | * |
| 341 | - * @param $propertyName | |
| 342 | - * @return mixed | |
| 343 | - * @throws Exception | |
| 341 | + * @param string $propertyName | |
| 342 | + * @return mixed | |
| 343 | + * @throws Exception | |
| 344 | 344 | */ |
| 345 | 345 | public function __get($propertyName) |
| 346 | 346 | { |
| 347 | 347 | $propertyName = strtolower($propertyName); |
| @@ -378,26 +378,23 @@ | ||
| 378 | 378 | |
| 379 | 379 | /** |
| 380 | 380 | * Returns the result of the function call or operation |
| 381 | 381 | * |
| 382 | - * @param string $functionName | |
| 383 | - * @param mixed[] $arguments | |
| 384 | - * @return Matrix|float | |
| 385 | - * @throws Exception|\InvalidArgumentException | |
| 382 | + * @param string $functionName | |
| 383 | + * @param mixed[] $arguments | |
| 384 | + * @return Matrix|float | |
| 385 | + * @throws Exception | |
| 386 | 386 | */ |
| 387 | 387 | public function __call($functionName, $arguments) |
| 388 | 388 | { |
| 389 | 389 | $functionName = strtolower(str_replace('_', '', $functionName)); |
| 390 | 390 | |
| 391 | - // Test for function calls | |
| 392 | - if (in_array($functionName, self::$functions)) { | |
| 391 | + if (in_array($functionName, self::$functions) || in_array($functionName, self::$operations)) { | |
| 393 | 392 | $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}"; |
| 394 | - return $functionName($this, ...$arguments); | |
| 395 | - } | |
| 396 | - // Test for operation calls | |
| 397 | - if (in_array($functionName, self::$operations)) { | |
| 398 | - $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}"; | |
| 399 | - return $functionName($this, ...$arguments); | |
| 393 | + if (is_callable($functionName)) { | |
| 394 | + $arguments = array_values(array_merge([$this], $arguments)); | |
| 395 | + return call_user_func_array($functionName, $arguments); | |
| 396 | + } | |
| 400 | 397 | } |
| 401 | 398 | throw new Exception('Function or Operation does not exist'); |
| 402 | 399 | } |
| 403 | 400 | } |