@@ -69,14 +69,6 @@ class Csv extends BaseReader
6969 */
7070 private ?string $ escapeCharacter = null ;
7171
72- /**
73- * The character that will be supplied to fgetcsv
74- * when escapeCharacter is null.
75- * It is anticipated that it will conditionally be set
76- * to null-string for Php9 and above.
77- */
78- private static string $ defaultEscapeCharacter = PHP_VERSION_ID < 90000 ? '\\' : '' ;
79-
8072 /**
8173 * Callback for setting defaults in construction.
8274 *
@@ -200,7 +192,7 @@ protected function inferSeparator(): void
200192 return ;
201193 }
202194
203- $ inferenceEngine = new Delimiter ($ this ->fileHandle , $ this ->escapeCharacter ?? self :: $ defaultEscapeCharacter , $ this ->enclosure );
195+ $ inferenceEngine = new Delimiter ($ this ->fileHandle , $ this ->getEscapeCharacter () , $ this ->enclosure );
204196
205197 // If number of lines is 0, nothing to infer : fall back to the default
206198 if ($ inferenceEngine ->linesCounted () === 0 ) {
@@ -323,10 +315,10 @@ public function setTestAutoDetect(bool $value): self
323315 return $ this ;
324316 }
325317
326- private function setAutoDetect (?string $ value ): ?string
318+ private function setAutoDetect (?string $ value, int $ version = PHP_VERSION_ID ): ?string
327319 {
328320 $ retVal = null ;
329- if ($ value !== null && $ this ->testAutodetect && PHP_VERSION_ID < 90000 ) {
321+ if ($ value !== null && $ this ->testAutodetect && $ version < 90000 ) {
330322 $ retVal2 = @ini_set ('auto_detect_line_endings ' , $ value );
331323 if (is_string ($ retVal2 )) {
332324 $ retVal = $ retVal2 ;
@@ -566,9 +558,9 @@ public function getContiguous(): bool
566558 * Not yet ready to mark deprecated in order to give users
567559 * a migration path.
568560 */
569- public function setEscapeCharacter (string $ escapeCharacter ): self
561+ public function setEscapeCharacter (string $ escapeCharacter, int $ version = PHP_VERSION_ID ): self
570562 {
571- if (PHP_VERSION_ID >= 90000 && $ escapeCharacter !== '' ) {
563+ if ($ version >= 90000 && $ escapeCharacter !== '' ) {
572564 throw new ReaderException ('Escape character must be null string for Php9+ ' );
573565 }
574566
@@ -577,9 +569,9 @@ public function setEscapeCharacter(string $escapeCharacter): self
577569 return $ this ;
578570 }
579571
580- public function getEscapeCharacter (): string
572+ public function getEscapeCharacter (int $ version = PHP_VERSION_ID ): string
581573 {
582- return $ this ->escapeCharacter ?? self ::$ defaultEscapeCharacter ;
574+ return $ this ->escapeCharacter ?? self ::getDefaultEscapeCharacter ( $ version ) ;
583575 }
584576
585577 /**
@@ -705,10 +697,11 @@ private static function getCsv(
705697 ?int $ length = null ,
706698 string $ separator = ', ' ,
707699 string $ enclosure = '" ' ,
708- ?string $ escape = null
700+ ?string $ escape = null ,
701+ int $ version = PHP_VERSION_ID
709702 ): array |false {
710- $ escape = $ escape ?? self ::$ defaultEscapeCharacter ;
711- if (PHP_VERSION_ID >= 80400 && $ escape !== '' ) {
703+ $ escape = $ escape ?? self ::getDefaultEscapeCharacter () ;
704+ if ($ version >= 80400 && $ escape !== '' ) {
712705 return @fgetcsv ($ stream , $ length , $ separator , $ enclosure , $ escape );
713706 }
714707
@@ -720,10 +713,11 @@ public static function affectedByPhp9(
720713 string $ inputEncoding = 'UTF-8 ' ,
721714 ?string $ delimiter = null ,
722715 string $ enclosure = '" ' ,
723- string $ escapeCharacter = '\\'
716+ string $ escapeCharacter = '\\' ,
717+ int $ version = PHP_VERSION_ID
724718 ): bool {
725- if (PHP_VERSION_ID < 70400 || PHP_VERSION_ID >= 90000 ) {
726- throw new ReaderException ('Function valid only for Php7.4 or Php8 ' ); // @codeCoverageIgnore
719+ if ($ version < 70400 || $ version >= 90000 ) {
720+ throw new ReaderException ('Function valid only for Php7.4 or Php8 ' );
727721 }
728722 $ reader1 = new self ();
729723 $ reader1 ->setInputEncoding ($ inputEncoding )
@@ -749,4 +743,15 @@ public static function affectedByPhp9(
749743
750744 return $ array1 !== $ array2 ;
751745 }
746+
747+ /**
748+ * The character that will be supplied to fgetcsv
749+ * when escapeCharacter is null.
750+ * It is anticipated that it will conditionally be set
751+ * to null-string for Php9 and above.
752+ */
753+ private static function getDefaultEscapeCharacter (int $ version = PHP_VERSION_ID ): string
754+ {
755+ return $ version < 90000 ? '\\' : '' ;
756+ }
752757}
0 commit comments