Skip to content

Commit 883de3f

Browse files
committed
PHP Stan level 0
1 parent cf17ce0 commit 883de3f

File tree

112 files changed

+947
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+947
-771
lines changed

lib/LongitudeOne/Spatial/DBAL/Types/AbstractSpatialType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use Doctrine\DBAL\Platforms\AbstractPlatform;
2222
use Doctrine\DBAL\Platforms\MySqlPlatform;
23-
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
23+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
2424
use Doctrine\DBAL\Types\Type;
2525
use LongitudeOne\Spatial\DBAL\Platform\MySql;
2626
use LongitudeOne\Spatial\DBAL\Platform\PlatformInterface;
@@ -227,7 +227,7 @@ private function getSpatialPlatform(AbstractPlatform $platform)
227227
return new MySql();
228228
}
229229

230-
if ($platform instanceof PostgreSqlPlatform) {
230+
if ($platform instanceof PostgreSQLPlatform) {
231231
return new PostgreSql();
232232
}
233233

lib/LongitudeOne/Spatial/PHP/Types/AbstractGeometry.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
namespace LongitudeOne\Spatial\PHP\Types;
2020

21-
use JsonSerializable;
2221
use LongitudeOne\Spatial\Exception\InvalidValueException;
2322
use LongitudeOne\Spatial\PHP\Types\Geometry\GeometryInterface;
2423

@@ -362,6 +361,13 @@ private function toStringPolygon(array $polygon)
362361
return $this->toStringMultiLineString($polygon);
363362
}
364363

