Skip to content

Commit 9021fbf

Browse files
Merge pull request #19730 from MauricioFauth/equal-operator
Replace equal operator with identical operator in GIS classes
2 parents f221262 + 92db227 commit 9021fbf

9 files changed

Lines changed: 37 additions & 89 deletions

File tree

phpstan-baseline.neon

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6912,30 +6912,6 @@ parameters:
69126912
count: 1
69136913
path: src/Footer.php
69146914

6915-
-
6916-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
6917-
identifier: notEqual.notAllowed
6918-
count: 1
6919-
path: src/Gis/Ds/Point.php
6920-
6921-
-
6922-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
6923-
identifier: equal.notAllowed
6924-
count: 2
6925-
path: src/Gis/Ds/Point.php
6926-
6927-
-
6928-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
6929-
identifier: equal.notAllowed
6930-
count: 2
6931-
path: src/Gis/Ds/Polygon.php
6932-
6933-
-
6934-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
6935-
identifier: notEqual.notAllowed
6936-
count: 2
6937-
path: src/Gis/GisGeometry.php
6938-
69396915
-
69406916
message: '#^Only numeric types are allowed in \+, int\<0, max\>\|false given on the left side\.$#'
69416917
identifier: plus.leftNonNumeric
@@ -7122,12 +7098,6 @@ parameters:
71227098
count: 3
71237099
path: src/Gis/GisMultiPoint.php
71247100

7125-
-
7126-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
7127-
identifier: equal.notAllowed
7128-
count: 8
7129-
path: src/Gis/GisMultiPoint.php
7130-
71317101
-
71327102
message: '#^Parameter \#1 \$point of method PhpMyAdmin\\Gis\\GisGeometry\:\:getWktCoord\(\) expects array\{x\: float\|int\|string, y\: float\|int\|string\}\|null, mixed given\.$#'
71337103
identifier: argument.type
@@ -7255,7 +7225,7 @@ parameters:
72557225
path: src/Gis/GisMultiPolygon.php
72567226

72577227
-
7258-
message: '#^Parameter \#1 \$points of method PhpMyAdmin\\Image\\ImageWrapper\:\:filledPolygon\(\) expects list\<int\>, array\<float\> given\.$#'
7228+
message: '#^Parameter \#1 \$points of method PhpMyAdmin\\Image\\ImageWrapper\:\:filledPolygon\(\) expects list\<int\>, list\<float\> given\.$#'
72597229
identifier: argument.type
72607230
count: 1
72617231
path: src/Gis/GisMultiPolygon.php
@@ -7296,12 +7266,6 @@ parameters:
72967266
count: 1
72977267
path: src/Gis/GisPoint.php
72987268

7299-
-
7300-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
7301-
identifier: equal.notAllowed
7302-
count: 4
7303-
path: src/Gis/GisPoint.php
7304-
73057269
-
73067270
message: '#^Parameter \#1 \$point of method PhpMyAdmin\\Gis\\GisGeometry\:\:getWktCoord\(\) expects array\{x\: float\|int\|string, y\: float\|int\|string\}\|null, mixed given\.$#'
73077271
identifier: argument.type
@@ -7351,7 +7315,7 @@ parameters:
73517315
path: src/Gis/GisPolygon.php
73527316

73537317
-
7354-
message: '#^Parameter \#1 \$points of method PhpMyAdmin\\Image\\ImageWrapper\:\:filledPolygon\(\) expects list\<int\>, array\<float\> given\.$#'
7318+
message: '#^Parameter \#1 \$points of method PhpMyAdmin\\Image\\ImageWrapper\:\:filledPolygon\(\) expects list\<int\>, list\<float\> given\.$#'
73557319
identifier: argument.type
73567320
count: 1
73577321
path: src/Gis/GisPolygon.php
@@ -7398,12 +7362,6 @@ parameters:
73987362
count: 1
73997363
path: src/Gis/GisVisualization.php
74007364

7401-
-
7402-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
7403-
identifier: notEqual.notAllowed
7404-
count: 1
7405-
path: src/Gis/GisVisualization.php
7406-
74077365
-
74087366
message: '#^Binary operation "\*" between 24 and mixed results in an error\.$#'
74097367
identifier: binaryOp.invalid

psalm-baseline.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.11.0@4ed53b7ccebc09ef60ec4c9e464bf8a01bfd35b0">
2+
<files psalm-version="6.12.0@cf420941d061a57050b6c468ef2c778faf40aee2">
33
<file src="app/services_loader.php">
44
<MixedArgument>
55
<code><![CDATA[$argumentName]]></code>
@@ -4622,9 +4622,9 @@
46224622
</RedundantPropertyInitializationCheck>
46234623
</file>
46244624
<file src="src/Gis/GisMultiPolygon.php">
4625-
<ArgumentTypeCoercion>
4625+
<InvalidArgument>
46264626
<code><![CDATA[$pointsArr]]></code>
4627-
</ArgumentTypeCoercion>
4627+
</InvalidArgument>
46284628
<MixedArgument>
46294629
<code><![CDATA[$dataRow[$k][$i][$j] ?? null]]></code>
46304630
<code><![CDATA[$labelPoint[0]]]></code>
@@ -4742,9 +4742,9 @@
47424742
</RedundantPropertyInitializationCheck>
47434743
</file>
47444744
<file src="src/Gis/GisPolygon.php">
4745-
<ArgumentTypeCoercion>
4745+
<InvalidArgument>
47464746
<code><![CDATA[$pointsArr]]></code>
4747-
</ArgumentTypeCoercion>
4747+
</InvalidArgument>
47484748
<MixedArgument>
47494749
<code><![CDATA[$dataRow[$i][$j] ?? null]]></code>
47504750
</MixedArgument>

