Skip to content

Commit 63a4827

Browse files
committed
add rector
1 parent cf65804 commit 63a4827

File tree

10 files changed

+92
-80
lines changed

10 files changed

+92
-80
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"phpunit/phpunit": "11.*",
1313
"symfony/var-dumper": "5.4 | 6.4 | 7.*",
1414
"friendsofphp/php-cs-fixer": "^3.64",
15-
"phpstan/phpstan": "^1.12"
15+
"phpstan/phpstan": "^1.12",
16+
"rector/rector": "^1.2"
1617
},
1718
"autoload": {
1819
"psr-4": {

rector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/examples',
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
])
13+
->withPhpSets(php82: true)
14+
->withPreparedSets(codeQuality: true, deadCode: true)
15+
->withTypeCoverageLevel(0);
16+

src/AvroExportException.php

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

3+
/*
4+
* This file is part of the SpiriitLabs php-excel-rust package.
5+
* Copyright (c) SpiriitLabs <https://www.spiriit.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
310
namespace Spiriit\Rustsheet;
411

512
class AvroExportException extends \Exception
613
{
7-
8-
}
14+
}

src/ExcelRust.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?php
22

3+
/*
4+
* This file is part of the SpiriitLabs php-excel-rust package.
5+
* Copyright (c) SpiriitLabs <https://www.spiriit.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
310
namespace Spiriit\Rustsheet;
411

512
use Psr\Log\LoggerAwareInterface;
@@ -11,14 +18,13 @@ class ExcelRust implements LoggerAwareInterface
1118
{
1219
use LoggerAwareTrait;
1320

14-
const RUST_GEN_AVRO = 'rust_gen_avro';
15-
const RUST_GEN_HTML = 'rust_gen_html';
21+
public const RUST_GEN_AVRO = 'rust_gen_avro';
22+
public const RUST_GEN_HTML = 'rust_gen_html';
1623

1724
public function __construct(
1825
private WorkbookFactory $workbookFactory,
1926
private string $rustGenLocation,
20-
)
21-
{
27+
) {
2228
}
2329

2430
public function generateExcelFromAvro(ExcelInterface $excel): void
@@ -68,7 +74,7 @@ private function exportAvro(array $results)
6874
{
6975
$schema = file_get_contents(__DIR__.'/../schema.json');
7076

71-
$avroFilePath = sprintf('%s/%s.avro', sys_get_temp_dir(), uniqid('avro', true));
77+
$avroFilePath = \sprintf('%s/%s.avro', sys_get_temp_dir(), uniqid('avro', true));
7278

7379
$exportAvro = new ExportAvro(schema: $schema, pathAvro: $avroFilePath);
7480
$exportAvro->export($results);
@@ -98,15 +104,10 @@ private function rustGen(string $filePath, string $mode, string $outputFile): ar
98104
];
99105
}
100106

101-
private function rustGenHtml(string $htmlFile)
102-
{
103-
104-
}
105-
106107
private function checkProcessStatus(mixed $status, mixed $stdout, mixed $stderr)
107108
{
108109
if (0 !== $status && '' !== $stderr) {
109-
throw new \RuntimeException(\sprintf('The exit status code \'%s\' says something went wrong:' . "\n" . 'stderr: "%s"' . "\n" . 'stdout: "%s"', $status, $stderr, $stdout), $status);
110+
throw new \RuntimeException(\sprintf('The exit status code \'%s\' says something went wrong:'."\n".'stderr: "%s"'."\n".'stdout: "%s"', $status, $stderr, $stdout), $status);
110111
}
111112
}
112113

@@ -125,12 +126,12 @@ private function checkOutput(string $filenameOutput): void
125126

126127
protected function filesize(string $filename): int
127128
{
128-
$filesize = \filesize($filename);
129+
$filesize = filesize($filename);
129130

130131
if (false === $filesize) {
131132
throw new \RuntimeException(\sprintf('Could not read file \'%s\' size.', $filename));
132133
}
133134

134135
return $filesize;
135136
}
136-
}
137+
}

src/Structure/Workbook.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public function toArray(): array
3333
{
3434
return [
3535
'filename' => $this->filename,
36-
'worksheets' => array_map(function (Worksheet $worksheet) {
37-
return $worksheet->toArray();
38-
}, $this->worksheets),
36+
'worksheets' => array_map(fn (Worksheet $worksheet) => $worksheet->toArray(), $this->worksheets),
3937
];
4038
}
4139
}

src/Structure/Worksheet.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111

1212
class Worksheet
1313
{
14-
private string $name;
15-
1614
/**
1715
* @var Cell[]
1816
*/
1917
private array $cells = [];
2018

2119
private bool $autofit = true;
2220

23-
private function __construct(string $name)
21+
private function __construct(private string $name)
2422
{
25-
$this->name = $name;
2623
}
2724

2825
public static function new(?string $name = null): self
@@ -41,14 +38,12 @@ public function toArray(): array
4138
{
4239
return [
4340
'name' => $this->name,
44-
'cells' => array_map(function (Cell $cell) {
45-
return [
46-
'columnIndex' => $cell->columnIndex,
47-
'rowIndex' => $cell->rowIndex,
48-
'value' => $cell->value,
49-
'format' => $cell->format?->toArray(),
50-
];
51-
}, $this->cells),
41+
'cells' => array_map(fn (Cell $cell) => [
42+
'columnIndex' => $cell->columnIndex,
43+
'rowIndex' => $cell->rowIndex,
44+
'value' => $cell->value,
45+
'format' => $cell->format?->toArray(),
46+
], $this->cells),
5247
'autofit' => $this->autofit,
5348
];
5449
}

