From fdf8f213864b05b985a899e3e3cf6114ff0abc64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sun, 6 Nov 2022 22:54:36 +0100 Subject: [PATCH] No implicit array creattion in gis generateParams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Krög --- .../classes/Gis/GisGeometryCollection.php | 11 ++- libraries/classes/Gis/GisLineString.php | 20 +++-- libraries/classes/Gis/GisMultiLineString.php | 36 ++++---- libraries/classes/Gis/GisMultiPoint.php | 26 ++++-- libraries/classes/Gis/GisMultiPolygon.php | 45 +++++----- libraries/classes/Gis/GisPoint.php | 20 +++-- libraries/classes/Gis/GisPolygon.php | 36 ++++---- phpstan-baseline.neon | 31 ++++--- psalm-baseline.xml | 87 +++---------------- 9 files changed, 143 insertions(+), 169 deletions(-) diff --git a/libraries/classes/Gis/GisGeometryCollection.php b/libraries/classes/Gis/GisGeometryCollection.php index 24441c223b..cef8ab9833 100644 --- a/libraries/classes/Gis/GisGeometryCollection.php +++ b/libraries/classes/Gis/GisGeometryCollection.php @@ -309,16 +309,19 @@ class GisGeometryCollection extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; $wkt = $data['wkt']; // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' $goem_col = mb_substr($wkt, 19, -1); // Split the geometry collection object to get its constituents. $sub_parts = $this->explodeGeomCol($goem_col); - $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts); + $params = [ + 'srid' => $data['srid'], + 'GEOMETRYCOLLECTION' => [ + 'geom_count' => count($sub_parts), + ], + ]; $i = 0; foreach ($sub_parts as $sub_part) { @@ -329,7 +332,7 @@ class GisGeometryCollection extends GisGeometry $type = mb_substr($sub_part, 0, $type_pos); /** - * @var GisMultiPolygon|GisPolygon|GisMultiPoint|GisPoint|GisMultiLineString|GisLineString $gis_obj + * @var GisMultiPolygon|GisPolygon|GisMultiPoint|GisPoint|GisMultiLineString|GisLineString|null $gis_obj */ $gis_obj = GisFactory::factory($type); if (! $gis_obj) { diff --git a/libraries/classes/Gis/GisLineString.php b/libraries/classes/Gis/GisLineString.php index 75b7182c74..792c630346 100644 --- a/libraries/classes/Gis/GisLineString.php +++ b/libraries/classes/Gis/GisLineString.php @@ -279,14 +279,18 @@ class GisLineString extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'LINESTRING'; + $params = [ + $index => ['gis_type' => 'LINESTRING'], + ]; $wkt = $value; } @@ -295,12 +299,16 @@ class GisLineString extends GisGeometry $points_arr = $this->extractPoints($linestring, null); $no_of_points = count($points_arr); - $params[$index]['LINESTRING']['no_of_points'] = $no_of_points; + $coords = ['no_of_points' => $no_of_points]; for ($i = 0; $i < $no_of_points; $i++) { - $params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0]; - $params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1]; + $coords[$i] = [ + 'x' => $points_arr[$i][0], + 'y' => $points_arr[$i][1], + ]; } + $params[$index]['LINESTRING'] = $coords; + return $params; } } diff --git a/libraries/classes/Gis/GisMultiLineString.php b/libraries/classes/Gis/GisMultiLineString.php index 0ffd2dc9db..0f6464bdc8 100644 --- a/libraries/classes/Gis/GisMultiLineString.php +++ b/libraries/classes/Gis/GisMultiLineString.php @@ -350,36 +350,40 @@ class GisMultiLineString extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'MULTILINESTRING'; + $params = [ + $index => ['gis_type' => 'MULTILINESTRING'], + ]; $wkt = $value; } // Trim to remove leading 'MULTILINESTRING((' and trailing '))' - $multilinestirng = mb_substr($wkt, 17, -2); - // Separate each linestring - $linestirngs = explode('),(', $multilinestirng); - $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs); + $wkt_multilinestring = mb_substr($wkt, 17, -2); + $wkt_linestrings = explode('),(', $wkt_multilinestring); + $coords = ['no_of_lines' => count($wkt_linestrings)]; - $j = 0; - foreach ($linestirngs as $linestring) { - $points_arr = $this->extractPoints($linestring, null); - $no_of_points = count($points_arr); - $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points; + foreach ($wkt_linestrings as $j => $wkt_linestring) { + $points = $this->extractPoints($wkt_linestring, null); + $no_of_points = count($points); + $coords[$j] = ['no_of_points' => $no_of_points]; for ($i = 0; $i < $no_of_points; $i++) { - $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0]; - $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1]; + $coords[$j][$i] = [ + 'x' => $points[$i][0], + 'y' => $points[$i][1], + ]; } - - $j++; } + $params[$index]['MULTILINESTRING'] = $coords; + return $params; } } diff --git a/libraries/classes/Gis/GisMultiPoint.php b/libraries/classes/Gis/GisMultiPoint.php index edc90fabda..8a6fdf86ce 100644 --- a/libraries/classes/Gis/GisMultiPoint.php +++ b/libraries/classes/Gis/GisMultiPoint.php @@ -326,28 +326,36 @@ class GisMultiPoint extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'MULTIPOINT'; + $params = [ + $index => ['gis_type' => 'MULTIPOINT'], + ]; $wkt = $value; } // Trim to remove leading 'MULTIPOINT(' and trailing ')' - $points = mb_substr($wkt, 11, -1); - $points_arr = $this->extractPoints($points, null); + $wkt_points = mb_substr($wkt, 11, -1); + $points = $this->extractPoints($wkt_points, null); - $no_of_points = count($points_arr); - $params[$index]['MULTIPOINT']['no_of_points'] = $no_of_points; + $no_of_points = count($points); + $coords = ['no_of_points' => $no_of_points]; for ($i = 0; $i < $no_of_points; $i++) { - $params[$index]['MULTIPOINT'][$i]['x'] = $points_arr[$i][0]; - $params[$index]['MULTIPOINT'][$i]['y'] = $points_arr[$i][1]; + $coords[$i] = [ + 'x' => $points[$i][0], + 'y' => $points[$i][1], + ]; } + $params[$index]['MULTIPOINT'] = $coords; + return $params; } diff --git a/libraries/classes/Gis/GisMultiPolygon.php b/libraries/classes/Gis/GisMultiPolygon.php index 9c3a81e016..0d1b8547ba 100644 --- a/libraries/classes/Gis/GisMultiPolygon.php +++ b/libraries/classes/Gis/GisMultiPolygon.php @@ -457,45 +457,44 @@ class GisMultiPolygon extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'MULTIPOLYGON'; + $params = [ + $index => ['gis_type' => 'MULTIPOLYGON'], + ]; $wkt = $value; } // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' - $multipolygon = mb_substr($wkt, 15, -3); - // Separate each polygon - $wkt_polygons = explode(')),((', $multipolygon); + $wkt_multipolygon = mb_substr($wkt, 15, -3); + $wkt_polygons = explode(')),((', $wkt_multipolygon); + $coords = ['no_of_polygons' => count($wkt_polygons)]; - $param_row =& $params[$index]['MULTIPOLYGON']; - $param_row['no_of_polygons'] = count($wkt_polygons); - - $k = 0; - foreach ($wkt_polygons as $wkt_polygon) { + foreach ($wkt_polygons as $k => $wkt_polygon) { $wkt_rings = explode('),(', $wkt_polygon); - $param_row[$k]['no_of_lines'] = count($wkt_rings); - $j = 0; - foreach ($wkt_rings as $wkt_ring) { - $points_arr = $this->extractPoints($wkt_ring, null); - $no_of_points = count($points_arr); - $param_row[$k][$j]['no_of_points'] = $no_of_points; + $coords[$k] = ['no_of_lines' => count($wkt_rings)]; + foreach ($wkt_rings as $j => $wkt_ring) { + $points = $this->extractPoints($wkt_ring, null); + $no_of_points = count($points); + $coords[$k][$j] = ['no_of_points' => $no_of_points]; for ($i = 0; $i < $no_of_points; $i++) { - $param_row[$k][$j][$i]['x'] = $points_arr[$i][0]; - $param_row[$k][$j][$i]['y'] = $points_arr[$i][1]; + $coords[$k][$j][$i] = [ + 'x' => $points[$i][0], + 'y' => $points[$i][1], + ]; } - - $j++; } - - $k++; } + $params[$index]['MULTIPOLYGON'] = $coords; + return $params; } } diff --git a/libraries/classes/Gis/GisPoint.php b/libraries/classes/Gis/GisPoint.php index 78ec1c69ba..38fd45f707 100644 --- a/libraries/classes/Gis/GisPoint.php +++ b/libraries/classes/Gis/GisPoint.php @@ -297,23 +297,29 @@ class GisPoint extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'POINT'; + $params = [ + $index => ['gis_type' => 'POINT'], + ]; $wkt = $value; } // Trim to remove leading 'POINT(' and trailing ')' - $point = mb_substr($wkt, 6, -1); - $points_arr = $this->extractPoints($point, null); + $wkt_point = mb_substr($wkt, 6, -1); + $points = $this->extractPoints($wkt_point, null); - $params[$index]['POINT']['x'] = $points_arr[0][0]; - $params[$index]['POINT']['y'] = $points_arr[0][1]; + $params[$index]['POINT'] = [ + 'x' => $points[0][0], + 'y' => $points[0][1], + ]; return $params; } diff --git a/libraries/classes/Gis/GisPolygon.php b/libraries/classes/Gis/GisPolygon.php index 11a845b0a0..07fbcaa9bd 100644 --- a/libraries/classes/Gis/GisPolygon.php +++ b/libraries/classes/Gis/GisPolygon.php @@ -480,36 +480,40 @@ class GisPolygon extends GisGeometry */ public function generateParams(string $value, int $index = -1): array { - $params = []; if ($index == -1) { $index = 0; $data = $this->parseWktAndSrid($value); - $params['srid'] = $data['srid']; + $params = [ + 'srid' => $data['srid'], + $index => [], + ]; $wkt = $data['wkt']; } else { - $params[$index]['gis_type'] = 'POLYGON'; + $params = [ + $index => ['gis_type' => 'POLYGON'], + ]; $wkt = $value; } // Trim to remove leading 'POLYGON((' and trailing '))' - $polygon = mb_substr($wkt, 9, -2); - // Separate each linestring - $linerings = explode('),(', $polygon); - $params[$index]['POLYGON']['no_of_lines'] = count($linerings); + $wkt_polygon = mb_substr($wkt, 9, -2); + $wkt_rings = explode('),(', $wkt_polygon); + $coords = ['no_of_lines' => count($wkt_rings)]; - $j = 0; - foreach ($linerings as $linering) { - $points_arr = $this->extractPoints($linering, null); - $no_of_points = count($points_arr); - $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points; + foreach ($wkt_rings as $j => $wkt_ring) { + $points = $this->extractPoints($wkt_ring, null); + $no_of_points = count($points); + $coords[$j] = ['no_of_points' => $no_of_points]; for ($i = 0; $i < $no_of_points; $i++) { - $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0]; - $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1]; + $coords[$j][$i] = [ + 'x' => $points[$i][0], + 'y' => $points[$i][1], + ]; } - - $j++; } + $params[$index]['POLYGON'] = $coords; + return $params; } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index efb7367131..e2531bcadf 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4340,18 +4340,13 @@ parameters: count: 1 path: libraries/classes/Gis/GisGeometryCollection.php - - - message: "#^Negated boolean expression is always false\\.$#" - count: 1 - path: libraries/classes/Gis/GisGeometryCollection.php - - message: "#^Static property PhpMyAdmin\\\\Gis\\\\GisGeometryCollection\\:\\:\\$instance \\(PhpMyAdmin\\\\Gis\\\\GisGeometryCollection\\) in isset\\(\\) is not nullable\\.$#" count: 1 path: libraries/classes/Gis/GisGeometryCollection.php - - message: "#^Cannot access offset 'x' on array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\.$#" + message: "#^Cannot access offset 'LINESTRING' on array\\{\\}\\|array\\{gis_type\\: 'LINESTRING'\\}\\|int\\.$#" count: 1 path: libraries/classes/Gis/GisLineString.php @@ -4401,12 +4396,12 @@ parameters: path: libraries/classes/Gis/GisLineString.php - - message: "#^Cannot access offset 'no_of_points' on array\\{0\\?\\: array\\{x\\: mixed, y\\: mixed\\}, no_of_points\\: int\\<0, max\\>\\}\\|int\\<1, max\\>\\.$#" + message: "#^Cannot access offset 'MULTILINESTRING' on array\\{\\}\\|array\\{gis_type\\: 'MULTILINESTRING'\\}\\|int\\.$#" count: 1 path: libraries/classes/Gis/GisMultiLineString.php - - message: "#^Cannot access offset 'x' on array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\.$#" + message: "#^Cannot access offset int\\<0, max\\> on non\\-empty\\-array\\<'no_of_points'\\|int\\<0, max\\>, array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\>\\|int\\<1, max\\>\\.$#" count: 1 path: libraries/classes/Gis/GisMultiLineString.php @@ -4461,7 +4456,7 @@ parameters: path: libraries/classes/Gis/GisMultiLineString.php - - message: "#^Cannot access offset 'x' on array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\.$#" + message: "#^Cannot access offset 'MULTIPOINT' on array\\{\\}\\|array\\{gis_type\\: 'MULTIPOINT'\\}\\|int\\.$#" count: 1 path: libraries/classes/Gis/GisMultiPoint.php @@ -4521,12 +4516,17 @@ parameters: path: libraries/classes/Gis/GisMultiPoint.php - - message: "#^Cannot access offset 'MULTIPOLYGON' on array\\{gis_type\\: 'MULTIPOLYGON'\\}\\|int\\.$#" + message: "#^Cannot access offset 'MULTIPOLYGON' on array\\{\\}\\|array\\{gis_type\\: 'MULTIPOLYGON'\\}\\|int\\.$#" count: 1 path: libraries/classes/Gis/GisMultiPolygon.php - - message: "#^Cannot access offset 'no_of_lines' on array\\{0\\: array\\{no_of_points\\: int\\<0, max\\>, 0\\?\\: array\\{x\\: mixed, y\\: mixed\\}\\}, no_of_lines\\: int\\<1, max\\>\\}\\|int\\<1, max\\>\\.$#" + message: "#^Cannot access offset int on non\\-empty\\-array\\<'no_of_lines'\\|int, array\\{no_of_points\\: int\\<0, max\\>, 0\\?\\: array\\{x\\: mixed, y\\: mixed\\}\\}\\|int\\<1, max\\>\\>\\|int\\<1, max\\>\\.$#" + count: 1 + path: libraries/classes/Gis/GisMultiPolygon.php + + - + message: "#^Cannot access offset int\\<0, max\\> on non\\-empty\\-array\\<'no_of_points'\\|int\\<0, max\\>, array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\>\\|int\\<1, max\\>\\.$#" count: 1 path: libraries/classes/Gis/GisMultiPolygon.php @@ -4590,6 +4590,11 @@ parameters: count: 2 path: libraries/classes/Gis/GisMultiPolygon.php + - + message: "#^Cannot access offset 'POINT' on array\\{\\}\\|array\\{gis_type\\: 'POINT'\\}\\|int\\.$#" + count: 1 + path: libraries/classes/Gis/GisPoint.php + - message: "#^Method PhpMyAdmin\\\\Gis\\\\GisPoint\\:\\:generateParams\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -4641,12 +4646,12 @@ parameters: path: libraries/classes/Gis/GisPoint.php - - message: "#^Cannot access offset 'no_of_points' on array\\{0\\?\\: array\\{x\\: mixed, y\\: mixed\\}, no_of_points\\: int\\<0, max\\>\\}\\|int\\<1, max\\>\\.$#" + message: "#^Cannot access offset 'POLYGON' on array\\{\\}\\|array\\{gis_type\\: 'POLYGON'\\}\\|int\\.$#" count: 1 path: libraries/classes/Gis/GisPolygon.php - - message: "#^Cannot access offset 'x' on array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\.$#" + message: "#^Cannot access offset int\\<0, max\\> on non\\-empty\\-array\\<'no_of_points'\\|int\\<0, max\\>, array\\{x\\: mixed, y\\: mixed\\}\\|int\\<0, max\\>\\>\\|int\\<1, max\\>\\.$#" count: 1 path: libraries/classes/Gis/GisPolygon.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 81c5cb09de..9bba536e0f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -7870,10 +7870,6 @@ - - ! $gis_obj - $gis_obj - $type @@ -7921,8 +7917,6 @@ $no_of_points - - $point $point $point @@ -7945,10 +7939,6 @@ - - - - isset(self::$instance) @@ -7982,8 +7972,8 @@ $point[1] $point[1] $point[1] - $points_arr[$i][0] - $points_arr[$i][1] + $points[$i][0] + $points[$i][1] $points_arr[1][0] $points_arr[1][0] $points_arr[1][1] @@ -7999,8 +7989,6 @@ $data_row $no_of_lines $no_of_points - - $point $point $point @@ -8026,14 +8014,6 @@ - - - - - - - - isset(self::$instance) @@ -8067,8 +8047,8 @@ $point[1] $point[1] $point[1] - $points_arr[$i][0] - $points_arr[$i][1] + $points[$i][0] + $points[$i][1] $points_arr[0][0] $points_arr[0][0] $points_arr[0][1] @@ -8080,8 +8060,6 @@ $no_of_points - - $point $point $point @@ -8105,10 +8083,6 @@ - - - - isset(self::$instance) @@ -8118,9 +8092,6 @@ $points_arr - - - $label_point[0] $label_point[0] @@ -8145,15 +8116,12 @@ $label_point[0] $label_point[1] $label_point[1] - $param_row[$k] - $param_row[$k][$j] - $param_row[$k][$j][$i] $point[0] $point[1] - $points_arr[$i][0] - $points_arr[$i][1] + $points[$i][0] + $points[$i][1] $points_arr[0][0] $points_arr[0][1] @@ -8170,20 +8138,6 @@ - $param_row[$k] - $param_row[$k] - $param_row[$k] - $param_row[$k] - $param_row[$k][$j] - $param_row[$k][$j] - $param_row[$k][$j] - $param_row[$k][$j][$i] - $param_row[$k][$j][$i] - - - - - @@ -8211,8 +8165,6 @@ $no_of_lines $no_of_points $no_of_polygons - - $point $point $ring @@ -8242,9 +8194,9 @@ $black $fill_color - + - + isset(self::$instance) @@ -8266,23 +8218,19 @@ + $points[0][0] + $points[0][1] $points_arr[0][0] $points_arr[0][0] $points_arr[0][0] $points_arr[0][0] $points_arr[0][0] - $points_arr[0][0] - $points_arr[0][1] $points_arr[0][1] $points_arr[0][1] $points_arr[0][1] $points_arr[0][1] $points_arr[0][1] - - - - $points_arr[0][0] $points_arr[0][1] @@ -8301,7 +8249,6 @@ - isset(self::$instance) @@ -8341,8 +8288,8 @@ $point[0] $point[1] - $points_arr[$i][0] - $points_arr[$i][1] + $points[$i][0] + $points[$i][1] $points_arr[0][0] $points_arr[0][1] @@ -8376,8 +8323,6 @@ $p1 $p1 $p2 - - $point @@ -8448,14 +8393,6 @@ - - - - - - - - $x1