Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid74 committed Jan 4, 2024
1 parent 4f78827 commit cac75e3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,20 +496,31 @@ public static function printVar($mixVar, string $sTitle = ''): void
*
* Заменяет строку в файле на другую строку и информирует, была ли выполнена замена
*
* @param string $filename The name of the file in which the replacement is being performed.
* @param string $searchString The search string.
* @param string $replaceString The replacement string.
* @param string|null $filename The name of the file in which the replacement is being performed.
* @param string|null $searchString The search string.
* @param string|null $replaceString The replacement string.
* @param string $enc The encoding.
*
* @return array Returns an array where the first element is true if the replacement is completed or no replacement is required,
* false if an error occurred, and the second element is details.
*
* Возвращает массив, где первый элемент - true, если замена выполнена или замена не требуется,
* false если произошла ошибка, а второй элемент - подробности.
*/
public function replaceStringInFile(string $filename, string $searchString, string $replaceString, $enc = static::$encoding): array
public function replaceStringInFile(?string $filename, ?string $searchString, ?string $replaceString = '', $enc = static::$encoding): array
{
try {
// Check if the file exists and if it is available for reading/writing
// Checking an empty file name
if (empty($filename)) {
throw new \RuntimeException("Передано пустое имя файла");
}

// Checking an empty searchString
if (empty($searchString)) {
throw new \RuntimeException("Передана пустая строка поиска");
}

// Checking if the file exists and if it is available for reading/writing
if (!\is_readable($filename) || !\is_writable($filename)) {
throw new \RuntimeException("Файл не доступен для чтения/записи: {$filename}");
}
Expand All @@ -520,7 +531,7 @@ public function replaceStringInFile(string $filename, string $searchString, stri
throw new \RuntimeException("Не удалось прочитать файл: {$filename}");
}

// Check if the required line is present in the file
// Checking if the required line is present in the file
if (\mb_strpos($fileContent, $searchString, 0, $enc) === false) {
// The required string was not found, no replacement is required
return [true, 'Искомая строка не найдена, замена не требуется'];
Expand Down

0 comments on commit cac75e3

Please sign in to comment.