364+
/**
365+
* Type of this geometry: Linestring, point, etc.
366+
*
367+
* @return string
368+
*/
369+
abstract public function getType();
370+
365371
/**
366372
* Convert this abstract geometry to an array.
367373
*
+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Class Doctrine\\\\DBAL\\\\Platforms\\\\MySqlPlatform not found\\.$#"
5+
count: 1
6+
path: ../../lib/LongitudeOne/Spatial/DBAL/Types/AbstractSpatialType.php
7+
-
8+
message: "#^Class Doctrine\\\\DBAL\\\\Platforms\\\\MySQL57Platform not found\\.$#"
9+
count: 1
10+
path: ../../tests/LongitudeOne/Spatial/Tests/SpatialTestCase.php

tests/LongitudeOne/Spatial/Tests/DBAL/Types/Geometry/PointTypeTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
use Doctrine\DBAL\Platforms\MySQLPlatform;
2222
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
2323
use LongitudeOne\Spatial\Tests\Fixtures\PointEntity;
24-
use LongitudeOne\Spatial\Tests\Helper\PersistHelperTrait;
25-
use LongitudeOne\Spatial\Tests\Helper\PointHelperTrait;
24+
use LongitudeOne\Spatial\Tests\Helper\PersistantPointHelperTrait;
2625
use LongitudeOne\Spatial\Tests\OrmTestCase;
2726

2827
/**
@@ -39,8 +38,7 @@
3938
*/
4039
class PointTypeTest extends OrmTestCase
4140
{
42-
use PersistHelperTrait;
43-
use PointHelperTrait;
41+
use PersistantPointHelperTrait;
4442

4543
/**
4644
* Set up the test.

tests/LongitudeOne/Spatial/Tests/DBAL/Types/Geometry/PolygonTypeTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
2323
use LongitudeOne\Spatial\Tests\Fixtures\PolygonEntity;
2424
use LongitudeOne\Spatial\Tests\Helper\LineStringHelperTrait;
25-
use LongitudeOne\Spatial\Tests\Helper\PolygonHelperTrait;
25+
use LongitudeOne\Spatial\Tests\Helper\PersistantPolygonHelperTrait;
2626
use LongitudeOne\Spatial\Tests\OrmTestCase;
2727

2828
/**
@@ -40,7 +40,7 @@
4040
class PolygonTypeTest extends OrmTestCase
4141
{
4242
use LineStringHelperTrait;
43-
use PolygonHelperTrait;
43+
use PersistantPolygonHelperTrait;
4444

4545
/**
4646
* Set up the test.

tests/LongitudeOne/Spatial/Tests/DBAL/Types/GeometryTypeTest.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
use LongitudeOne\Spatial\PHP\Types\Geometry\Polygon;
2828
use LongitudeOne\Spatial\Tests\Fixtures\GeometryEntity;
2929
use LongitudeOne\Spatial\Tests\Fixtures\NoHintGeometryEntity;
30-
use LongitudeOne\Spatial\Tests\Helper\GeometryHelperTrait;
31-
use LongitudeOne\Spatial\Tests\Helper\PersistHelperTrait;
32-
use LongitudeOne\Spatial\Tests\Helper\PolygonHelperTrait;
30+
use LongitudeOne\Spatial\Tests\Helper\PersistantGeometryHelperTrait;
31+
use LongitudeOne\Spatial\Tests\Helper\PersistantPolygonHelperTrait;
3332
use LongitudeOne\Spatial\Tests\OrmTestCase;
3433

3534
/**
@@ -46,9 +45,8 @@
4645
*/
4746
class GeometryTypeTest extends OrmTestCase
4847
{
49-
use GeometryHelperTrait;
50-
use PersistHelperTrait;
51-
use PolygonHelperTrait;
48+
use PersistantGeometryHelperTrait;
49+
use PersistantPolygonHelperTrait;
5250

5351
/**
5452
* Set up the geography type test.

tests/LongitudeOne/Spatial/Tests/Doctrine/ConnectionParameters.php

+27-13
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,38 @@ class ConnectionParameters
2626
/**
2727
* Return common connection parameters.
2828
*
29-
* @return array{driver: string, user: string, password: null|string, host: string, dbname: null|string, port: string, unix_socket: null|string, driverOptions: array<string, string>}
29+
* @return array{driver: ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv'), user: string, password: null|string, host: string, dbname: null|string, port: int, unix_socket: null|string, driverOptions: array<string, string>}
3030
*/
3131
public static function getCommonConnectionParameters(): array
3232
{
3333
// phpcs:enable Generic.Files.LineLength.MaxExceeded
3434
$connectionParams = [
35-
'driver' => $GLOBALS['db_type'],
35+
'driver' => static::checkDriver(),
3636
'user' => $GLOBALS['db_username'],
37-
'password' => null,
37+
'password' => '',
3838
'host' => $GLOBALS['db_host'],
39-
'dbname' => null,
40-
'port' => $GLOBALS['db_port'],
39+
'dbname' => 'main',
40+
'port' => (int) $GLOBALS['db_port'],
4141
];
4242

43-
if (isset($GLOBALS['db_name'])) {
43+
if (null !== $GLOBALS['db_name']) {
4444
$connectionParams['dbname'] = $GLOBALS['db_name'];
4545
}
4646

4747
if (isset($GLOBALS['db_server'])) {
48-
$connectionParams['server'] = $GLOBALS['db_server'];
48+
$connectionParams['server'] = (string) $GLOBALS['db_server'];
4949
}
5050

5151
if (!empty($GLOBALS['db_password'])) {
52-
$connectionParams['password'] = $GLOBALS['db_password'];
52+
$connectionParams['password'] = (string) $GLOBALS['db_password'];
5353
}
5454

5555
if (isset($GLOBALS['db_unix_socket'])) {
5656
$connectionParams['unix_socket'] = $GLOBALS['db_unix_socket'];
5757
}
5858

5959
if (isset($GLOBALS['db_version'])) {
60-
$connectionParams['driverOptions']['server_version'] = (string) $GLOBALS['db_version'];
60+
$connectionParams['driverOptions']['server_version'] = $GLOBALS['db_version'];
6161
}
6262

6363
return $connectionParams;
@@ -66,7 +66,7 @@ public static function getCommonConnectionParameters(): array
6666
/**
6767
* Return connection parameters.
6868
*
69-
* @return array<string, string>
69+
* @return array{driver: ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv'), user: string, password: string, host: string, dbname: string, port: int, unix_socket: string, driverOptions: array<string, string>}
7070
*
7171
* @throws Exception when connection is not successful
7272
*/
@@ -77,22 +77,36 @@ public static function getConnectionParameters(): array
7777

7878
$connection = DriverManager::getConnection($parameters);
7979
$manager = $connection->createSchemaManager();
80-
$dbName = $GLOBALS['db_name'];
80+
$dbName = (string) $GLOBALS['db_name'];
8181
$manager->dropDatabase($dbName);
8282
$manager->createDatabase($dbName);
8383
$parameters['dbname'] = $dbName;
8484

8585
return $parameters;
8686
}
8787

88+
/**
89+
* @return ('ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv') driver
90+
*/
91+
private static function checkDriver(): string
92+
{
93+
$drivers = DriverManager::getAvailableDrivers();
94+
95+
if (in_array($GLOBALS['db_type'], $drivers)) {
96+
return $GLOBALS['db_type'];
97+
}
98+
99+
throw new \InvalidArgumentException(sprintf('Driver %s is not available.', $GLOBALS['driver']));
100+
}
101+
88102
/**
89103
* Return connection parameters for alternate database.
90104
*
91105
* Alternate database is used with PostgreSQL and doctrine/orm3.0,
92106
* because we cannot drop database as long as we are connected to it.
93107
*/
94-
private static function getAlternateDatabaseName(): ?string
108+
private static function getAlternateDatabaseName(): string
95109
{
96-
return $GLOBALS['db_alternate'] ?? $GLOBALS['db_name'] ?? static::getCommonConnectionParameters()['dbname'];
110+
return $GLOBALS['db_alternate'] ?? $GLOBALS['db_name'] ?? static::getCommonConnectionParameters()['dbname'] ?? 'main';
97111
}
98112
}

tests/LongitudeOne/Spatial/Tests/Helper/GeometryHelperTrait.php

+1-98
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@
1818

1919
namespace LongitudeOne\Spatial\Tests\Helper;
2020

21-
use Doctrine\ORM\EntityManagerInterface;
2221
use LongitudeOne\Spatial\Exception\InvalidValueException;
23-
use LongitudeOne\Spatial\PHP\Types\Geometry\GeometryInterface;
24-
use LongitudeOne\Spatial\PHP\Types\Geometry\LineString;
2522
use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
26-
use LongitudeOne\Spatial\Tests\Fixtures\GeometryEntity;
2723

2824
/**
2925
* GeometryHelperTrait Trait.
@@ -35,10 +31,7 @@
3531
*
3632
* @see /docs/Test.rst
3733
*
38-
* @author Alexandre Tranchant <[email protected]>
39-
* @license https://alexandre-tranchant.mit-license.org MIT
40-
*
41-
* @method EntityManagerInterface getEntityManager Return the entity interface
34+
* @method static never fail(string $message = '')
4235
*/
4336
trait GeometryHelperTrait
4437
{
@@ -57,94 +50,4 @@ protected static function createGeometryPoint(string $name, float $x, float $y):
5750
static::fail(sprintf('Unable to create point %s(%d %d): %s', $name, $x, $y, $e->getMessage()));
5851
}
5952
}
60-
61-
/**
62-
* Create a geometric Point entity from an array of points.
63-
*
64-
* @param GeometryInterface $geometry object implementing Geometry interface
65-
*/
66-
protected function persistGeometry(GeometryInterface $geometry): GeometryEntity
67-
{
68-
$entity = new GeometryEntity();
69-
$entity->setGeometry($geometry);
70-
$this->getEntityManager()->persist($entity);
71-
$this->getEntityManager()->flush();
72-
73-
return $entity;
74-
}
75-
76-
/**
77-
* Create a geometric point at A (1 1).
78-
*
79-
* @param null|int $srid Spatial Reference System Identifier
80-
*/
81-
protected function persistGeometryA(?int $srid = null): GeometryEntity
82-
{
83-
$point = static::createGeometryPoint('A', 1, 1);
84-
if (null !== $srid) {
85-
$point->setSrid($srid);
86-
}
87-
88-
return $this->persistGeometry($point);
89-
}
90-
91-
/**
92-
* Create a geometric point E (5 5).
93-
*
94-
* @param null|int $srid Spatial Reference System Identifier
95-
*/
96-
protected function persistGeometryE(?int $srid = null): GeometryEntity
97-
{
98-
$point = static::createGeometryPoint('E', 5, 5);
99-
if (null !== $srid) {
100-
$point->setSrid($srid);
101-
}
102-
103-
return $this->persistGeometry($point);
104-
}
105-
106-
/**
107-
* Create a geometric point at origin.
108-
*
109-
* @param null|int $srid Spatial Reference System Identifier
110-
*/
111-
protected function persistGeometryO(?int $srid = null): GeometryEntity
112-
{
113-
$point = static::createGeometryPoint('O', 0, 0);
114-
if (null !== $srid) {
115-
$point->setSrid($srid);
116-
}
117-
118-
return $this->persistGeometry($point);
119-
}
120-
121-
/**
122-
* Create a straight linestring in a geometry entity.
123-
*/
124-
protected function persistGeometryStraightLine(): GeometryEntity
125-
{
126-
try {
127-
$straightLineString = new LineString([
128-
[1, 1],
129-
[2, 2],
130-
[5, 5],
131-
]);
132-
} catch (InvalidValueException $e) {
133-
static::fail(sprintf('Unable to create linestring Y (1 1, 2 2, 5 5): %s', $e->getMessage()));
134-
}
135-
136-
return $this->persistGeometry($straightLineString);
137-
}
138-
139-
/**
140-
* Persist an entity with null as geometry.
141-
*/
142-
protected function persistNullGeometry(): GeometryEntity
143-
{
144-
$entity = new GeometryEntity();
145-
$this->getEntityManager()->persist($entity);
146-
$this->getEntityManager()->flush();
147-
148-
return $entity;
149-
}
15053
}

0 commit comments

Comments
 (0)