src/Gis/Ds/Point.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function isInsidePolygon(Polygon $polygon): bool
2121
$noOfPoints = $polygon->count();
2222

2323
// If first point is repeated at the end remove it
24-
if ($polygon->top() == $polygon->bottom()) {
24+
if ($polygon->top()->isEqual($polygon->bottom())) {
2525
--$noOfPoints;
2626
}
2727

@@ -46,11 +46,11 @@ public function isInsidePolygon(Polygon $polygon): bool
4646
continue;
4747
}
4848

49-
if ($p1->y != $p2->y) {
49+
if ($p1->y !== $p2->y) {
5050
$xinters = ($this->y - $p1->y)
5151
* ($p2->x - $p1->x)
5252
/ ($p2->y - $p1->y) + $p1->x;
53-
if ($p1->x == $p2->x || $this->x <= $xinters) {
53+
if ($p1->x === $p2->x || $this->x <= $xinters) {
5454
$counter++;
5555
}
5656
}
@@ -60,4 +60,9 @@ public function isInsidePolygon(Polygon $polygon): bool
6060

6161
return $counter % 2 !== 0;
6262
}
63+
64+
public function isEqual(self $point): bool
65+
{
66+
return $this->x === $point->x && $this->y === $point->y;
67+
}
6368
}

src/Gis/Ds/Polygon.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function area(): float
3131
$noOfPoints = $this->count();
3232

