Skip to content

Commit dea6aa4

Browse files
committed
v1 fixes
1 parent 052c250 commit dea6aa4

File tree

11 files changed

+73
-73
lines changed

11 files changed

+73
-73
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ trim_trailing_whitespace = false
1313

1414
[*.{yml,yaml}]
1515
indent_size = 2
16+
17+
[phpstan.neon]
18+
indent_size = 2

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
}
3636
},
3737
"scripts": {
38-
"phpstan": "./vendor/bin/phpstan analyse --memory-limit=2G",
39-
"pest": "./vendor/bin/pest",
40-
"pest-coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --min=100",
38+
"phpstan": "vendor/bin/phpstan analyse --memory-limit=2G",
39+
"test": "vendor/bin/pest",
40+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/pest --coverage --min=100",
4141
"format": "vendor/bin/pint"
4242
},
4343
"config": {

phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ parameters:
99
level: max
1010
checkMissingIterableValueType: true
1111
checkGenericClassInNonGenericObjectType: false
12+
ignoreErrors:
13+
-
14+
message: '#^Call to an undefined method Pest\\Expectation\|Pest\\Support\\Extendable\:\:.+\(\)\.$#'
15+
path: tests/*.php
16+
-
17+
message: '#^Access to an undefined property Pest\\Expectation\|Pest\\Support\\Extendable\:\:\$.+\.$#'
18+
path: tests/*.php

src/Eloquent/GeometryCast.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function get($model, string $key, mixed $value, array $attributes): ?Geom
3434
}
3535

3636
if ($value instanceof Expression) {
37-
$wkt = $this->extractWktFromExpression($value, $model->getConnection());
38-
$srid = $this->extractSridFromExpression($value, $model->getConnection());
37+
$wkt = $this->extractWktFromExpression($value);
38+
$srid = $this->extractSridFromExpression($value);
3939

4040
return $this->className::fromWkt($wkt, $srid);
4141
}
@@ -73,22 +73,16 @@ public function set($model, string $key, mixed $value, array $attributes): Expre
7373
return $value->toSqlExpression($model->getConnection());
7474
}
7575

76-
private function extractWktFromExpression(Expression $expression, Connection $connection): string
76+
private function extractWktFromExpression(Expression $expression): string
7777
{
78-
$grammar = $connection->getQueryGrammar();
79-
$expressionValue = $expression->getValue($grammar);
80-
81-
preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match);
78+
preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expression, $match);
8279

8380
return $match[1];
8481
}
8582

86-
private function extractSridFromExpression(Expression $expression, Connection $connection): int
83+
private function extractSridFromExpression(Expression $expression): int
8784
{
88-
$grammar = $connection->getQueryGrammar();
89-
$expressionValue = $expression->getValue($grammar);
90-
91-
preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expressionValue, $match);
85+
preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expression, $match);
9286

9387
return (int) $match[1];
9488
}

src/Eloquent/HasSpatial.php

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,34 @@ public function scopeWithDistance(
3333
Expression|Geometry|string $geometryOrColumn,
3434
string $alias = 'distance'
3535
): Builder {
36-
if (is_null($this->columns)) {
36+
if (is_null($query->getQuery()->columns)) {
3737
$query->select();
3838
}
3939

40-
$query->selectRaw(
41-
sprintf(
42-
'ST_DISTANCE(%s, %s) AS %s',
43-
$this->toSpatialExpressionString($query, $column),
44-
$this->toSpatialExpressionString($query, $geometryOrColumn),
45-
$alias,
46-
)
47-
);
40+
return $query->selectRaw(sprintf(
41+
'ST_DISTANCE(%s, %s) AS %s',
42+
$this->toSpatialExpressionString($query, $column),
43+
$this->toSpatialExpressionString($query, $geometryOrColumn),
44+
$alias,
45+
));
46+
}
4847

49-
return $query;
48+
public function scopeWithDistanceSphere(
49+
Builder $query,
50+
Expression|Geometry|string $column,
51+
Expression|Geometry|string $geometryOrColumn,
52+
string $alias = 'distance'
53+
): Builder {
54+
if (is_null($query->getQuery()->columns)) {
55+
$query->select();
56+
}
57+
58+
return $query->selectRaw(sprintf(
59+
'ST_DISTANCE_SPHERE(%s, %s) AS %s',
60+
$this->toSpatialExpressionString($query, $column),
61+
$this->toSpatialExpressionString($query, $geometryOrColumn),
62+
$alias,
63+
));
5064
}
5165

5266
public function scopeWhereDistance(
@@ -87,28 +101,6 @@ public function scopeOrderByDistance(
87101
return $query;
88102
}
89103

90-
public function scopeWithDistanceSphere(
91-
Builder $query,
92-
Expression|Geometry|string $column,
93-
Expression|Geometry|string $geometryOrColumn,
94-
string $alias = 'distance'
95-
): Builder {
96-
if (is_null($this->columns)) {
97-
$query->select();
98-
}
99-
100-
$query->selectRaw(
101-
sprintf(
102-
'ST_DISTANCE_SPHERE(%s, %s) AS %s',
103-
$this->toSpatialExpressionString($query, $column),
104-
$this->toSpatialExpressionString($query, $geometryOrColumn),
105-
$alias,
106-
)
107-
);
108-
109-
return $query;
110-
}
111-
112104
public function scopeWhereDistanceSphere(
113105
Builder $query,
114106
Expression|Geometry|string $column,
@@ -337,6 +329,6 @@ protected function toSpatialExpressionString(Builder $query, Expression|Geometry
337329
$expression = $query->raw($grammar->wrap($value));
338330
}
339331

340-
return (string)$expression->getValue($grammar);
332+
return (string)$expression;
341333
}
342334
}

src/LaravelSpatialServiceProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
namespace ASanikovich\LaravelSpatial;
66

7-
use Doctrine\DBAL\Exception;
87
use Doctrine\DBAL\Types\Type;
98
use Illuminate\Database\DatabaseServiceProvider;
109
use Illuminate\Support\Facades\DB;
1110
use ASanikovich\LaravelSpatial\Enums\GeometryType;
1211
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
12+
use Throwable;
1313

1414
final class LaravelSpatialServiceProvider extends DatabaseServiceProvider
1515
{
1616
/**
1717
* @throws LaravelSpatialException
18-
* @throws Exception
18+
* @throws Throwable
1919
*/
2020
public function boot(): void
2121
{
@@ -31,7 +31,7 @@ public function boot(): void
3131
}
3232

3333
/**
34-
* @throws Exception
34+
* @throws Throwable
3535
*/
3636
private function registerDoctrineTypes(): void
3737
{
@@ -44,7 +44,7 @@ private function registerDoctrineTypes(): void
4444

4545
/**
4646
* @param class-string<Type> $class
47-
* @throws Exception
47+
* @throws Throwable
4848
*/
4949
private function registerDoctrineType(string $class, string $type): void
5050
{
@@ -58,7 +58,7 @@ private function registerDoctrineType(string $class, string $type): void
5858
*/
5959
private function validateConfig(): void
6060
{
61-
/** @var array<class-string<Geometry\Geometry>> $config */
61+
/** @var array<class-string<Geometry\Geometry>>|array<string> $config */
6262
$config = config('laravel-spatial');
6363

6464
foreach (GeometryType::cases() as $type) {
@@ -70,6 +70,7 @@ private function validateConfig(): void
7070
}
7171

7272
$baseClass = $type->getBaseGeometryClassName();
73+
/** @phpstan-ignore-next-line */
7374
if ($configType !== $baseClass && ! $configType instanceof $baseClass) {
7475
throw new LaravelSpatialException(sprintf(
7576
'Class for geometry type "%s" should be instance of "%s" ("%s" provided), please check config',

tests/database/TestFactories/TestPlaceFactory.php renamed to tests/Database/TestFactories/TestPlaceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestPlaceFactory extends Factory
1313
protected $model = TestPlace::class;
1414

1515
/**
16-
* @return array<string, mixed>
16+
* @return array<string, string>
1717
*/
1818
public function definition(): array
1919
{

tests/database/TestModels/TestPlace.php renamed to tests/Database/TestModels/TestPlace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Illuminate\Database\Eloquent\Model;
1616

1717
/**
18+
* @property-read int $id
19+
* @property-read int $id_new
1820
* @property string $name
1921
* @property Point $point
2022
* @property MultiPoint $multi_point
@@ -26,6 +28,7 @@
2628
* @property float|null $distance
2729
* @property float|null $distance_in_meters
2830
*
31+
* @method static query()
2932
* @mixin Model
3033
*/
3134
class TestPlace extends Model
File renamed without changes.

tests/Eloquent/HasSpatialTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
1515

1616
/** @var TestPlace $testPlaceWithDistance */
17-
$testPlaceWithDistance = TestPlace::withDistance('point', new Point(1, 1, Srid::WGS84->value))->firstOrFail();
17+
$testPlaceWithDistance = TestPlace::query()->select(['id'])->selectRaw(DB::raw('id as id_new'))
18+
->withDistance('point', new Point(1, 1, Srid::WGS84->value))
19+
->firstOrFail();
1820

19-
expect($testPlaceWithDistance->distance)->toBe(156897.79947260793);
21+
expect($testPlaceWithDistance->distance)->toBe(156897.79947260793)
22+
->and($testPlaceWithDistance->id)->toBe(1)
23+
->and($testPlaceWithDistance->id_new)->toBe(1);
2024
})->skip(fn () => ! isSupportAxisOrder());
2125

2226
it('calculates distance - without axis-order', function (): void {
2327
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
2428

2529
/** @var TestPlace $testPlaceWithDistance */
26-
$testPlaceWithDistance = TestPlace::withDistance('point', new Point(1, 1, Srid::WGS84->value))
27-
->firstOrFail();
30+
$testPlaceWithDistance = TestPlace::withDistance('point', new Point(1, 1, Srid::WGS84->value))->firstOrFail();
2831

2932
expect($testPlaceWithDistance->distance)->toBe(1.4142135623730951);
30-
})->skip(fn () => isSupportAxisOrder());
33+
})->skip(fn() => isSupportAxisOrder());
3134

3235
it('calculates distance with alias', function (): void {
3336
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
@@ -37,7 +40,7 @@
3740
->firstOrFail();
3841

3942
expect($testPlaceWithDistance->distance_in_meters)->toBe(156897.79947260793);
40-
})->skip(fn () => ! isSupportAxisOrder());
43+
})->skip(fn() => !isSupportAxisOrder());
4144

4245
it('calculates distance with alias - without axis-order', function (): void {
4346
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
@@ -47,7 +50,7 @@
4750
->firstOrFail();
4851

4952
expect($testPlaceWithDistance->distance_in_meters)->toBe(1.4142135623730951);
50-
})->skip(fn () => isSupportAxisOrder());
53+
})->skip(fn() => isSupportAxisOrder());
5154

