Skip to content

Commit d61c38d

Browse files
committed
increase phpstan level to 7
1 parent 08e6a82 commit d61c38d

File tree

8 files changed

+49
-17
lines changed

8 files changed

+49
-17
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ parameters:
3232

3333
-
3434
message: "#^Unsafe usage of new static\\(\\)\\.$#"
35-
count: 2
35+
count: 3
3636
path: src/Types/GeometryCollection.php
3737

3838
-

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 6
5+
level: 7
66
paths:
77
- src
88
tmpDir: build/phpstan
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Limenet\LaravelMysqlSpatial\Exceptions;
4+
5+
class UnknownWKBException extends \RuntimeException
6+
{
7+
}

src/Types/Geometry.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GeoJson\Feature\Feature;
77
use GeoJson\GeoJson;
88
use Illuminate\Contracts\Support\Jsonable;
9+
use Limenet\LaravelMysqlSpatial\Exceptions\UnknownWKBException;
910
use Limenet\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
1011

1112
abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializable
@@ -45,10 +46,15 @@ public static function getWKTArgument(string $value): string
4546
return substr($value, $left + 1, $right - $left - 1);
4647
}
4748

48-
/** @return class-string */
49+
/** @return class-string<Geometry> */
4950
public static function getWKTClass(string $value): string
5051
{
5152
$left = strpos($value, '(');
53+
54+
if ($left === false) {
55+
throw new UnknownWKTTypeException('Could not parse '.$value);
56+
}
57+
5258
$type = trim(substr($value, 0, $left));
5359

5460
return match (strtoupper($type)) {
@@ -66,7 +72,13 @@ public static function getWKTClass(string $value): string
6672
public static function fromWKB(string $wkb): Geometry
6773
{
6874
$srid = substr($wkb, 0, 4);
69-
$srid = unpack('L', $srid)[1];
75+
$unpacked = unpack('L', $srid);
76+
77+
if ($unpacked === false) {
78+
throw new UnknownWKBException($wkb);
79+
}
80+
81+
$srid = $unpacked[1];
7082

7183
$wkb = substr($wkb, 4);
7284
$parser = new Parser(new Factory());
@@ -110,6 +122,6 @@ public static function fromJson(string|GeoJson $geoJson): self
110122

111123
public function toJson($options = 0)
112124
{
113-
return json_encode($this, $options);
125+
return json_encode($this, $options | JSON_THROW_ON_ERROR);
114126
}
115127
}

src/Types/GeometryCollection.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Limenet\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
1414

1515
/**
16-
* @template G
16+
* @template GeoType of GeometryInterface
1717
*
1818
* @implements GeometryInterface<FeatureCollection>
1919
*/
@@ -32,12 +32,12 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
3232
/**
3333
* The items contained in the spatial collection.
3434
*
35-
* @var G[]
35+
* @var GeoType[]
3636
*/
3737
protected array $items = [];
3838

3939
/**
40-
* @param GeometryInterface[] $geometries
40+
* @param GeoType[] $geometries
4141
*
4242
* @throws InvalidArgumentException
4343
*/
@@ -51,7 +51,7 @@ public function __construct(array $geometries, ?int $srid = 0)
5151
}
5252

5353
/**
54-
* @return G[]
54+
* @return GeoType[]
5555
*/
5656
public function getGeometries(): array
5757
{
@@ -75,12 +75,11 @@ public static function fromString(string $wktArgument, int $srid = 0): static
7575
}
7676

7777
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
78+
if ($geometry_strings === false) {
79+
return new static([]);
80+
}
7881

79-
return new static(array_map(function ($geometry_string) {
80-
$klass = Geometry::getWKTClass($geometry_string);
81-
82-
return call_user_func($klass.'::fromWKT', $geometry_string);
83-
}, $geometry_strings), $srid);
82+
return new static(array_map(fn ($geometry_string) => call_user_func([Geometry::getWKTClass($geometry_string), 'fromWKT'], $geometry_string), $geometry_strings), $srid);
8483
}
8584

8685
public function toArray()
@@ -159,6 +158,8 @@ public function jsonSerialize()
159158

160159
/**
161160
* Checks whether the items are valid to create this collection.
161+
*
162+
* @param GeoType[] $items
162163
*/
163164
protected function validateItems(array $items): void
164165
{
@@ -172,7 +173,7 @@ protected function validateItems(array $items): void
172173
/**
173174
* Checks whether the array has enough items to generate a valid WKT.
174175
*
175-
* @param GeometryInterface[] $items
176+
* @param GeoType[] $items
176177
*
177178
* @see $minimumCollectionItems
178179
*/

src/Types/GeometryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use GeoJson\GeoJson;
66

77
/**
8-
* @template T
8+
* @template GeoType
99
*/
1010
interface GeometryInterface
1111
{
@@ -20,7 +20,7 @@ public static function fromString(string $wktArgument, int $srid = 0): self;
2020
public static function fromJson(string|GeoJson $geoJson): self;
2121

2222
/**
23-
* @return T
23+
* @return GeoType
2424
*/
2525
#[\ReturnTypeWillChange]
2626
public function jsonSerialize();

src/Types/MultiLineString.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeoJson\GeoJson;
66
use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString;
77
use Limenet\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
8+
use RuntimeException;
89

910
/**
1011
* @implements GeometryInterface<LineString>
@@ -36,6 +37,11 @@ public function toWKT(): string
3637
public static function fromString(string $wktArgument, int $srid = 0): static
3738
{
3839
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
40+
41+
if ($str === false) {
42+
throw new RuntimeException();
43+
}
44+
3945
$lineStrings = array_map(fn ($data) => LineString::fromString($data), $str);
4046

4147
return new static($lineStrings, $srid);

src/Types/MultiPolygon.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GeoJson\GeoJson;
66
use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon;
77
use Limenet\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
8+
use RuntimeException;
89

910
/**
1011
* @implements GeometryInterface<MultiPolygon>
@@ -36,6 +37,11 @@ public function __toString()
3637
public static function fromString(string $wktArgument, int $srid = 0): static
3738
{
3839
$parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE);
40+
41+
if ($parts === false) {
42+
throw new RuntimeException();
43+
}
44+
3945
$polygons = static::assembleParts($parts);
4046

4147
return new static(array_map(fn ($polygonString) => Polygon::fromString($polygonString), $polygons), $srid);

0 commit comments

Comments
 (0)