Skip to content

Commit 576bdad

Browse files
committed
Add PHPStan
1 parent 767219c commit 576bdad

File tree

8 files changed

+64
-46
lines changed

8 files changed

+64
-46
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ jobs:
101101
if: matrix.coding-standards != true
102102
run: composer remove friendsofphp/php-cs-fixer --dev --no-update
103103

104-
- name: Remove vimeo/psalm
104+
- name: Remove phpstan/phpstan
105105
if: matrix.static-analysis != true
106-
run: composer remove vimeo/psalm --dev --no-update
106+
run: composer remove phpstan/phpstan --dev --no-update
107107

108108
- name: Configure Symfony Flex
109109
if: matrix.symfony-version
@@ -130,6 +130,6 @@ jobs:
130130
if: matrix.coding-standards
131131
run: cat LICENSE |grep -E "\(c\) ([0-9]+\-)*`date +%Y`"
132132

133-
- name: Run Psalm
133+
- name: Run PHPStan
134134
if: matrix.static-analysis
135-
run: php vendor/bin/psalm --stats --output-format=github
135+
run: php vendor/bin/phpstan --no-progress --error-format=github

UPGRADE-2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
* Remove deprecated `unix2dos_path` option
44
* The default value of the `escape` option is now an empty string (previously `\`), to ensure compliance with RFC 4180
5+
* Update signature : `public function write($data): void` to `public function write(array $data): void`

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323
"require-dev": {
2424
"friendsofphp/php-cs-fixer": "^3.0",
25-
"phpunit/phpunit": "^9.0",
26-
"vimeo/psalm": "^5"
25+
"phpstan/phpstan": "^2.1",
26+
"phpunit/phpunit": "^9.0"
2727
},
2828
"config": {
2929
"sort-packages": true

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#Parameter \#3 \$options of class Ecommit\\CsvTableGenerator\\Csv constructor expects array.+array<string, mixed> given#'
5+
path: tests/

phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
4+
5+
parameters:
6+
level: max
7+
paths:
8+
- src
9+
- tests

psalm.xml

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Csv.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,33 @@
1515

1616
use Symfony\Component\OptionsResolver\OptionsResolver;
1717

18+
/**
19+
* @phpstan-type Options array{
20+
* header?: ?array<?string>,
21+
* max_lines?: ?int,
22+
* delimiter?: string,
23+
* enclosure?: string,
24+
* eol?: self::EOL_*,
25+
* escape?: string,
26+
* add_utf8_bom?: bool,
27+
* }
28+
* @phpstan-type ResolvedOptions array{
29+
* header: ?array<?string>,
30+
* max_lines: ?int,
31+
* delimiter: string,
32+
* enclosure: string,
33+
* eol: self::EOL_*,
34+
* escape: string,
35+
* add_utf8_bom: bool,
36+
* }
37+
*/
1838
class Csv
1939
{
2040
public const EOL_LF = 'LF';
2141
public const EOL_CRLF = 'CR+LF';
2242

2343
/**
24-
* @var resource|false|null
25-
*
26-
* @psalm-var resource|closed-resource|false|null
44+
* @var resource|closed-resource|false|null
2745
*/
2846
protected mixed $handle = null;
2947

@@ -32,7 +50,7 @@ class Csv
3250
protected string $currentPathname;
3351

3452
/**
35-
* @var array<string, string>|null
53+
* @var array<?string>|null
3654
*/
3755
protected ?array $header;
3856

@@ -49,9 +67,9 @@ class Csv
4967
/**
5068
* Constructor.
5169
*
52-
* @param string $pathDir Path folder
53-
* @param string $filename Filename (without path folder and extension)
54-
* @param array $options See README.md
70+
* @param string $pathDir Path folder
71+
* @param string $filename Filename (without path folder and extension)
72+
* @param Options $options See README.md
5573
*/
5674
public function __construct(string $pathDir, string $filename, array $options = [])
5775
{
@@ -73,6 +91,7 @@ public function __construct(string $pathDir, string $filename, array $options =
7391
$resolver->setAllowedTypes('escape', 'string');
7492
$resolver->setAllowedTypes('add_utf8_bom', 'bool');
7593
$this->configureOptions($resolver);
94+
/** @var ResolvedOptions $options */
7695
$options = $resolver->resolve($options);
7796

7897
// Test folder
@@ -153,22 +172,20 @@ protected function newFile(): void
153172
/**
154173
* Add line in CSV file.
155174
*
156-
* @param array $data
175+
* @param array<?scalar> $data
157176
*/
158-
public function write($data): void
177+
public function write(array $data): void
159178
{
160-
if (!\is_resource($this->handle)) {
161-
throw new \Exception(\sprintf('Handle does not exist. File %s', $this->filename));
162-
}
163-
164179
// New file
165180
if (null !== $this->maxLines && $this->maxLines == $this->lines) {
166181
$this->newFile();
167182
}
168183

169184
// Write
170185
$eol = (self::EOL_CRLF === $this->eol) ? "\r\n" : "\n";
171-
/** @psalm-suppress TooManyArguments */
186+
if (!\is_resource($this->handle)) {
187+
throw new \Exception(\sprintf('Handle does not exist. File %s', $this->filename));
188+
}
172189
$result = fputcsv($this->handle, $data, $this->delimiter, $this->enclosure, $this->escape, $eol);
173190
if (false === $result) {
174191
throw new \Exception(\sprintf('Error during the writing in %s file', $this->filename));

tests/CsvTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class CsvTest extends TestCase
2020
{
21-
protected $path;
21+
protected string $path;
2222

2323
protected function setUp(): void
2424
{
@@ -39,9 +39,12 @@ protected function deleteDir(): void
3939
}
4040

4141
$files = glob($this->path.'/*');
42-
foreach ($files as $file) {
43-
is_dir($file) ? $this->deleteDir($file) : unlink($file);
42+
if ($files) {
43+
foreach ($files as $file) {
44+
is_dir($file) ? $this->deleteDir() : unlink($file);
45+
}
4446
}
47+
4548
rmdir($this->path);
4649
}
4750

@@ -355,11 +358,17 @@ public function testGetCurrentPathnameSecondFile(Csv $csv): void
355358
$csv->close();
356359
}
357360

361+
/**
362+
* @param array<string, mixed> $options
363+
*/
358364
protected function createCsv(array $options = []): Csv
359365
{
360366
return new Csv($this->path, 'my-csv', $options);
361367
}
362368

369+
/**
370+
* @param array<?scalar> $expectedRows
371+
*/
363372
protected function assertCsvFile(string $filename, array $expectedRows, bool $useCrlf = false, string $beforeContent = ''): void
364373
{
365374
$this->assertFileExists($this->path.'/'.$filename, 'File not found');

0 commit comments

Comments
 (0)