3333
// If the last point is same as the first point ignore it
34-
if ($this->top() == $this->bottom()) {
34+
if ($this->top()->isEqual($this->bottom())) {
3535
--$noOfPoints;
3636
}
3737

@@ -108,7 +108,7 @@ public function getPointOnSurface(): Point|false
108108
//If both are outside the polygon reduce the epsilon and
109109
//recalculate the points(reduce exponentially for faster convergence)
110110
$epsilon **= 2;
111-
if ($epsilon == 0) {
111+
if ($epsilon === 0.0) {
112112
return false;
113113
}
114114
}

src/Gis/GisGeometry.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function generateParams(string $value): array
235235
* @param ScaleData|null $scaleData data related to scaling
236236
* @param bool $linear if true, as a 1D array, else as a 2D array
237237
*
238-
* @return float[]|float[][] scaled points
238+
* @psalm-return ($linear is true ? list<float> : list<array{float, float}>)
239239
*/
240240
private function extractPointsInternal(string $pointSet, ScaleData|null $scaleData, bool $linear): array
241241
{
@@ -249,7 +249,7 @@ private function extractPointsInternal(string $pointSet, ScaleData|null $scaleDa
249249
// Extract coordinates of the point
250250
$coordinates = explode(' ', $point);
251251

252-
if (isset($coordinates[1]) && trim($coordinates[0]) != '' && trim($coordinates[1]) != '') {
252+
if (isset($coordinates[1]) && trim($coordinates[0]) !== '' && trim($coordinates[1]) !== '') {
253253
$x = (float) $coordinates[0];
254254
$y = (float) $coordinates[1];
255255
if ($scaleData !== null) {
@@ -278,14 +278,11 @@ private function extractPointsInternal(string $pointSet, ScaleData|null $scaleDa
278278
* @param string $wktCoords string of comma separated points
279279
* @param ScaleData|null $scaleData data related to scaling
280280
*
281-
* @return float[][] scaled points
281+
* @return list<array{float, float}>
282282
*/
283283
protected function extractPoints1d(string $wktCoords, ScaleData|null $scaleData): array
284284
{
285-
/** @var float[][] $pointsArr */
286-
$pointsArr = $this->extractPointsInternal($wktCoords, $scaleData, false);
287-
288-
return $pointsArr;
285+
return $this->extractPointsInternal($wktCoords, $scaleData, false);
289286
}
290287

291288
/**
@@ -294,14 +291,11 @@ protected function extractPoints1d(string $wktCoords, ScaleData|null $scaleData)
294291
* @param string $wktCoords string of comma separated points
295292
* @param ScaleData|null $scaleData data related to scaling
296293
*
297-
* @return float[] scaled points
294+
* @return list<float>
298295
*/
299296
protected function extractPoints1dLinear(string $wktCoords, ScaleData|null $scaleData): array
300297
{
301-
/** @var float[] $pointsArr */
302-
$pointsArr = $this->extractPointsInternal($wktCoords, $scaleData, true);
303-
304-
return $pointsArr;
298+
return $this->extractPointsInternal($wktCoords, $scaleData, true);
305299
}
306300

307301
/**

src/Gis/GisMultiPoint.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ public function prepareRowAsPng(
8787

8888
foreach ($pointsArr as $point) {
8989
// draw a small circle to mark the point
90-
if ($point[0] == '' || $point[1] == '') {
91-
continue;
92-
}
93-
9490
$image->arc(
9591
(int) round($point[0]),
9692
(int) round($point[1]),
@@ -102,7 +98,7 @@ public function prepareRowAsPng(
10298
);
10399
}
104100

105-
if ($label === '' || $pointsArr[0][0] == '' || $pointsArr[0][1] == '') {
101+
if ($label === '' || ! isset($pointsArr[0])) {
106102
return;
107103
}
108104

@@ -139,14 +135,10 @@ public function prepareRowAsPdf(
139135

140136
foreach ($pointsArr as $point) {
141137
// draw a small circle to mark the point
142-
if ($point[0] == '' || $point[1] == '') {
143-
continue;
144-
}
145-
146138
$pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line);
147139
}
148140

149-
if ($label === '' || $pointsArr[0][0] == '' || $pointsArr[0][1] == '') {
141+
if ($label === '' || ! isset($pointsArr[0])) {
150142
return;
151143
}
152144

src/Gis/GisPoint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function prepareRowAsPng(
8383
$pointsArr = $this->extractPoints1dLinear($point, $scaleData);
8484

8585
// draw a small circle to mark the point
86-
if ($pointsArr[0] == '' || $pointsArr[1] == '') {
86+
if (! isset($pointsArr[0], $pointsArr[1])) {
8787
return;
8888
}
8989

@@ -131,7 +131,7 @@ public function prepareRowAsPdf(
131131
$point = mb_substr($spatial, 6, -1);
132132
$pointsArr = $this->extractPoints1dLinear($point, $scaleData);
133133

134-
if ($pointsArr[0] == '' || $pointsArr[1] == '') {
134+
if (! isset($pointsArr[0], $pointsArr[1])) {
135135
return;
136136
}
137137

src/Gis/GisVisualization.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class GisVisualization
5656
[135, 201, 191],
5757
];
5858

59-
/** @var mixed[][] Raw data for the visualization */
59+
/** @var array<int, array<int|string|null>> Raw data for the visualization */
6060
private array $data;
6161

6262
/** The width of the GIS visualization.*/
@@ -117,8 +117,7 @@ public static function get(
117117
/**
118118
* Get visualization
119119
*
120-
* @param mixed[][] $data Raw data, if set, parameters other
121-
* than $options will be ignored
120+
* @param array<int, array<int|string|null>> $data Raw data, if set, parameters other than $options will be ignored
122121
*/
123122
public static function getByData(array $data, GisVisualizationSettings $options): GisVisualization
124123
{
@@ -131,7 +130,7 @@ public static function getByData(array $data, GisVisualizationSettings $options)
131130
public function hasSrid(): bool
132131
{
133132
foreach ($this->data as $row) {
134-
if ($row['srid'] != 0) {
133+
if ((int) $row['srid'] !== 0) {
135134
return true;
136135
}
137136
}
@@ -142,11 +141,10 @@ public function hasSrid(): bool
142141
/**
143142
* Stores user specified options.
144143
*
145-
* @param mixed[][]|string $sqlOrData SQL to fetch raw data for visualization
146-
* or an array with data.
147-
* If it is an array row and pos are ignored
148-
* @param int $rows Number of rows
149-
* @param int $pos Start position
144+
* @param array<int, array<int|string|null>>|string $sqlOrData SQL to fetch raw data for visualization or an array
145+
* with data. If it is an array row and pos are ignored
146+
* @param int $rows Number of rows
147+
* @param int $pos Start position
150148
*/
151149
private function __construct(
152150
array|string $sqlOrData,
@@ -167,7 +165,7 @@ private function __construct(
167165
: $sqlOrData;
168166
}
169167

170-
/** @return mixed[][] raw data */
168+
/** @return array<int, array<string|null>> */
171169
private function modifyQueryAndFetch(string $sqlQuery): array
172170
{
173171
$modifiedSql = $this->modifySqlQuery($sqlQuery);
@@ -235,7 +233,7 @@ private function modifySqlQuery(string $sqlQuery): string
235233
/**
236234
* Returns raw data for GIS visualization.
237235
*
238-
* @return mixed[][] the raw data.
236+
* @return array<int, array<string|null>>
239237
*/
240238
private function fetchRawData(string $modifiedSql): array
241239
{

tests/unit/Gis/Ds/PolygonTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\Attributes\DataProvider;
1212

1313
#[CoversClass(Polygon::class)]
14+
#[CoversClass(Point::class)]
1415
class PolygonTest extends AbstractTestCase
1516
{
1617
/**

0 commit comments

Comments
 (0)