From 630845e0bd3e2da6d62e8cdf8d9f422933732b20 Mon Sep 17 00:00:00 2001 From: mace Date: Thu, 3 Aug 2023 21:02:27 +0200 Subject: [PATCH] fix missing final keyword, keep file open for csv writer --- .gitignore | 1 - README.md | 6 ------ composer.lock | 20 ++++++++++++++++++++ src/Csv/CsvWriter.php | 26 ++++++++++++++++++++------ src/CsvCalculationService.php | 2 +- src/MathOperationFactory.php | 2 +- 6 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index 9c01165..7151afe 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,4 @@ log.txt result.csv .run -*.lock .php-cs-fixer.cache diff --git a/README.md b/README.md index c1dace2..83f66c8 100644 --- a/README.md +++ b/README.md @@ -54,14 +54,8 @@ _numbers are 3 and 0 are wrong, is not allowed_
## Task You need to refactor code and write it on proper way. Just do your best: update/delete/add code as you wish. -After finishing - please push your code in your github/bitbucket account, and send me link back. - ### Requirements * After refactoring code shoud work * Code should work on PHP8.0+ * As file source example please use test.csv - -### Result -Please put result of your work in your Github or Bitbucket account, and send link back. - diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..d26d95e --- /dev/null +++ b/composer.lock @@ -0,0 +1,20 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "63e7cd6af94fff1be77b03d112d0b91c", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=8.1" + }, + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/src/Csv/CsvWriter.php b/src/Csv/CsvWriter.php index aaef145..8f9089f 100644 --- a/src/Csv/CsvWriter.php +++ b/src/Csv/CsvWriter.php @@ -6,23 +6,37 @@ namespace App\Csv; use RuntimeException; -class CsvWriter +final class CsvWriter { private string $separator = ';'; + /** + * @var false|resource + */ + private $fileHandle; + public function __construct(private readonly string $filePath) { file_put_contents($this->filePath, ''); } + /** + * @param array $data + */ public function addLine(array $data): void { - $handle = fopen($this->filePath, 'ab'); - if (!$handle) { - throw new RuntimeException('Could not open file.'); + if (!$this->fileHandle) { + $this->fileHandle = fopen($this->filePath, 'ab'); + if (!$this->fileHandle) { + throw new RuntimeException('Could not open file.'); + } } - fputcsv($handle, $data, $this->separator); - fclose($handle); + fputcsv($this->fileHandle, $data, $this->separator); + } + + public function __destruct() + { + fclose($this->fileHandle); } } diff --git a/src/CsvCalculationService.php b/src/CsvCalculationService.php index 60bc4f5..df233c2 100644 --- a/src/CsvCalculationService.php +++ b/src/CsvCalculationService.php @@ -10,7 +10,7 @@ use App\Logger\LoggerInterface; use App\Operations\Exceptions\InvalidResultException; use App\Operations\MathOperationInterface; -class CsvCalculationService +private class CsvCalculationService { public function __construct( private readonly CsvReader $csvReader, diff --git a/src/MathOperationFactory.php b/src/MathOperationFactory.php index cfb241e..20333ef 100644 --- a/src/MathOperationFactory.php +++ b/src/MathOperationFactory.php @@ -12,7 +12,7 @@ use App\Operations\Subtract; use App\Operations\Multiply; use App\Options\CliOption; -class MathOperationFactory +final class MathOperationFactory { public static function getOperationFromCliOption(CliOption $cliOption): MathOperationInterface {