Skip to content

Commit

Permalink
Bump php to 8.0 (#587)
Browse files Browse the repository at this point in the history
* Bump php to 8.0

* Bump symfony

* Review

* Improvements

* Fix phpstan
  • Loading branch information
VincentLanglet committed May 26, 2022
1 parent 655b00a commit 992e062
Show file tree
Hide file tree
Showing 35 changed files with 182 additions and 494 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,17 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.0'
- '8.1'
dependencies: [highest]
allowed-to-fail: [false]
symfony-require: ['']
variant: [normal]
include:
- php-version: '7.4'
- php-version: '8.0'
dependencies: lowest
allowed-to-fail: false
variant: normal
- php-version: '8.1'
dependencies: highest
allowed-to-fail: false
symfony-require: 4.4.*
variant: symfony/symfony:"4.4.*"
- php-version: '8.1'
dependencies: highest
allowed-to-fail: false
symfony-require: 5.3.*
variant: symfony/symfony:"5.3.*"
- php-version: '8.1'
dependencies: highest
allowed-to-fail: false
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
],
"homepage": "https://docs.sonata-project.org/projects/exporter",
"require": {
"php": "^7.4 || ^8.0"
"php": "^8.0"
},
"require-dev": {
"doctrine/annotations": "^1.13",
"doctrine/dbal": "^2.13 || ^3.0",
"doctrine/dbal": "^3.1",
"doctrine/mongodb-odm": "^2.0",
"doctrine/orm": "^2.9",
"friendsofphp/php-cs-fixer": "^3.4",
Expand All @@ -42,13 +42,13 @@
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
"rector/rector": "^0.12",
"symfony/config": "^4.4 || ^5.3 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0",
"symfony/http-foundation": "^4.4 || ^5.3 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.3 || ^6.0",
"symfony/config": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/http-foundation": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/phpunit-bridge": "^6.0",
"symfony/property-access": "^4.4 || ^5.3 || ^6.0",
"symfony/routing": "^4.4 || ^5.3 || ^6.0",
"symfony/property-access": "^5.4 || ^6.0",
"symfony/routing": "^5.4 || ^6.0",
"vimeo/psalm": "^4.9.2"
},
"conflict": {
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->importNames();
Expand Down
12 changes: 4 additions & 8 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@

final class Handler
{
private \Iterator $source;

private WriterInterface $writer;

public function __construct(\Iterator $source, WriterInterface $writer)
{
$this->source = $source;
$this->writer = $writer;
public function __construct(
private \Iterator $source,
private WriterInterface $writer
) {
}

public function export(): void
Expand Down
73 changes: 20 additions & 53 deletions src/Source/AbstractPropertySourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,24 @@ abstract class AbstractPropertySourceIterator implements \Iterator
's' => 'S',
];

/**
* @var \Iterator|null
*/
protected $iterator;

/**
* @var PropertyAccessor
*/
protected $propertyAccessor;
protected ?\Iterator $iterator = null;

/**
* @var string default DateTime format
*/
protected $dateTimeFormat;

/**
* @var string[]
*/
protected $fields = [];
protected PropertyAccessor $propertyAccessor;

/**
* @param string[] $fields Fields to export
*/
public function __construct(array $fields, string $dateTimeFormat = 'r')
{
public function __construct(
protected array $fields,
protected string $dateTimeFormat = 'r'
) {
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
$this->fields = $fields;

$this->dateTimeFormat = $dateTimeFormat;
}

/**
* @return array<string, mixed>
*/
#[\ReturnTypeWillChange]
public function current()
public function current(): array
{
$current = $this->iterator->current();

Expand All @@ -81,11 +63,7 @@ public function next(): void
$this->iterator->next();
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function key()
public function key(): mixed
{
return $this->iterator->key();
}
Expand All @@ -108,11 +86,11 @@ public function getDateTimeFormat(): string
}

/**
* @param object|mixed[] $current
* @phpstan-param object|array<mixed> $current TODO: Change to param when https://github.com/rectorphp/rector/issues/7186 is released
*
* @return array<string, mixed>
*/
protected function getCurrentData($current): array
protected function getCurrentData(object | array $current): array
{
$data = [];
foreach ($this->fields as $key => $field) {
Expand All @@ -123,7 +101,7 @@ protected function getCurrentData($current): array
$propertyValue = $this->propertyAccessor->getValue($current, new PropertyPath($propertyPath));

$data[$name] = $this->getValue($propertyValue);
} catch (UnexpectedTypeException $e) {
} catch (UnexpectedTypeException) {
// Non existent object in path will be ignored but a wrong path will still throw exceptions
$data[$name] = null;
}
Expand All @@ -132,27 +110,16 @@ protected function getCurrentData($current): array
return $data;
}

/**
* @param mixed $value
*
* @return bool|int|float|string|null
*/
protected function getValue($value)
protected function getValue(mixed $value): bool | int | float | string | null
{
switch (true) {
case \is_array($value):
return '['.implode(', ', array_map([$this, 'getValue'], $value)).']';
case $value instanceof \Traversable:
return '['.implode(', ', array_map([$this, 'getValue'], iterator_to_array($value))).']';
case $value instanceof \DateTimeInterface:
return $value->format($this->dateTimeFormat);
case $value instanceof \DateInterval:
return $this->getDuration($value);
case \is_object($value):
return method_exists($value, '__toString') ? (string) $value : null;
default:
return $value;
}
return match (true) {
\is_array($value) => '['.implode(', ', array_map([$this, 'getValue'], $value)).']',
$value instanceof \Traversable => '['.implode(', ', array_map([$this, 'getValue'], iterator_to_array($value))).']',
$value instanceof \DateTimeInterface => $value->format($this->dateTimeFormat),
$value instanceof \DateInterval => $this->getDuration($value),
\is_object($value) => method_exists($value, '__toString') ? (string) $value : null,
default => $value,
};
}

/**
Expand Down
73 changes: 16 additions & 57 deletions src/Source/AbstractXmlSourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,113 +22,72 @@
*/
abstract class AbstractXmlSourceIterator implements \Iterator
{
/**
* @var string
*/
protected $filename;

/**
* @var resource|null
* @phpstan-var resource|null
* @psalm-var resource|closed-resource|null
*/
protected $file;

/**
* @var bool
*/
protected $hasHeaders;

/**
* @var string[]
*/
protected $columns = [];
protected array $columns = [];

/**
* @var \XMLParser|null
*/
protected $parser;
protected ?\XMLParser $parser = null;

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

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

/**
* @var array<mixed>|null
*/
protected $currentRow;
protected ?array $currentRow = null;

/**
* @var array<string, array<string>>
*/
protected $bufferedRow = [];

/**
* @var bool
*/
protected $currentRowEnded = false;
protected bool $currentRowEnded = false;

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

public function __construct(string $filename, bool $hasHeaders = true)
{
$this->filename = $filename;
$this->hasHeaders = $hasHeaders;
public function __construct(
protected string $filename,
protected bool $hasHeaders = true
) {
}

/**
* Start element handler.
*
* @param resource $parser
* @param array<string, string> $attributes
*
* @return void
*/
abstract public function tagStart($parser, string $name, array $attributes = []);
abstract public function tagStart(\XMLParser $parser, string $name, array $attributes = []): void;

/**
* End element handler.
*
* @param resource $parser
*
* @return void
*/
abstract public function tagEnd($parser, string $name);
abstract public function tagEnd(\XMLParser $parser, string $name): void;

/**
* Tag content handler.
*
* @param resource $parser
*
* @return void
*/
abstract public function tagContent($parser, string $data);
abstract public function tagContent(\XMLParser $parser, string $data): void;

/**
* @return array<mixed>
*/
#[\ReturnTypeWillChange]
final public function current()
final public function current(): array
{
\assert(\is_array($this->currentRow));

return $this->currentRow;
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
final public function key()
final public function key(): mixed
{
return $this->position;
}
Expand Down
9 changes: 2 additions & 7 deletions src/Source/ChainSourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function addSource(\Iterator $source): void
/**
* @return array<mixed>
*/
#[\ReturnTypeWillChange]
public function current()
public function current(): array
{
return $this->sources->current()->current();
}
Expand All @@ -54,11 +53,7 @@ public function next(): void
$this->sources->current()->next();
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function key()
public function key(): mixed
{
return $this->sources->current()->key();
}
Expand Down

0 comments on commit 992e062

Please sign in to comment.