Skip to content

Commit

Permalink
PHP 8.2 Update
Browse files Browse the repository at this point in the history
Removed Port/Spreadsheet
  • Loading branch information
FBGER\dwinter committed Aug 22, 2024
1 parent 4e7e055 commit 19293ad
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 10 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
}
],
"require": {
"php": "^7.4|^8.0|^8.1",
"php": ">=8.2",
"ext-zlib": "*",
"doctrine/persistence": "^2.4|^3.0",
"guzzlehttp/guzzle": "^7.5",
"portphp/csv": "^2.0",
"portphp/spreadsheet": "^1.0",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/filesystem": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/string": "^4.4|^5.0|^6.0",
"webmozart/assert" : "^1.11"
"symfony/config": "^6.0",
"symfony/filesystem": "^6.0",
"symfony/framework-bundle": "^6.0",
"symfony/string": "^6.0",
"webmozart/assert" : "^1.11",
"phpoffice/phpspreadsheet": "^2.1"
},
"require-dev": {
"fastbolt/test-helpers": "^0.1.1",
Expand Down
92 changes: 89 additions & 3 deletions src/Reader/XlsxReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace Fastbolt\EntityImporter\Reader;

use Port\Spreadsheet\SpreadsheetReader;
use PhpOffice\PhpSpreadsheet\IOFactory;
use SplFileObject;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
class XlsxReader extends SpreadsheetReader implements ReaderInterface
class XlsxReader implements ReaderInterface
{
/**
* Faulty rows
Expand All @@ -23,21 +23,58 @@ class XlsxReader extends SpreadsheetReader implements ReaderInterface
*/
protected ?array $errors = null;

/**
* @var array
*/
protected array $columnHeaders;

/**
* Total number of rows
*
* @var int
*/
protected int $count;

/**
* @var int|null
*/
protected ?int $headerRowNumber = null;

/**
* @var int
*/
protected int $pointer = 0;

/**
* @var array
*/
protected array $worksheet;

/**
* @param SplFileObject $file
* @param array<int,string> $columnHeaders
* @param ?int $headerRowNumber
*/
public function __construct(SplFileObject $file, array $columnHeaders, ?int $headerRowNumber)
{
parent::__construct($file, $headerRowNumber);
$reader = IOFactory::createReaderForFile($file->getPathName());
$reader->setReadDataOnly(true);

$spreadsheet = $reader->load($file->getPathname());

$this->worksheet = $spreadsheet->getActiveSheet()->toArray();

if (null !== $headerRowNumber) {
$this->setHeaderRowNumber($headerRowNumber);
}
$this->setColumnHeaders($columnHeaders);
}

public function setColumnHeaders(array $columnHeaders): void
{
$this->columnHeaders = $columnHeaders;
}

/**
* @inheritDoc
*/
Expand All @@ -61,4 +98,53 @@ public function getErrors(): array
/** @psalm-var array<int,array<int,mixed>> */
return $this->errors;
}

public function current()

Check failure on line 102 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MethodSignatureMustProvideReturnType

src/Reader/XlsxReader.php:102:21: MethodSignatureMustProvideReturnType: Method Fastbolt\EntityImporter\Reader\XlsxReader::current must have a return type signature (see https://psalm.dev/282)

Check failure on line 102 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MethodSignatureMustProvideReturnType

src/Reader/XlsxReader.php:102:21: MethodSignatureMustProvideReturnType: Method Fastbolt\EntityImporter\Reader\XlsxReader::current must have a return type signature (see https://psalm.dev/282)
{
$row = $this->worksheet[$this->pointer];

Check failure on line 104 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedAssignment

src/Reader/XlsxReader.php:104:9: MixedAssignment: Unable to determine the type that $row is being assigned to (see https://psalm.dev/032)

Check failure on line 104 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedAssignment

src/Reader/XlsxReader.php:104:9: MixedAssignment: Unable to determine the type that $row is being assigned to (see https://psalm.dev/032)

// If the spreadsheet file has column headers, use them to construct an associative
// array for the columns in this line
if (!empty($this->columnHeaders) && count($this->columnHeaders) === count($row)) {

Check failure on line 108 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

src/Reader/XlsxReader.php:108:83: MixedArgument: Argument 1 of count cannot be mixed, expecting Countable|array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 108 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

src/Reader/XlsxReader.php:108:83: MixedArgument: Argument 1 of count cannot be mixed, expecting Countable|array<array-key, mixed> (see https://psalm.dev/030)
return array_combine(array_values($this->columnHeaders), $row);

Check failure on line 109 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgumentTypeCoercion

src/Reader/XlsxReader.php:109:34: MixedArgumentTypeCoercion: Argument 1 of array_combine expects array<array-key, array-key>, but parent type non-empty-list<mixed> provided (see https://psalm.dev/194)

Check failure on line 109 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

src/Reader/XlsxReader.php:109:70: MixedArgument: Argument 2 of array_combine cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 109 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgumentTypeCoercion

src/Reader/XlsxReader.php:109:34: MixedArgumentTypeCoercion: Argument 1 of array_combine expects array<array-key, array-key>, but parent type non-empty-list<mixed> provided (see https://psalm.dev/194)

Check failure on line 109 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

src/Reader/XlsxReader.php:109:70: MixedArgument: Argument 2 of array_combine cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
}

// Else just return the column values
return $row;

Check failure on line 113 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnStatement

src/Reader/XlsxReader.php:113:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 113 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedReturnStatement

src/Reader/XlsxReader.php:113:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
}

public function setHeaderRowNumber(int $rowNumber): void
{
$this->headerRowNumber = $rowNumber;
$this->columnHeaders = $this->worksheet[$rowNumber];

Check failure on line 119 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedAssignment

src/Reader/XlsxReader.php:119:9: MixedAssignment: Unable to determine the type that $this->columnHeaders is being assigned to (see https://psalm.dev/032)

Check failure on line 119 in src/Reader/XlsxReader.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedAssignment

src/Reader/XlsxReader.php:119:9: MixedAssignment: Unable to determine the type that $this->columnHeaders is being assigned to (see https://psalm.dev/032)
}

public function getColumnHeaders(): array
{
return $this->columnHeaders;
}

public function next(): void
{
$this->pointer++;
}

public function key(): mixed
{
return $this->pointer;
}

public function valid(): bool
{
return isset($this->worksheet[$this->pointer]);
}

public function rewind(): void
{
if (null === $this->headerRowNumber) {
$this->pointer = 0;
} else {
$this->pointer = $this->headerRowNumber + 1;
}
}
}

0 comments on commit 19293ad

Please sign in to comment.