diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..1344ecb --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,11 @@ +exclude('vendor') + ->in(__DIR__); + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => true, + ]) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 23723af..3dba5a8 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,9 @@ }, "require-dev": { "phpunit/phpunit": "^10.0", - "orchestra/testbench": "^8.6" + "phpstan/phpstan": "^1.10", + "friendsofphp/php-cs-fixer": "^3.26", + "squizlabs/php_codesniffer": "^3.7" }, "autoload": { "psr-4": {"AnourValar\\Office\\": "src/"} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..10fcd8d --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,36 @@ + + + + + PHPCS ruleset + + src + tests + + + + + + + + + + + + + + + + + + + + + tests/ + + + + tests/ + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..9c20df8 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,50 @@ +parameters: + + paths: + - src + - tests + + # The level 9 is the highest level + level: 5 + + ignoreErrors: + - '#has an uninitialized readonly property#' + - '#Binary operation \"\+\" between string#' + - '#Call to an undefined method AnourValar\\Office\\Drivers\\SaveInterface\:\:getSheetCount\(\)#' + - '#Call to an undefined method AnourValar\\Office\\Drivers\\SaveInterface\:\:replace\(\)#' + - '#unknown class PhpOffice#' + - '#Unsafe usage of new static#' + - '#Call to an undefined method AnourValar\\Office\\Drivers\\SaveInterface\:\:setGrid\(\)#' + - '#Parameter \#1 \$driver of method AnourValar\\Office\\GridService\:\:getGenerator\(\) expects AnourValar\\Office\\Drivers\\GridInterface#' + - '#Class AnourValar\\Office\\Tests\\SheetsParserTest has an uninitialized property \$service#' + - '#has an uninitialized property \$fileSystem#' + - '#\(\) on iterable\.#' + - '#Instantiated class ZipStream#' + - '#unknown class ZipStream#' + - '#has an uninitialized property \$sourceActiveSheetIndex#' + - '#\$format is assigned outside of the constructor#' + - '#has invalid return type PhpOffice#' + - '#\$spreadsheet is assigned outside of the constructor#' + - '#Binary operation \"\-\" between string and string results in an error#' + - '#Instantiated class PhpOffice#' + - '#expects string, int given#' + - '#expects int, string given#' + - '#has invalid type PhpOffice#' + - '#Match expression does not handle remaining value: mixed#' + - '#Access to an undefined property AnourValar\\Office\\Drivers\\MixInterface\:\:\$spreadsheet#' + - '#Call to an undefined method AnourValar\\Office\\Drivers\\MixInterface\:\:sheet\(\)#' + + + excludePaths: + + + checkFunctionNameCase: true + checkInternalClassCaseSensitivity: true + checkAlwaysTrueInstanceof: true + reportMaybesInMethodSignatures: true + reportStaticMethodSignatures: true + checkUninitializedProperties: true + checkDynamicProperties: true + reportAlwaysTrueInLastCondition: true + reportWrongPhpDocTypeInVarTag: true + checkMissingCallableSignature: true diff --git a/src/Drivers/PhpSpreadsheetDriver.php b/src/Drivers/PhpSpreadsheetDriver.php index 8887d63..f368a73 100644 --- a/src/Drivers/PhpSpreadsheetDriver.php +++ b/src/Drivers/PhpSpreadsheetDriver.php @@ -49,7 +49,7 @@ public function sheet(): \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet */ public function create(): self { - $instance = new static; + $instance = new static(); $instance->spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $instance->sourceActiveSheetIndex = 0; @@ -63,7 +63,7 @@ public function create(): self */ public function load(string $file, \AnourValar\Office\Format $format): self { - $instance = new static; + $instance = new static(); $instance->spreadsheet = IOFactory::createReader($instance->getFormat($format))->load($file); $instance->sourceActiveSheetIndex = $instance->spreadsheet->getActiveSheetIndex(); @@ -240,7 +240,7 @@ public function getValues(?string $ceilRange): array */ public function getMergeCells(): array { - return array_values( $this->sheet()->getMergeCells() ); + return array_values($this->sheet()->getMergeCells()); } /** @@ -531,7 +531,7 @@ public function duplicateRows(string $ceilRange, callable $value, int $indentRow && $this->isColumnGE($item[0][0], $range[0][0]) && $this->isColumnLE($item[0][0], $range[1][0]) // columns && $this->isColumnGE($item[1][0], $range[0][0]) && $this->isColumnLE($item[1][0], $range[1][0]) ) { - $this->mergeCells($item[0][0].($item[0][1]+$shift) . ':' . $item[1][0].($item[1][1]+$shift)); + $this->mergeCells($item[0][0].($item[0][1] + $shift) . ':' . $item[1][0].($item[1][1] + $shift)); } } @@ -617,7 +617,7 @@ public function setStyle(string $range, array $style): self } if (isset($style['align'])) { - $align = match($style['align']) { + $align = match ($style['align']) { \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT => 'left', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER => 'center', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT => 'right', @@ -630,7 +630,7 @@ public function setStyle(string $range, array $style): self } if (isset($style['valign'])) { - $valign = match($style['valign']) { + $valign = match ($style['valign']) { \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP => 'top', \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER => 'center', \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM => 'bottom', @@ -741,11 +741,12 @@ protected function writeConfiguration(\PhpOffice\PhpSpreadsheet\Writer\IWriter $ */ protected function getFormat(\AnourValar\Office\Format $format): string { - return match($format) { + return match ($format) { \AnourValar\Office\Format::Xlsx => 'Xlsx', \AnourValar\Office\Format::Pdf => 'Mpdf', \AnourValar\Office\Format::Html => 'Html', \AnourValar\Office\Format::Ods => 'Ods', + default => throw new \RuntimeException('Format is not supported.'), }; } } diff --git a/src/Drivers/ZipDriver.php b/src/Drivers/ZipDriver.php index 302841e..908d8b9 100644 --- a/src/Drivers/ZipDriver.php +++ b/src/Drivers/ZipDriver.php @@ -4,7 +4,7 @@ class ZipDriver implements DocumentInterface, GridInterface { - use \Anourvalar\Office\Traits\Parser; + use \AnourValar\Office\Traits\Parser; use \AnourValar\Office\Traits\XFormat; /** @@ -41,10 +41,10 @@ public function load(string $file, \AnourValar\Office\Format $format): self throw new \LogicException('Driver only supports Docx, Xlsx formats.'); } - $instance = new static; + $instance = new static(); $fileSystem = []; - $zipArchive = new \ZipArchive; + $zipArchive = new \ZipArchive(); $zipArchive->open($file); try { $count = $zipArchive->numFiles; @@ -82,7 +82,7 @@ public function save(string $file, \AnourValar\Office\Format $format): void try { foreach ($this->fileSystem as $filename => $content) { $zipStream->addFile($filename, $content); - } + } } catch (\Throwable $e) { $zipStream->finish(); ob_get_clean(); @@ -148,7 +148,7 @@ public function setGrid(iterable $data): self $column = 'A'; foreach ($titles as $value) { $value = (string) $value; - if ($value === null || $value === '') { + if ($value === '') { $firstColumn++; $column++; continue; diff --git a/src/Format.php b/src/Format.php index f6dc377..db1c478 100644 --- a/src/Format.php +++ b/src/Format.php @@ -15,7 +15,7 @@ enum Format: string */ public function fileExtension(): string { - return match($this) { + return match ($this) { Format::Xlsx => 'xlsx', Format::Pdf => 'pdf', Format::Html => 'html', @@ -31,7 +31,7 @@ public function fileExtension(): string */ public function contentType(): string { - return match($this) { + return match ($this) { Format::Xlsx => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', Format::Pdf => 'application/pdf', Format::Html => 'text/html', diff --git a/src/GridService.php b/src/GridService.php index 7b6eb13..ea8ff4a 100644 --- a/src/GridService.php +++ b/src/GridService.php @@ -61,7 +61,7 @@ public function __construct(GridInterface $driver = new \AnourValar\Office\Drive * Generate a document from the template (grid) * * @param array $headers - * @param iterable|\Closure $data + * @param iterable|\Closure $data * @param string $leftTopCorner * @return \AnourValar\Office\Generated */ diff --git a/src/Mixer.php b/src/Mixer.php index 1b4549e..e93c607 100644 --- a/src/Mixer.php +++ b/src/Mixer.php @@ -45,7 +45,7 @@ public function __invoke(...$generated): Generated for ($i = 0; $i < $count; $i++) { $driver->setSheet($i); - $driver->setSheetTitle( $titles[] = $this->getTitle($driver->getSheetTitle(), $titles) ); + $driver->setSheetTitle($titles[] = $this->getTitle($driver->getSheetTitle(), $titles)); $referenceDriver->mergeDriver($driver); } } diff --git a/src/Sheets/Parser.php b/src/Sheets/Parser.php index 9e15510..8dbb285 100644 --- a/src/Sheets/Parser.php +++ b/src/Sheets/Parser.php @@ -223,7 +223,7 @@ protected function calculateDataSchema( $qty = 0; $pattern = $markerName; while (array_key_exists($pattern = $this->increment($pattern, true), $data)) { - $qty++; + $qty++; } $additionRows = max($additionRows, $qty); diff --git a/tests/GridServiceTest.php b/tests/GridServiceTest.php index 602dbfd..3375564 100644 --- a/tests/GridServiceTest.php +++ b/tests/GridServiceTest.php @@ -583,8 +583,7 @@ public function test_generate_statistic_without_headers_with_shift() */ protected function getDriver(): \AnourValar\Office\Drivers\GridInterface { - return new class implements \AnourValar\Office\Drivers\GridInterface - { + return new class () implements \AnourValar\Office\Drivers\GridInterface { public function create(): self { return $this; diff --git a/tests/SheetsParserTest.php b/tests/SheetsParserTest.php index 26ba8ff..e08c247 100644 --- a/tests/SheetsParserTest.php +++ b/tests/SheetsParserTest.php @@ -281,10 +281,14 @@ public function test_schema_not_scalar() ], 'data' => [ - 'foo' => function () {}, + 'foo' => function () { + }, 'baz' => new \DateTime('2022-11-16'), - 'test' => function () {}, - 'test2' => function () { throw new \LogicException('oops'); }, + 'test' => function () { + }, + 'test2' => function () { + throw new \LogicException('oops'); + }, ], ], ]; @@ -337,13 +341,14 @@ public function test_schema_not_scalar() 'copy_width' => [], ], - $this->service->schema([1 => ['A' => 'hello [world]']], ['world' => function () {}], [])->toArray() + $this->service->schema([1 => ['A' => 'hello [world]']], ['world' => function () { + }], [])->toArray() ); } - /** - * @return void - */ + /** + * @return void + */ public function test_schema_conditions1() { $data = [ @@ -403,9 +408,9 @@ public function test_schema_conditions1() } } - /** - * @return void - */ + /** + * @return void + */ public function test_schema_conditions2() { $data = [