Skip to content

Commit e831636

Browse files
authored
Merge pull request #4439 from oleibman/stan1001
Phpstan Tweaks
2 parents 5f6d741 + c2b5cb3 commit e831636

36 files changed

+151
-55
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787

8888
# This is non-ideal because it only checks for the last commit of the PR, not all of them, but better than nothing
8989
- name: Check PHPDoc types
90-
run: ./bin/check-phpdoc-types
90+
run: ./bin/check-phpdoc-types.php
9191

9292
php-cs-fixer:
9393
runs-on: ubuntu-latest

.php-cs-fixer.dist.php

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

33
$finder = PhpCsFixer\Finder::create()
4-
->exclude('vendor')
4+
->exclude(['vendor', 'docs', '.git', '.github'])
55
->notPath('src/PhpSpreadsheet/Writer/ZipStream3.php')
6-
->name('/(\.php|^generate-document|^generate-locales|^check-phpdoc-types)$/')
76
->in(__DIR__);
87

98
$config = new PhpCsFixer\Config();

.phpcs.xml.dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
<file>src</file>
77
<file>tests</file>
88
<file>infra</file>
9-
<file>bin/generate-document</file>
10-
<file>bin/generate-locales</file>
11-
<file>bin/check-phpdoc-types</file>
12-
13-
<exclude-pattern>samples/Header.php</exclude-pattern>
14-
<exclude-pattern>*/tests/Core/*/*Test\.(inc|css|js)$</exclude-pattern>
9+
<file>bin</file>
1510

1611
<arg name="report-width" value="200"/>
1712
<arg name="parallel" value="80"/>

bin/check-phpdoc-types renamed to bin/check-phpdoc-types.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* This will help us slowly migrate away from PHPDoc typing to PHP native typing.
1010
*/
11-
function checkPhpDocTypes(): void
11+
function checkPhpDocTypes(): int
1212
{
1313
$content = shell_exec('git diff --cached') ?? shell_exec('git diff') ?? shell_exec('git show HEAD');
1414
preg_match_all('~^\+ +\* @(param|var) (mixed|string|int|float|bool|null|array|\?|\|)+( \$\w+)?$~m', "$content", $parameters);
@@ -19,11 +19,16 @@ function checkPhpDocTypes(): void
1919
...$returns[0],
2020
];
2121

22-
if ($errors) {
22+
if (!empty($errors)) {
2323
echo 'PHP native types must be used instead of PHPDoc types (without comments), for the following lines:' . PHP_EOL . PHP_EOL;
2424
echo implode(PHP_EOL, $errors) . PHP_EOL;
25-
exit(1);
25+
26+
return 1;
2627
}
28+
29+
return 0;
2730
}
2831

29-
checkPhpDocTypes();
32+
if (checkPhpDocTypes()) {
33+
exit(1);
34+
}

bin/generate-document renamed to bin/generate-document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
55
use PhpOffice\PhpSpreadsheetInfra\DocumentGenerator;
66

7-
require_once 'vendor/autoload.php';
7+
require_once __DIR__ . '/..' . '/vendor/autoload.php';
88

99
$phpSpreadsheetFunctions = Calculation::getFunctions();
1010
ksort($phpSpreadsheetFunctions);

bin/generate-locales renamed to bin/generate-locales.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
55
use PhpOffice\PhpSpreadsheetInfra\LocaleGenerator;
66

7-
require_once 'vendor/autoload.php';
7+
require_once __DIR__ . '/..' . '/vendor/autoload.php';
88

99
$phpSpreadsheetFunctions = Calculation::getFunctions();
1010

bin/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if [ "$files" != "" ]; then
2424
fi
2525