5255
it('filters by distance', function (): void {
5356
$pointWithinDistance = new Point(0, 0, Srid::WGS84->value);
@@ -61,7 +64,7 @@
6164

6265
expect($testPlacesWithinDistance)->toHaveCount(1)
6366
->and($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance);
64-
})->skip(fn () => ! isSupportAxisOrder());
67+
})->skip(fn() => !isSupportAxisOrder());
6568

6669
it('filters by distance - without axis-order', function (): void {
6770
$pointWithinDistance = new Point(0, 0, Srid::WGS84->value);
@@ -74,7 +77,7 @@
7477

7578
expect($testPlacesWithinDistance)->toHaveCount(1)
7679
->and($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance);
77-
})->skip(fn () => isSupportAxisOrder());
80+
})->skip(fn() => isSupportAxisOrder());
7881

7982
it('orders by distance ASC', function (): void {
8083
$closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]);
@@ -107,7 +110,7 @@
107110

108111
expect($testPlaceWithDistance->distance)->toBe(157249.59776850493)
109112
->and($testPlaceWithDistance->name)->not()->toBeNull();
110-
})->skip(fn () => ! isSupportAxisOrder());
113+
})->skip(fn() => !isSupportAxisOrder());
111114

112115
it('calculates distance sphere - without axis-order', function (): void {
113116
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
@@ -117,7 +120,7 @@
117120

118121
expect($testPlaceWithDistance->distance)->toBe(157249.0357231545)
119122
->and($testPlaceWithDistance->name)->not()->toBeNull();
120-
})->skip(fn () => isSupportAxisOrder());
123+
})->skip(fn() => isSupportAxisOrder());
121124

