@@ -12,6 +12,14 @@ public function __construct(
1212 ) {
1313 }
1414
15+ /**
16+ * @param string $filename
17+ * @param string $keyword
18+ * @param string $column
19+ * @param string|null $format
20+ * @return bool|array<string,string>
21+ * @throws FileHandlerException
22+ */
1523 public function searchInCsvFile (
1624 string $ filename ,
1725 string $ keyword ,
@@ -32,13 +40,18 @@ public function searchInCsvFile(
3240 }
3341
3442
35- public function toJson (string $ filename ): string
43+ public function toJson (string $ filename ): string | false
3644 {
3745 $ data = $ this ->toArray ($ filename );
3846
3947 return json_encode ($ data );
4048 }
4149
50+ /**
51+ * @param string $filename
52+ * @return array<int,array<string,string>>
53+ * @throws FileHandlerException
54+ */
4255 public function toArray (string $ filename ): array
4356 {
4457 if (!file_exists ($ filename )) {
@@ -61,6 +74,9 @@ public function findAndReplaceInCsv(
6174 }
6275
6376 $ tempFilePath = $ this ->tempFileHandler ->createTempFileWithHeaders ($ headers );
77+ if (!$ tempFilePath ) {
78+ return false ;
79+ }
6480
6581
6682 try {
@@ -86,22 +102,24 @@ public function findAndReplaceInCsv(
86102 return true ;
87103 }
88104
105+ /**
106+ * @param mixed $file
107+ * @return array<string>|false
108+ */
109+
89110 private function extractHeader (mixed $ file ): array |false
90111 {
91112 $ headers = [];
92113 if (is_resource ($ file )) {
93114 $ headers = fgetcsv ($ file );
94115 }
95116 if (is_string ($ file )) {
96- if (!file_exists ($ file )) {
117+ $ file = fopen ($ file , 'r ' );
118+ if (!$ file ) {
97119 return false ;
98120 }
99- try {
100- $ file = fopen ($ file , 'r ' );
101- $ headers = fgetcsv ($ file );
102- } finally {
103- fclose ($ file );
104- }
121+ $ headers = fgetcsv ($ file );
122+ fclose ($ file );
105123 }
106124
107125 if (!$ headers ) {
@@ -116,6 +134,10 @@ private function extractHeader(mixed $file): array|false
116134 return $ headers ;
117135 }
118136
137+ /**
138+ * @param array<string> $row
139+ * @return bool
140+ */
119141 private function isValidCsvFileFormat (array $ row ): bool
120142 {
121143 if (count ($ row ) <= 1 ) {
@@ -124,10 +146,21 @@ private function isValidCsvFileFormat(array $row): bool
124146 return true ;
125147 }
126148
149+ /**
150+ * @param string $filename
151+ * @return Generator
152+ * @throws FileHandlerException
153+ */
127154 private function getRows (string $ filename ): Generator
128155 {
129156 $ csvFile = fopen ($ filename , 'r ' );
157+ if (!$ csvFile ) {
158+ throw new FileHandlerException ('file not found ' );
159+ }
130160 $ headers = $ this ->extractHeader ($ csvFile );
161+ if (!is_array ($ headers )) {
162+ throw new FileHandlerException ('could not extract header ' );
163+ }
131164
132165
133166 $ isEmptyFile = true ;
@@ -151,6 +184,12 @@ private function getRows(string $filename): Generator
151184 }
152185 }
153186
187+ /**
188+ * @param array<string> $row
189+ * @param string $keyword
190+ * @param string $replace
191+ * @return int
192+ */
154193 private function replaceKeywordInRow (array &$ row , string $ keyword , string $ replace ): int
155194 {
156195 $ count = 0 ;
@@ -164,6 +203,13 @@ private function replaceKeywordInRow(array &$row, string $keyword, string $repla
164203 return $ count ;
165204 }
166205
206+ /**
207+ * @param array<string> $row
208+ * @param string $column
209+ * @param string $keyword
210+ * @param string $replace
211+ * @return int
212+ */
167213 private function replaceKeywordInColumn (array &$ row , string $ column , string $ keyword , string $ replace ): int
168214 {
169215 $ count = 0 ;
0 commit comments