1515
1616use 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+ */
1838class 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 ));
0 commit comments