Add phpstan until level 7

This commit is contained in:
Antoine Lamirault 2021-09-18 18:37:45 +02:00
parent 540667870a
commit efcf35be50
7 changed files with 67 additions and 11 deletions

View File

@ -82,7 +82,28 @@ This will print a diff of proposed code style changes. To apply these suggestion
vendor/bin/php-cs-fixer fix --config=.php_cs.dist
```
### Step 9: Send the pull request
You can also use
```
composer phpcs
```
### Step 9: Fix phpstan
Run the following command to analyse source code.
Make sure to fix all errors before submitting a pull request.
```
vendor/bin/phpstan analyse
```
You can also use
```
composer phpstan
```
### Step 10: Send the pull request
Send the pull request from your feature branch to us. Be sure to include a description that lets us know what work you did.

View File

@ -16,7 +16,7 @@ class StringHelper
/** @var bool Whether the code is running with PHP7 or older versions */
private $isRunningPhp7OrOlder;
/** @var array Locale info, used for number formatting */
/** @var array<string, mixed> Locale info, used for number formatting */
private $localeInfo;
/**
@ -93,13 +93,16 @@ class StringHelper
public function formatNumericValue($numericValue)
{
if ($this->isRunningPhp7OrOlder && is_float($numericValue)) {
return str_replace(
/** @var string $replaced */
$replaced = str_replace(
[$this->localeInfo['thousands_sep'], $this->localeInfo['decimal_point']],
['', '.'],
$numericValue
(string) $numericValue
);
return $replaced;
}
return $numericValue;
return (string) $numericValue;
}
}

View File

@ -11,7 +11,14 @@ use Box\Spout\Common\Entity\Style\Style;
*/
class PossiblyUpdatedStyle
{
/**
* @var Style
*/
private $style;
/**
* @var bool
*/
private $isUpdated;
public function __construct(Style $style, bool $isUpdated)

View File

@ -87,6 +87,7 @@ class SheetTest extends TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToContentFile = $resourcePath . '#content.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToContentFile);
$this->assertStringContainsString(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
@ -162,6 +163,7 @@ class SheetTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertStringContainsString("table:name=\"$expectedName\"", $xmlContents, $message);

View File

@ -284,19 +284,28 @@ class WriterTest extends TestCase
*/
public function testAddRowShouldSupportFloatValuesInDifferentLocale()
{
$previousLocale = \setlocale(LC_ALL, 0);
/** @var string[] $previousLocale */
$previousLocale = \setlocale(LC_ALL, '0');
try {
// Pick a supported locale whose decimal point is a comma.
// Installed locales differ from one system to another, so we can't pick
// a given locale.
$supportedLocales = explode("\n", shell_exec('locale -a'));
$commaDecimalPointSupported = false;
foreach ($supportedLocales as $supportedLocale) {
\setlocale(LC_ALL, $supportedLocale);
if (\localeconv()['decimal_point'] === ',') {
$commaDecimalPointSupported = true;
break;
}
}
if ($commaDecimalPointSupported === false) {
$this->markTestSkipped('System has no local which support comma decimal point');
}
$this->assertEquals(',', \localeconv()['decimal_point']);
$fileName = 'test_add_row_should_support_float_values_in_different_locale.xlsx';
@ -306,8 +315,8 @@ class WriterTest extends TestCase
$this->writeToODSFile($dataRows, $fileName);
$this->assertValueWasNotWrittenToSheet($fileName, 1, "1234,5");
$this->assertValueWasWrittenToSheet($fileName, 1, "1234.5");
$this->assertValueWasNotWrittenToSheet($fileName, 1, '1234,5');
$this->assertValueWasWrittenToSheet($fileName, 1, '1234.5');
} finally {
// reset locale
\setlocale(LC_ALL, $previousLocale);
@ -581,6 +590,7 @@ class WriterTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToContentFile = $resourcePath . '#content.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToContentFile);
$this->assertStringContainsString($value, $xmlContents, $message);

View File

@ -87,6 +87,7 @@ class SheetTest extends TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertStringContainsString(' state="hidden"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
@ -164,6 +165,7 @@ class SheetTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertStringContainsString("<sheet name=\"$expectedName\"", $xmlContents, $message);

View File

@ -399,7 +399,8 @@ class WriterTest extends TestCase
*/
public function testAddRowShouldSupportFloatValuesInDifferentLocale()
{
$previousLocale = \setlocale(LC_ALL, 0);
/** @var string[] $previousLocale */
$previousLocale = \setlocale(LC_ALL, '0');
$valueToWrite = 1234.5; // needs to be defined before changing the locale as PHP8 would expect 1234,5
try {
@ -407,12 +408,19 @@ class WriterTest extends TestCase
// Installed locales differ from one system to another, so we can't pick
// a given locale.
$supportedLocales = explode("\n", shell_exec('locale -a'));
$commaDecimalPointSupported = false;
foreach ($supportedLocales as $supportedLocale) {
\setlocale(LC_ALL, $supportedLocale);
if (\localeconv()['decimal_point'] === ',') {
$commaDecimalPointSupported = true;
break;
}
}
if ($commaDecimalPointSupported === false) {
$this->markTestSkipped('System has no local which support comma decimal point');
}
$this->assertEquals(',', \localeconv()['decimal_point']);
$fileName = 'test_add_row_should_support_float_values_in_different_locale.xlsx';
@ -422,8 +430,8 @@ class WriterTest extends TestCase
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
$this->assertInlineDataWasNotWrittenToSheet($fileName, 1, "1234,5");
$this->assertInlineDataWasWrittenToSheet($fileName, 1, "1234.5");
$this->assertInlineDataWasNotWrittenToSheet($fileName, 1, '1234,5');
$this->assertInlineDataWasWrittenToSheet($fileName, 1, '1234.5');
} finally {
// reset locale
\setlocale(LC_ALL, $previousLocale);
@ -643,6 +651,7 @@ class WriterTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetIndex . '.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToSheetFile);
$this->assertStringContainsString((string) $inlineData, $xmlContents, $message);
@ -659,6 +668,7 @@ class WriterTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToSheetFile = $resourcePath . '#xl/worksheets/sheet' . $sheetIndex . '.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToSheetFile);
$this->assertStringNotContainsString((string) $inlineData, $xmlContents, $message);
@ -674,6 +684,7 @@ class WriterTest extends TestCase
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToSharedStringsFile = $resourcePath . '#xl/sharedStrings.xml';
/** @var string $xmlContents */
$xmlContents = file_get_contents('zip://' . $pathToSharedStringsFile);
$this->assertStringContainsString($sharedString, $xmlContents, $message);