Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/_static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ jobs:
- name: Run PHPStan
run: |
composer phpstan

- name: Run Rector
run: |
composer rector:check
4 changes: 2 additions & 2 deletions bin/MindeeCLICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mindee\Input\InputSource;
use Mindee\Input\PageOptions;
use Mindee\Input\PathInput;
use Mindee\Input\URLInputSource;
use Mindee\Input\UrlInputSource;
use Mindee\V1\Client;
use Mindee\V1\ClientOptions\PredictMethodOptions;
use Mindee\V1\ClientOptions\PredictOptions;
Expand Down Expand Up @@ -359,7 +359,7 @@ private function areMutuallyExclusivePagesOptions(InputInterface $input, OutputI
* @param string $filePathOrUrl Path of the file, or URL if it's remote.
* @param Client $client Mindee Client.
* @param OutputInterface $output Output interface of the CLI.
* @return PathInput|URLInputSource|null A valid InputSource.
* @return PathInput|UrlInputSource|null A valid InputSource.
*/
private function getFileSource(string $filePathOrUrl, Client $client, OutputInterface $output)
{
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"friendsofphp/php-cs-fixer": "^3.38",
"phpunit/phpunit": "^9.6",
"madewithlove/license-checker": "^v1.0",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-deprecation-rules": "^2.0",
"rector/rector": "^2.4"
},
"suggest": {
"ext-imagick": "Required for PDF rasterization and image processing features",
Expand All @@ -35,6 +37,8 @@
"scripts": {
"lint": "php-cs-fixer fix --dry-run --diff",
"phpstan": "phpstan analyse src --level 6",
"format": "php-cs-fixer fix"
"format": "php-cs-fixer fix",
"rector": "rector process src tests",
"rector:check": "rector process src tests --dry-run"
}
}
2 changes: 1 addition & 1 deletion docs/code_samples/v2_extraction_webhook.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $mindeeClient = new Client($apiKey);
$inferenceParams = new ExtractionParameters(
// ID of the model, required.
$modelId,
webhooksIds: ["MY_WEBHOOK_ID"],
webhookIds: ["MY_WEBHOOK_ID"],

// Options: set to `true` or `false` to override defaults

Expand Down
6 changes: 3 additions & 3 deletions docs/code_samples/v2_ocr.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Mindee\V2\Client;
use Mindee\V2\Product\OCR\Params\OCRParameters;
use Mindee\V2\Product\OCR\OCRResponse;
use Mindee\V2\Product\Ocr\Params\OcrParameters;
use Mindee\V2\Product\Ocr\OcrResponse;
use Mindee\Input\PathInput;

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

// Set ocr parameters
// Note: modelId is mandatory.
$ocrParams = new OCRParameters(
$ocrParams = new OcrParameters(
// ID of the model, required.
$modelId,
);
Expand Down
15 changes: 15 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets()
->withTypeCoverageLevel(0)
->withDeadCodeLevel(0)
->withCodeQualityLevel(0);
4 changes: 2 additions & 2 deletions src/Dependency/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function isGhostscriptAvailable(): void
} else {
$commandWasExecuted = (bool) shell_exec('which gs');
}
} catch (Exception $e) {
} catch (Exception) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable Ghostscript on your PHP installation.",
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function isImageMagickPolicyAllowed(): void
/** @phpstan-ignore-next-line */
TestingUtilities::getV1DataDir() . "/products/expense_receipts/default_sample.jpg"
);
} catch (Exception $e) {
} catch (Exception) {
throw new MindeeUnhandledException(
"To enable full support of PDF features, you need "
. "to enable ImageMagick on your PHP installation. Also, you "
Expand Down
9 changes: 2 additions & 7 deletions src/Error/MindeeHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
*/
class MindeeHttpException extends MindeeException
{
/**
* @var integer Status code as sent by the server.
*/
public int $statusCode;
/**
* @var string|null API code as sent by the server.
*/
Expand All @@ -40,11 +36,10 @@ class MindeeHttpException extends MindeeException
/**
* @param array<string, int|float|string|bool|null|array<array-key, mixed>> $httpError Array containing the error data.
* @param string $url Remote URL the error was found on.
* @param integer $code Error code.
* @param integer $statusCode Error code.
*/
public function __construct(array $httpError, string $url, int $code)
public function __construct(array $httpError, string $url, public int $statusCode)
{
$this->statusCode = $code;
if (array_key_exists('code', $httpError)) {
$this->apiCode = $httpError['code'];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
/**
* Exceptions relating to the handling of PDF documents.
*/
class MindeePDFException extends MindeeException {}
class MindeePdfException extends MindeeException {}
36 changes: 3 additions & 33 deletions src/Extraction/ExtractedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,21 @@
*/
class ExtractedImage
{
/**
* @var Imagick Wrapper for the image.
*/
public Imagick $image;

/**
* @var string Name of the file.
*/
public string $filename;

/**
* @var integer Page ID of the image.
*/
public int $pageId;

/**
* @var integer Element ID of the image.
*/
public int $elementId;

/**
* @var string String representation of the save format.
*/
protected string $saveFormat;

/**
* Initializes a new instance of the ExtractedImage class.
*
* @param Imagick $image The extracted image.
* @param string $filename The filename for the image.
* @param string $saveFormat The format to save the image.
* @param integer $pageIndex The page index of the image.
* @param integer $index The element index of the image.
* @param integer $pageId The page index of the image.
* @param integer $elementId The element index of the image.
*
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
*/
public function __construct(Imagick $image, string $filename, string $saveFormat, int $pageIndex, int $index)
public function __construct(public Imagick $image, public string $filename, protected string $saveFormat, public int $pageId, public int $elementId)
{
DependencyChecker::isImageMagickAvailable();
DependencyChecker::isGhostscriptAvailable();
$this->image = $image;
$this->filename = $filename;
$this->saveFormat = $saveFormat;
$this->pageId = $pageIndex;
$this->elementId = $index;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Mindee\Dependency\DependencyChecker;
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeePDFException;
use Mindee\Error\MindeePdfException;
use Mindee\Error\MindeeUnhandledException;
use Mindee\Input\BytesInput;
use setasign\Fpdi\Fpdi;
Expand All @@ -17,18 +17,8 @@
/**
* An extracted sub-Pdf.
*/
class ExtractedPDF
class ExtractedPdf
{
/**
* @var string name of the original file
*/
public string $filename;

/**
* @var string File object for an ExtractedPdf.
*/
protected string $pdfBytes;

/**
* Initializes a new instance of the ExtractedPdf class.
*
Expand All @@ -37,20 +27,18 @@ class ExtractedPDF
*
* @throws MindeeUnhandledException Throws if PDF operations aren't supported.
*/
public function __construct(string $pdfBytes, string $filename)
public function __construct(protected string $pdfBytes, public string $filename)
{
DependencyChecker::isImageMagickAvailable();
DependencyChecker::isGhostscriptAvailable();
$this->pdfBytes = $pdfBytes;
$this->filename = $filename;
}

/**
* Wrapper for pdf GetPageCount().
*
* @return integer the number of pages in the file
*
* @throws MindeePDFException Throws if FPDI is unable to process the file.
* @throws MindeePdfException Throws if FPDI is unable to process the file.
*/
public function getPageCount(): int
{
Expand All @@ -62,7 +50,7 @@ public function getPageCount(): int

return $pdfHandle->setSourceFile($tempFilename);
} catch (PdfParserException $e) {
throw new MindeePDFException(
throw new MindeePdfException(
"Couldn't open PDF file.",
ErrorCode::PDF_CANT_CREATE,
$e
Expand Down Expand Up @@ -102,7 +90,7 @@ public function asInputSource(): BytesInput
/**
* @return string the pdf bytes
*/
public function getPDFBytes(): string
public function getPdfBytes(): string
{
return $this->pdfBytes;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Extraction/ImageExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeeGeometryException;
use Mindee\Error\MindeeImageException;
use Mindee\Error\MindeePDFException;
use Mindee\Error\MindeePdfException;
use Mindee\Geometry\BBox;
use Mindee\Geometry\BBoxUtils;
use Mindee\Geometry\Point;
Expand Down Expand Up @@ -51,7 +51,7 @@ class ImageExtractor
* @param LocalInputSource $localInput Local input, accepts all compatible formats.
* @param null|string $saveFormat Save format, will be coerced to jpg by default.
*
* @throws MindeePDFException Throws if PDF operations aren't supported, or if the file can't be read, respectively.
* @throws MindeePdfException Throws if PDF operations aren't supported, or if the file can't be read, respectively.
*/
public function __construct(LocalInputSource $localInput, ?string $saveFormat = null)
{
Expand All @@ -71,14 +71,14 @@ public function __construct(LocalInputSource $localInput, ?string $saveFormat =
$this->saveFormat = $saveFormat;
}

if ($this->inputSource->isPDF()) {
$this->pageImages = $this->pdfToImages($this->inputSource->readContents()[1]);
if ($this->inputSource->isPdf()) {
$this->pageImages = static::pdfToImages($this->inputSource->readContents()[1]);
} else {
try {
$image = new Imagick();
$image->readImageBlob($this->inputSource->readContents()[1]);
} catch (ImagickException $e) {
throw new MindeePDFException(
throw new MindeePdfException(
"Image couldn't be processed.",
ErrorCode::IMAGE_CANT_PROCESS,
$e
Expand Down
Loading
Loading