Skip to content

Commit 2f1b074

Browse files
fix a LOT of issues, but not all of them
1 parent fe1ec17 commit 2f1b074

249 files changed

Lines changed: 943 additions & 792 deletions

File tree

Some content is hidden

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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
],
3535
"scripts": {
3636
"lint": "php-cs-fixer fix --dry-run --diff",
37+
"phpstan": "phpstan analyse src --level max",
3738
"format": "php-cs-fixer fix"
3839
}
3940
}

src/Dependency/DependencyChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static function isImageMagickPolicyAllowed(): void
9393
$imagick = new Imagick();
9494
try {
9595
$imagick->readImage(
96-
/** @phpstan-ignore-next-line */
96+
/** @phpstan-ignore-next-line */
9797
TestingUtilities::getV1DataDir() . "/products/expense_receipts/default_sample.jpg"
9898
);
9999
} catch (Exception $e) {

src/Error/MindeeHttpException.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class MindeeHttpException extends MindeeException
2525
*/
2626
public int $statusCode;
2727
/**
28-
* @var string|mixed|null API code as sent by the server.
28+
* @var string|null API code as sent by the server.
2929
*/
3030
public ?string $apiCode;
3131
/**
32-
* @var mixed|null API details field as sent by the server.
32+
* @var string|array<string,mixed>|null API details field as sent by the server.
3333
*/
34-
public $apiDetails;
34+
public mixed $apiDetails;
3535
/**
36-
* @var string|mixed|null API message field as sent by the server.
36+
* @var string|array<string,mixed>|null API message field as sent by the server.
3737
*/
38-
public ?string $apiMessage;
38+
public mixed $apiMessage;
3939

4040
/**
41-
* @param array $httpError Array containing the error data.
41+
* @param array<string,mixed> $httpError Array containing the error data.
4242
* @param string $url Remote URL the error was found on.
4343
* @param integer $code Error code.
4444
*/
@@ -71,11 +71,11 @@ public function __construct(array $httpError, string $url, int $code)
7171
/**
7272
* Builds an appropriate error object from the server reply.
7373
*
74-
* @param array|string $response Parsed server response.
74+
* @param array<string,mixed>|string|null $response Parsed server response.
7575
* @return string[]
7676
* @throws MindeeException Throws if the error itself can't be built.
7777
*/
78-
public static function createErrorObj($response): array
78+
public static function createErrorObj(mixed $response): array
7979
{
8080
if (is_string($response)) {
8181
if (str_contains($response, 'Maximum pdf pages')) {
@@ -125,7 +125,7 @@ public static function createErrorObj($response): array
125125
) {
126126
return $response['api_request']['error'];
127127
}
128-
if (!$response) {
128+
if (!isset($response)) {
129129
throw new MindeeException(
130130
"Request to the API failed.",
131131
ErrorCode::API_REQUEST_FAILED
@@ -139,9 +139,9 @@ public static function createErrorObj($response): array
139139

140140
/**
141141
* @param string $url Remote URL the error was found on.
142-
* @param array|string|boolean $response Raw server response.
142+
* @param array<string,mixed>|string|boolean $response Raw server response.
143143
*/
144-
public static function handleError(string $url, $response): self
144+
public static function handleError(string $url, mixed $response): self
145145
{
146146
if (is_array($response)) {
147147
$dataResponse = $response['data'] ?? ["data" => null];

src/Error/MindeeV2HttpException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Mindee\Error;
66

7+
use Mindee\V2\Parsing\ErrorItem;
78
use Mindee\V2\Parsing\ErrorResponse;
89

910
/**
@@ -29,7 +30,7 @@ class MindeeV2HttpException extends MindeeException
2930
*/
3031
public ?string $errorCode;
3132
/**
32-
* @var array List of associated errors.
33+
* @var array<ErrorItem> List of associated errors.
3334
*/
3435
public array $errors;
3536

src/Extraction/ExtractedImage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function writeToFile(string $outputPath, ?string $format = null, int $qua
8585
$quality = min(100, max(0, $quality));
8686
if ('png' === $format) {
8787
$finalQuality = round($quality * 0.09);
88-
$this->image->setOption('png:compression-level', $finalQuality);
88+
$this->image->setOption('png:compression-level', (string)$finalQuality);
8989
} elseif (in_array($format, ['jpg', 'jpeg'], true)) {
9090
$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
9191
}

src/Extraction/ImageExtractor.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Mindee\Extraction;
66

7+
use Exception;
78
use Mindee\Dependency\DependencyChecker;
89
use Mindee\Error\ErrorCode;
910
use Mindee\Error\MindeeGeometryException;
1011
use Mindee\Error\MindeeImageException;
1112
use Mindee\Error\MindeePDFException;
1213
use Mindee\Geometry\BBox;
1314
use Mindee\Geometry\BBoxUtils;
15+
use Mindee\Geometry\Point;
1416
use Mindee\Geometry\Polygon;
1517
use Mindee\Input\LocalInputSource;
1618
use Mindee\V1\Parsing\Standard\BaseField;
@@ -129,11 +131,11 @@ public function getPageCount(): int
129131
/**
130132
* Extract multiple images on a given page from a list of fields having position data.
131133
*
132-
* @param array $fields List of Fields to extract.
134+
* @param array<BaseField> $fields List of Fields to extract.
133135
* @param integer $pageIndex The page index to extract, begins at 0.
134136
* @param null|string $outputName The base output filename, must have an image extension.
135137
*
136-
* @return array a list of extracted images
138+
* @return array<ExtractedImage> a list of extracted images
137139
*/
138140
public function extractImagesFromPage(array $fields, int $pageIndex, ?string $outputName = null): array
139141
{
@@ -144,12 +146,12 @@ public function extractImagesFromPage(array $fields, int $pageIndex, ?string $ou
144146
/**
145147
* Extracts images from a page.
146148
*
147-
* @param array $polygons List of polygons to extract.
149+
* @param array<Polygon|array<Point>> $polygons List of polygons to extract.
148150
* @param integer $pageIndex The page index to extract, begins at 0.
149151
* @param null|string $filenamePrefix Output filename prefix.
150152
* @param null|string $format Save format for extracted images. Defaults to the original format.
151153
*
152-
* @return array an array of created images
154+
* @return array<ExtractedImage> An array of created images
153155
* @throws MindeeImageException Throws if the image can't be processed.
154156
*/
155157
public function extractPolygonsFromPage(
@@ -173,7 +175,7 @@ public function extractPolygonsFromPage(
173175
$saveFormat
174176
);
175177
}
176-
} catch (ImagickException $e) {
178+
} catch (Exception $e) {
177179
throw new MindeeImageException($e->getMessage(), $e->getCode(), $e);
178180
}
179181

@@ -205,9 +207,8 @@ public function extractPolygonFromPage(
205207
} catch (ImagickException $e) {
206208
throw new MindeeImageException($e->getMessage(), $e->getCode(), $e);
207209
}
208-
$filename ??= $this->filename;
209210
$format ??= $this->saveFormat;
210-
$filename ??= sprintf('%s.%s_page%d-%d.%s', $filename, $format, $pageIndex, $index, $format);
211+
$filename = sprintf('%s.%s_page%d-%d.%s', $filename ?? $this->filename, $format, $pageIndex, $index, $format);
211212
return new ExtractedImage($extractedImageData, $filename, $format, $pageIndex, $index);
212213
}
213214

@@ -264,16 +265,15 @@ public function getInputSource(): LocalInputSource
264265
/**
265266
* Extracts images from a page.
266267
*
267-
* @param array $fields List of Fields to extract.
268+
* @param array<BaseField> $fields List of Fields to extract.
268269
* @param integer $pageIndex The page index to extract, begins at 0.
269270
* @param string $outputName Name of the created file.
270271
* @param string $format The output format.
271272
*
272-
* @return array an array of created images
273+
* @return array<ExtractedImage> An array of created images
273274
*/
274275
protected function extractFromPage(array $fields, int $pageIndex, string $outputName, string $format = 'jpg'): array
275276
{
276-
$format ??= $this->saveFormat;
277277
$extractedImages = [];
278278

279279
$i = 0;

src/Geometry/BBoxUtils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function generateBBoxFromPolygon(Polygon $polygon): ?BBox
3030
/**
3131
* Generates a BBox from an array of polygons. Returns null if no polygons are provided.
3232
*
33-
* @param array $polygons Series of polygons to get the BBox of.
33+
* @param array<Polygon> $polygons Series of polygons to get the BBox of.
3434
*/
3535
public static function generateBBoxFromPolygons(array $polygons): ?BBox
3636
{
@@ -39,7 +39,7 @@ public static function generateBBoxFromPolygons(array $polygons): ?BBox
3939
}
4040
$merged = $polygons[0];
4141
foreach ($polygons as $polygon) {
42-
if ($polygon && $merged !== $polygon) {
42+
if ($merged !== $polygon) {
4343
$merged = PolygonUtils::merge($merged, $polygon);
4444
}
4545
}
@@ -54,7 +54,7 @@ public static function generateBBoxFromPolygons(array $polygons): ?BBox
5454
/**
5555
* Merges an array of bboxes.
5656
*
57-
* @param array $bboxes BBoxes to merge.
57+
* @param array<BBox> $bboxes BBoxes to merge.
5858
*/
5959
public static function mergeBBoxes(array $bboxes): ?BBox
6060
{

src/Geometry/MinMaxUtils.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ class MinMaxUtils
1717
/**
1818
* Retrieves the upper and lower bounds of the y-axis from an array of points.
1919
*
20-
* @param array $points An array of points.
20+
* @param array<Point>|Polygon $points An array of points.
2121
* @throws MindeeGeometryException Throws if the provided array is too small.
2222
*/
23-
public static function getMinMaxY(array $points): MinMax
23+
public static function getMinMaxY(mixed $points): MinMax
2424
{
25+
if (is_a(Polygon::class, $points)) {
26+
$points = $points->getCoordinates();
27+
}
2528
if (count($points) < 1) {
2629
throw new MindeeGeometryException(
2730
'The provided point array must have at least 1 point to calculate the Y bounds.',
@@ -38,11 +41,14 @@ public static function getMinMaxY(array $points): MinMax
3841
/**
3942
* Retrieves the upper and lower bounds of the x-axis from an array of points.
4043
*
41-
* @param array $points An array of points.
44+
* @param array<Point>|Polygon $points An array of points.
4245
* @throws MindeeGeometryException Throws if the provided array is too small.
4346
*/
44-
public static function getMinMaxX(array $points): MinMax
47+
public static function getMinMaxX(mixed $points): MinMax
4548
{
49+
if (is_a(Polygon::class, $points)) {
50+
$points = $points->getCoordinates();
51+
}
4652
if (count($points) < 1) {
4753
throw new MindeeGeometryException(
4854
'The provided point array must have at least 1 point to calculate the X bounds.',

src/Geometry/Point.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
namespace Mindee\Geometry;
66

77
use ArrayAccess;
8+
use BadMethodCallException;
89
use InvalidArgumentException;
910

1011
/**
1112
* Representation of the coordinates of a point.
13+
*
14+
* @implements ArrayAccess<int, float>
1215
*/
1316
class Point implements ArrayAccess
1417
{
@@ -101,12 +104,6 @@ public function offsetSet($offset, $value): void
101104
*/
102105
public function offsetUnset($offset): void
103106
{
104-
if ($offset === 0) {
105-
$this->x = null;
106-
} elseif ($offset === 1) {
107-
$this->y = null;
108-
} else {
109-
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
110-
}
107+
throw new BadMethodCallException("Cannot unset coordinates of a Point.");
111108
}
112109
}

src/Geometry/Polygon.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Polygon
1313
{
1414
/**
15-
* @var Point[]|null Vertices of the polygon.
15+
* @var array<Point>|null Vertices of the polygon.
1616
*/
1717
public ?array $coordinates;
1818

@@ -27,7 +27,7 @@ class Polygon
2727
private MinMax $minMaxX;
2828

2929
/**
30-
* @param array|null $coordinates Coordinates of the polygon as a set of Points.
30+
* @param array<array<float|integer>>|null $coordinates Coordinates of the polygon as a set of Points.
3131
*/
3232
public function __construct(?array $coordinates = null)
3333
{
@@ -146,7 +146,7 @@ public function isEmpty(): bool
146146

147147
/**
148148
* Retrieves the coordinates of the polygon.
149-
*
149+
* @return array<Point>|null Coordinates of the polygon.
150150
*/
151151
public function getCoordinates(): ?array
152152
{

0 commit comments

Comments
 (0)