src/WorkbookBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function addWorksheet(Worksheet $worksheet): self
3737

3838
public function build(): array
3939
{
40-
if (null !== $this->defaultStyleHeader) {
40+
if (is_a($this->defaultStyleHeader, Format::class)) {
4141
/** @var Worksheet $worksheet */
4242
foreach ($this->workbook->getWorksheets() as $worksheet) {
4343
foreach ($worksheet->getCellsRowHeaders() as $cell) {

src/WorkbookFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
class WorkbookFactory
1616
{
17-
public function __construct()
18-
{
19-
}
20-
2117
public function create(ExcelInterface $excel, array $options = []): array
2218
{
2319
$options = $this->getOptionsResolver($excel)->resolve($options);

tests/Fixtures/MyExcel.php

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,56 @@ public function buildSheet(WorkbookBuilder $builder): void
3030
$worksheet = Worksheet::new()
3131
->setName('Worksheet1');
3232

33-
// $worksheet->writeCell(new Cell(columnIndex: 0, rowIndex: 0, format: null, value: 'Item'));
34-
// $worksheet->writeCell(new Cell(columnIndex: 1, rowIndex: 0, format: null, value: 'Cost'));
35-
// $worksheet->writeCell(new Cell(columnIndex: 2, rowIndex: 0, format: Format::new()->bold(), value: 'Date'));
36-
// $worksheet->writeCell(new Cell(columnIndex: 3, rowIndex: 0, format: Format::new()->bold(), value: 'Autre'));
37-
//
38-
// $worksheet->writeCell(
39-
// new Cell(
40-
// columnIndex: 0,
41-
// rowIndex: 1,
42-
// format: null,
43-
// value: 'test'
44-
// ),
45-
// );
46-
//
47-
// $worksheet->writeCell(
48-
// new Cell(
49-
// columnIndex: 1,
50-
// rowIndex: 1,
51-
// format: null,
52-
// value: '=1+1'
53-
// ),
54-
// );
55-
//
56-
// $worksheet->writeCell(
57-
// new Cell(
58-
// columnIndex: 2,
59-
// rowIndex: 1,
60-
// format: Format::new()->setNumFormat('d mmm yyyy'),
61-
// value: '2024-09-28'
62-
// ),
63-
// );
64-
//
65-
// $worksheet->writeCell(
66-
// new Cell(
67-
// columnIndex: 3,
68-
// rowIndex: 1,
69-
// format: Format::new()->fontSize(20)->setNumFormat(Format::FORMAT_NUMBER_CURRENCY_FR),
70-
// value: 1200.554
71-
// ),
72-
// );
33+
// $worksheet->writeCell(new Cell(columnIndex: 0, rowIndex: 0, format: null, value: 'Item'));
34+
// $worksheet->writeCell(new Cell(columnIndex: 1, rowIndex: 0, format: null, value: 'Cost'));
35+
// $worksheet->writeCell(new Cell(columnIndex: 2, rowIndex: 0, format: Format::new()->bold(), value: 'Date'));
36+
// $worksheet->writeCell(new Cell(columnIndex: 3, rowIndex: 0, format: Format::new()->bold(), value: 'Autre'));
37+
//
38+
// $worksheet->writeCell(
39+
// new Cell(
40+
// columnIndex: 0,
41+
// rowIndex: 1,
42+
// format: null,
43+
// value: 'test'
44+
// ),
45+
// );
46+
//
47+
// $worksheet->writeCell(
48+
// new Cell(
49+
// columnIndex: 1,
50+
// rowIndex: 1,
51+
// format: null,
52+
// value: '=1+1'
53+
// ),
54+
// );
55+
//
56+
// $worksheet->writeCell(
57+
// new Cell(
58+
// columnIndex: 2,
59+
// rowIndex: 1,
60+
// format: Format::new()->setNumFormat('d mmm yyyy'),
61+
// value: '2024-09-28'
62+
// ),
63+
// );
64+
//
65+
// $worksheet->writeCell(
66+
// new Cell(
67+
// columnIndex: 3,
68+
// rowIndex: 1,
69+
// format: Format::new()->fontSize(20)->setNumFormat(Format::FORMAT_NUMBER_CURRENCY_FR),
70+
// value: 1200.554
71+
// ),
72+
// );
7373

7474
for ($i = 0; $i < 4000; ++$i) {
7575
for ($j = 0; $j < 50; ++$j) {
76-
if ($j % 2 === 1) {
76+
if (1 === $j % 2) {
7777
$worksheet->writeCell(
7878
new Cell(
7979
columnIndex: $j,
8080
rowIndex: $i,
8181
format: null,
82-
value: "Foo"
82+
value: 'Foo'
8383
),
8484
);
8585
} else {
@@ -92,7 +92,6 @@ public function buildSheet(WorkbookBuilder $builder): void
9292
),
9393
);
9494
}
95-
9695
}
9796
}
9897

tests/GenerateExcelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function it_must_generate_excel(): void
2525

2626
$schema = file_get_contents(__DIR__.'/../schema.json');
2727

28-
$avroFile = __DIR__.DIRECTORY_SEPARATOR.'test.avro';
28+
$avroFile = __DIR__.\DIRECTORY_SEPARATOR.'test.avro';
2929
$exportAvro = new ExportAvro(schema: $schema, pathAvro: $avroFile);
3030
$exportAvro->export($results);
3131

0 commit comments

Comments
 (0)