Skip to content

Commit c3f5719

Browse files
🔧 add PHPstan and remove PHP Code Sniffer (#188)
1 parent f45c2c4 commit c3f5719

258 files changed

Lines changed: 1160 additions & 1546 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.

.github/workflows/_static-analysis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ jobs:
3434
- name: Run lint
3535
run: |
3636
composer lint
37-
37+
38+
- name: Run PHPStan
39+
run: |
40+
composer phpstan

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"require-dev": {
1717
"friendsofphp/php-cs-fixer": "^3.38",
1818
"phpunit/phpunit": "^9.6",
19-
"madewithlove/license-checker": "^v1.0"
19+
"madewithlove/license-checker": "^v1.0",
20+
"phpstan/phpstan": "^2.1"
2021
},
2122
"suggest": {
2223
"ext-imagick": "Required for PDF rasterization and image processing features",
@@ -33,6 +34,7 @@
3334
],
3435
"scripts": {
3536
"lint": "php-cs-fixer fix --dry-run --diff",
37+
"phpstan": "phpstan analyse src --level 6",
3638
"format": "php-cs-fixer fix"
3739
}
3840
}

docs/code_samples/v2_ocr.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use Mindee\V2\Client;
4-
use Mindee\V2\Product\Ocr\Params\OcrParameters;
5-
use Mindee\V2\Product\Ocr\OcrResponse;
4+
use Mindee\V2\Product\OCR\Params\OCRParameters;
5+
use Mindee\V2\Product\OCR\OCRResponse;
66
use Mindee\Input\PathInput;
77

88
$apiKey = "MY_API_KEY";
@@ -14,7 +14,7 @@ $mindeeClient = new Client($apiKey);
1414

1515
// Set ocr parameters
1616
// Note: modelId is mandatory.
17-
$ocrParams = new OcrParameters(
17+
$ocrParams = new OCRParameters(
1818
// ID of the model, required.
1919
$modelId,
2020
);

src/Dependency/DependencyChecker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public static function isImageMagickPolicyAllowed(): void
9393
$imagick = new Imagick();
9494
try {
9595
$imagick->readImage(
96+
/** @phpstan-ignore-next-line */
9697
TestingUtilities::getV1DataDir() . "/products/expense_receipts/default_sample.jpg"
9798
);
9899
} 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, int|float|string|bool|null|array<array-key, mixed>>|null API details field as sent by the server.
3333
*/
34-
public $apiDetails;
34+
public string|array|null $apiDetails;
3535
/**
36-
* @var string|mixed|null API message field as sent by the server.
36+
* @var string|array<string, int|float|string|bool|null|array<array-key, mixed>>|null API message field as sent by the server.
3737
*/
38-
public ?string $apiMessage;
38+
public string|array|null $apiMessage;
3939

4040
/**
41-
* @param array $httpError Array containing the error data.
41+
* @param array<string, int|float|string|bool|null|array<array-key, 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, int|float|string|bool|null|array<array-key, 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(array|string|null $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|null $response Raw server response.
143143
*/
144-
public static function handleError(string $url, $response): self
144+
public static function handleError(string $url, array|string|null $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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ class ExtractedImage
4747
/**
4848
* Initializes a new instance of the ExtractedImage class.
4949
*
50-
* @param mixed $image The extracted image. Not explicitly typed as \Imagick to avoid errors.
50+
* @param Imagick $image The extracted image.
5151
* @param string $filename The filename for the image.
5252
* @param string $saveFormat The format to save the image.
5353
* @param integer $pageIndex The page index of the image.
5454
* @param integer $index The element index of the image.
5555
*
5656
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
5757
*/
58-
public function __construct(mixed $image, string $filename, string $saveFormat, int $pageIndex, int $index)
58+
public function __construct(Imagick $image, string $filename, string $saveFormat, int $pageIndex, int $index)
5959
{
6060
DependencyChecker::isImageMagickAvailable();
6161
DependencyChecker::isGhostscriptAvailable();
@@ -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: 12 additions & 12 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<string|float|integer|boolean|Polygon>> $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,16 +207,15 @@ 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_page%d-%d.%s', $this->filename, $pageIndex, $index, $format);
211212
return new ExtractedImage($extractedImageData, $filename, $format, $pageIndex, $index);
212213
}
213214

214215
/**
215216
* Extracts a single image from a Position field.
216217
*
217-
* @param BaseField $field The field to extract.
218+
* @param BaseField<string|float|integer|boolean|Polygon> $field The field to extract.
218219
* @param integer $pageIndex The page index to extract, begins at 0.
219220
* @param integer $index The index to use for naming the extracted image.
220221
* @param string $filename The output filename.
@@ -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<string|float|integer|boolean|Polygon>> $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;
@@ -316,7 +316,7 @@ protected function extractImageFromBbox(BBox $bbox, int|float $pageIndex): Imagi
316316
* Splits the filename into name and extension.
317317
*
318318
* @param string $filename Name of the file.
319-
* @return array An array containing the name and extension of the file.
319+
* @return array{0: string, 1: string} An array containing the name and extension of the file.
320320
*/
321321
protected static function splitNameStrict(string $filename): array
322322
{

src/Extraction/PDFExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public function getPageCount(): int
8686
/**
8787
* Extracts sub-documents from the source document using list of page indexes.
8888
*
89-
* @param array|InvoiceSplitterV1InvoicePageGroups $pageIndexes List of sub-lists of pages to keep.
89+
* @param array<array<integer>>|InvoiceSplitterV1InvoicePageGroups $pageIndexes List of sub-lists of pages to keep.
9090
*
9191
* @return ExtractedPDF[] list of extracted documents
9292
*
9393
* @throws MindeePDFException Throws if FDPF/FPDI wasn't able to handle the pdf during the extraction.
9494
* @throws InvalidArgumentException Throws if invalid indexes are provided.
9595
*/
96-
public function extractSubDocuments(mixed $pageIndexes): array
96+
public function extractSubDocuments(array|InvoiceSplitterV1InvoicePageGroups $pageIndexes): array
9797
{
9898
$extractedPdfs = [];
9999

@@ -141,12 +141,12 @@ public function extractSubDocuments(mixed $pageIndexes): array
141141
/**
142142
* Extracts invoices as complete PDFs from the document.
143143
*
144-
* @param array|InvoiceSplitterV1InvoicePageGroups $pageIndexes List of sub-lists of pages to keep.
144+
* @param array<array<integer>>|InvoiceSplitterV1InvoicePageGroups $pageIndexes List of sub-lists of pages to keep.
145145
* @param boolean $strict Whether to trust confidence scores or not.
146146
*
147147
* @return ExtractedPDF[] a list of extracted invoices
148148
*/
149-
public function extractInvoices(mixed $pageIndexes, bool $strict = false): array
149+
public function extractInvoices(array|InvoiceSplitterV1InvoicePageGroups $pageIndexes, bool $strict = false): array
150150
{
151151
if (empty($pageIndexes)) {
152152
return [];

src/Geometry/BBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public function getMaxY(): float
8383
/**
8484
* Extends the BBox with the provided points.
8585
*
86-
* @param array|Polygon $points Series of points to add to the BBox.
86+
* @param array<Point>|Polygon $points Series of points to add to the BBox.
8787
*/
88-
public function extendWith(Polygon|array $points): void
88+
public function extendWith(array|Polygon $points): void
8989
{
9090
if ($points instanceof Polygon) {
9191
$sequence = $points->getCoordinates();

0 commit comments

Comments
 (0)