122125
it('calculates distance sphere with alias', function (): void {
123126
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
@@ -127,7 +130,7 @@
127130
$testPlaceWithDistance = TestPlace::withDistanceSphere('point', $point, 'distance_in_meters')->firstOrFail();
128131

129132
expect($testPlaceWithDistance->distance_in_meters)->toBe(157249.59776850493);
130-
})->skip(fn () => ! isSupportAxisOrder());
133+
})->skip(fn() => !isSupportAxisOrder());
131134

132135
it('calculates distance sphere with alias - without axis-order', function (): void {
133136
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);
@@ -137,7 +140,7 @@
137140
$testPlaceWithDistance = TestPlace::withDistanceSphere('point', $point, 'distance_in_meters')->firstOrFail();
138141

139142
expect($testPlaceWithDistance->distance_in_meters)->toBe(157249.0357231545);
140-
})->skip(fn () => isSupportAxisOrder());
143+
})->skip(fn() => isSupportAxisOrder());
141144

142145
it('filters distance sphere', function (): void {
143146
$pointWithinDistance = new Point(0, 0, Srid::WGS84->value);
@@ -395,9 +398,8 @@
395398

396399
$result = $method->invoke($model, $model->newQuery(), $polygon);
397400

398-
$grammar = $model->newQuery()->getGrammar();
399401
$connection = $model->newQuery()->getConnection();
400-
$sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue($grammar);
402+
$sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue();
401403
expect($result)->toBe($sqlSerializedPolygon);
402404
});
403405

0 commit comments

Comments
 (0)