2626
# Check PHPDoc types
27-
./bin/check-phpdoc-types
27+
./bin/check-phpdoc-types.php
2828
if [ $? -ne 0 ]; then
2929
pass=false
3030
fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
],
4747
"scripts": {
4848
"check": [
49-
"php ./bin/check-phpdoc-types",
49+
"php bin/check-phpdoc-types.php",
5050
"phpcs samples/ src/ tests/ --report=checkstyle",
5151
"phpcs samples/ src/ tests/ --standard=PHPCompatibility --runtime-set testVersion 8.0- --exclude=PHPCompatibility.Variables.ForbiddenThisUseContexts -n",
5252
"php-cs-fixer fix --ansi --dry-run --diff",

infra/DocumentGenerator.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ public static function generateFunctionListByCategory(array $phpSpreadsheetFunct
3333
return $result;
3434
}
3535

36+
/** @return array<string, string> */
3637
private static function getCategories(): array
3738
{
38-
return (new ReflectionClass(Category::class))->getConstants();
39+
/** @var array<string, string> */
40+
$x = (new ReflectionClass(Category::class))->getConstants();
41+
42+
return $x;
3943
}
4044

45+
/**
46+
* @param int[] $lengths
47+
* @param null|array<int, int|string> $values
48+
*/
4149
private static function tableRow(array $lengths, ?array $values = null): string
4250
{
4351
$result = '';
@@ -46,7 +54,7 @@ private static function tableRow(array $lengths, ?array $values = null): string
4654
if ($i > 0) {
4755
$result .= '|' . $pad;
4856
}
49-
$result .= str_pad($value ?? '', $length, $pad);
57+
$result .= str_pad("$value", $length ?? 0, $pad);
5058
}
5159

5260
return rtrim($result, ' ');
@@ -71,7 +79,7 @@ private static function getPhpSpreadsheetFunctionText(mixed $functionCall): stri
7179
}
7280

7381
/**
74-
* @param array[] $phpSpreadsheetFunctions
82+
* @param array<string, array<string, int|string>> $phpSpreadsheetFunctions
7583
*/
7684
public static function generateFunctionListByName(array $phpSpreadsheetFunctions): string
7785
{

infra/LocaleGenerator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LocaleGenerator
3030

3131
protected string $translationBaseFolder;
3232

33+
/** @var string[] */
3334
protected array $phpSpreadsheetFunctions;
3435

3536
protected Spreadsheet $translationSpreadsheet;
@@ -38,16 +39,23 @@ class LocaleGenerator
3839

3940
protected Worksheet $localeTranslations;
4041

42+
/** @var string[] */
4143
protected array $localeLanguageMap = [];
4244

45+
/** @var array<string, int> */
4346
protected array $errorCodeMap = [];
4447

4548
private Worksheet $functionNameTranslations;
4649

50+
/** @var string[] */
4751
protected array $functionNameLanguageMap = [];
4852

53+
/** @var array<string, int|string> */
4954
protected array $functionNameMap = [];
5055

56+
/**
57+
* @param string[] $phpSpreadsheetFunctions
58+
*/
5159
public function __construct(
5260
string $translationBaseFolder,
5361
string $translationSpreadsheetName,
@@ -268,6 +276,7 @@ protected function getTranslationSheet(string $sheetName): Worksheet
268276
return $worksheet;
269277
}
270278

279+
/** @return string[] */
271280
protected function mapLanguageColumns(Worksheet $translationWorksheet): array
272281
{
273282
$sheetName = $translationWorksheet->getTitle();
@@ -282,7 +291,7 @@ protected function mapLanguageColumns(Worksheet $translationWorksheet): array
282291
$cells->setIterateOnlyExistingCells(true);
283292
foreach ($cells as $cell) {
284293
if ($this->localeCanBeSupported($translationWorksheet, $cell)) {
285-
$languageNameMap[$cell->getColumn()] = $cell->getValue();
294+
$languageNameMap[$cell->getColumn()] = $cell->getValueString();
286295
$this->log($cell->getColumn() . ' -> ' . $cell->getValueString());
287296
}
288297
}

0 commit comments

Comments
 (0)