Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 15, 2023
1 parent ea5639c commit 5c565ec
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 30 deletions.
11 changes: 11 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
])
->setFinder($finder);
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}
Expand Down
36 changes: 36 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<!-- @see https://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php -->
<ruleset name="PHPCS Rules">

<description>PHPCS ruleset</description>

<file>src</file>
<file>tests</file>

<!-- Show progress of the run -->
<arg value= "p"/>

<!-- Show sniff codes in all reports -->
<arg value= "s"/>

<!-- Our base rule: set to PSR12 -->
<rule ref="PSR12">
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore"/>
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceAfter"/>
<exclude name="PSR12.Traits.UseDeclaration.MultipleImport"/>
<exclude name="Generic.Files.LineLength.TooLong"/>
<exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose"/>
<exclude name="PSR12.Classes.OpeningBraceSpace.Found"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/>
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>tests/</exclude-pattern>
</rule>

<rule ref="Internal.NoCodeFound">
<exclude-pattern>tests/</exclude-pattern>
</rule>

</ruleset>
50 changes: 50 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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
15 changes: 8 additions & 7 deletions src/Drivers/PhpSpreadsheetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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.'),
};
}
}
10 changes: 5 additions & 5 deletions src/Drivers/ZipDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ZipDriver implements DocumentInterface, GridInterface
{
use \Anourvalar\Office\Traits\Parser;
use \AnourValar\Office\Traits\Parser;
use \AnourValar\Office\Traits\XFormat;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/GridService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<iterable> $data
* @param iterable|\Closure $data
* @param string $leftTopCorner
* @return \AnourValar\Office\Generated
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Mixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sheets/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions tests/GridServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 15 additions & 10 deletions tests/SheetsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
],
],
];
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -403,9 +408,9 @@ public function test_schema_conditions1()
}
}

/**
* @return void
*/
/**
* @return void
*/
public function test_schema_conditions2()
{
$data = [
Expand Down

0 comments on commit 5c565ec

Please sign in to comment.