From 25d891f33273bbf93b7fc13008e4a7db521306e8 Mon Sep 17 00:00:00 2001 From: RasmuS2024 Date: Mon, 4 Nov 2024 20:23:23 +0500 Subject: [PATCH] modified files for mentor checks. --- Makefile | 4 +- composer.json | 13 ++--- composer.lock | 64 +++------------------- phpstan.neon | 1 - src/Differ.php | 3 +- src/Formatters.php | 2 +- {formatters => src/Formatters}/Json.php | 0 {formatters => src/Formatters}/Plain.php | 0 {formatters => src/Formatters}/Stylish.php | 0 src/Parsers.php | 26 +++++---- tests/DiffTest.php | 18 ++++++ tests/fixtures/ParseError.txt | 1 + 12 files changed, 48 insertions(+), 84 deletions(-) rename {formatters => src/Formatters}/Json.php (100%) rename {formatters => src/Formatters}/Plain.php (100%) rename {formatters => src/Formatters}/Stylish.php (100%) create mode 100644 tests/fixtures/ParseError.txt diff --git a/Makefile b/Makefile index b6449fb..ac7c52e 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ validate: composer validate lint: - composer exec --verbose phpcs -- --standard=PSR12 src bin tests formatters + composer exec --verbose phpcs -- --standard=PSR12 src bin tests composer exec -v phpstan analyse -- -c phpstan.neon --ansi test: @@ -21,4 +21,4 @@ test-coverage-text: XDEBUG_MODE=coverage composer exec --verbose phpunit tests -- --coverage-text test-coverage-html: - XDEBUG_MODE=coverage composer exec --verbose phpunit tests -- --coverage-html \ No newline at end of file + XDEBUG_MODE=coverage composer exec phpunit tests -- --coverage-html build/over \ No newline at end of file diff --git a/composer.json b/composer.json index 819e3f5..ec912cb 100644 --- a/composer.json +++ b/composer.json @@ -1,22 +1,18 @@ { "name": "hexlet/code", "description": "My 2 project on Hexlet - Gendiff", - "type": "project", + "type": "library", "license": "MIT", "autoload": { - "psr-4": { - "Hexlet\\Code\\": "src/" - }, "files": [ "src/Differ.php", "src/Parsers.php", "src/Formatters.php", - "formatters/Stylish.php", - "formatters/Plain.php", - "formatters/Json.php" + "src/Formatters/Stylish.php", + "src/Formatters/Plain.php", + "src/Formatters/Json.php" ] }, - "authors": [ { "name": "RasmuS2024", @@ -36,7 +32,6 @@ "squizlabs/php_codesniffer": "^3.10", "phpstan/phpstan": "1.10.24", "phpstan/extension-installer": "1.4.3", - "phpstan/phpstan-beberlei-assert": "^1.1", "phpstan/phpstan-strict-rules": "^1.5", "hexlet/phpstan-fp": "^2.0" }, diff --git a/composer.lock b/composer.lock index 6395cc3..3cd0f63 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ba14010428b18f39d515689469fae4b7", + "content-hash": "42669c66293ab2c4970ee9648281be89", "packages": [ { "name": "docopt/docopt", @@ -355,16 +355,16 @@ }, { "name": "symfony/yaml", - "version": "v6.4.12", + "version": "v6.4.13", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "762ee56b2649659380e0ef4d592d807bc17b7971" + "reference": "e99b4e94d124b29ee4cf3140e1b537d2dad8cec9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/762ee56b2649659380e0ef4d592d807bc17b7971", - "reference": "762ee56b2649659380e0ef4d592d807bc17b7971", + "url": "https://api.github.com/repos/symfony/yaml/zipball/e99b4e94d124b29ee4cf3140e1b537d2dad8cec9", + "reference": "e99b4e94d124b29ee4cf3140e1b537d2dad8cec9", "shasum": "" }, "require": { @@ -407,7 +407,7 @@ "description": "Loads and dumps YAML files", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/yaml/tree/v6.4.12" + "source": "https://github.com/symfony/yaml/tree/v6.4.13" }, "funding": [ { @@ -423,7 +423,7 @@ "type": "tidelift" } ], - "time": "2024-09-17T12:47:12+00:00" + "time": "2024-09-25T14:18:03+00:00" } ], "packages-dev": [ @@ -1016,56 +1016,6 @@ ], "time": "2023-07-05T12:32:13+00:00" }, - { - "name": "phpstan/phpstan-beberlei-assert", - "version": "1.1.2", - "source": { - "type": "git", - "url": "https://github.com/phpstan/phpstan-beberlei-assert.git", - "reference": "0918f2c71f93d44839b74fe3ba2682b2083a8729" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-beberlei-assert/zipball/0918f2c71f93d44839b74fe3ba2682b2083a8729", - "reference": "0918f2c71f93d44839b74fe3ba2682b2083a8729", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0", - "phpstan/phpstan": "^1.10" - }, - "require-dev": { - "beberlei/assert": "^3.3.0", - "nikic/php-parser": "^4.13.0", - "php-parallel-lint/php-parallel-lint": "^1.2", - "phpstan/phpstan-phpunit": "^1.0", - "phpstan/phpstan-strict-rules": "^1.0", - "phpunit/phpunit": "^9.5" - }, - "type": "phpstan-extension", - "extra": { - "phpstan": { - "includes": [ - "extension.neon" - ] - } - }, - "autoload": { - "psr-4": { - "PHPStan\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "PHPStan beberlei/assert extension", - "support": { - "issues": "https://github.com/phpstan/phpstan-beberlei-assert/issues", - "source": "https://github.com/phpstan/phpstan-beberlei-assert/tree/1.1.2" - }, - "time": "2023-09-04T12:13:01+00:00" - }, { "name": "phpstan/phpstan-strict-rules", "version": "1.5.1", diff --git a/phpstan.neon b/phpstan.neon index 108de64..34c24dd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,7 +2,6 @@ parameters: level: 8 paths: - src - - formatters - bin checkMissingIterableValueType: false diff --git a/src/Differ.php b/src/Differ.php index 7f4e836..4061c58 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -13,9 +13,8 @@ function genDiff(mixed $file1Path, mixed $file2Path, string $formatName = 'styli if ($data1 !== false && $data2 !== false) { $dataDiff = arraysDiffer($data1, $data2); return formatSelect($dataDiff, $formatName); - } else { - return ''; } + return "Parsing of file(s) error!\n"; } function getSortedKeys(array $json1, array $json2): array diff --git a/src/Formatters.php b/src/Formatters.php index 2ca6a3b..51a2f1f 100644 --- a/src/Formatters.php +++ b/src/Formatters.php @@ -12,7 +12,7 @@ function formatSelect(array $diffSource, string $formatName): string 'stylish' => stylish($diffSource), 'plain' => plain($diffSource), 'json' => json($diffSource), - default => '', + default => "Unknown format: \"{$formatName}\"", }; return $result; } diff --git a/formatters/Json.php b/src/Formatters/Json.php similarity index 100% rename from formatters/Json.php rename to src/Formatters/Json.php diff --git a/formatters/Plain.php b/src/Formatters/Plain.php similarity index 100% rename from formatters/Plain.php rename to src/Formatters/Plain.php diff --git a/formatters/Stylish.php b/src/Formatters/Stylish.php similarity index 100% rename from formatters/Stylish.php rename to src/Formatters/Stylish.php diff --git a/src/Parsers.php b/src/Parsers.php index 6637ee4..760d1b8 100644 --- a/src/Parsers.php +++ b/src/Parsers.php @@ -9,18 +9,20 @@ function fileParser(string $pathToFile): mixed $pathToFileElements = pathinfo($pathToFile); if (array_key_exists('extension', $pathToFileElements)) { $extension = $pathToFileElements['extension']; - $fileContent = file_get_contents($pathToFile); - if ($fileContent !== false) { - return match ($extension) { - 'json' => json_decode($fileContent, true), - 'yaml' => Yaml::parseFile($pathToFile), - 'yml' => Yaml::parseFile($pathToFile), - default => '', - }; - } else { - return false; + switch ($extension) { + case 'json': + $fileContent = file_get_contents($pathToFile); + if ($fileContent !== false) { + return json_decode($fileContent, true); + } + return false; + case 'yaml': + return Yaml::parseFile($pathToFile); + case 'yml': + return Yaml::parseFile($pathToFile); + default: + return false; } - } else { - return false; } + return false; } diff --git a/tests/DiffTest.php b/tests/DiffTest.php index 269a073..42ba6b5 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -35,6 +35,15 @@ public function testFilesDiffYaml(): void $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); } + public function testFilesDiffYamlWithJson(): void + { + $pathToFile1 = $this->getFixtureFullPath('File1.yaml'); + $pathToFile2 = $this->getFixtureFullPath('File2.json'); + $pathToFile3 = $this->getFixtureFullPath('Result.txt'); + $diffStringFromFiles = genDiff($pathToFile1, $pathToFile2); + $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); + } + public function testFilesDiffJsonToPlainFormat(): void { $pathToFile1 = $this->getFixtureFullPath('File1.json'); @@ -52,4 +61,13 @@ public function testFilesDiffJsonToJsonFormat(): void $diffStringFromFiles = genDiff($pathToFile1, $pathToFile2, 'json'); $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); } + + public function testFilesNotFound(): void + { + $pathToFile1 = $this->getFixtureFullPath('File.json'); + $pathToFile2 = $this->getFixtureFullPath('File5.json'); + $pathToFile3 = $this->getFixtureFullPath('ParseError.txt'); + $diffStringFromFiles = genDiff($pathToFile1, $pathToFile2); + $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); + } } diff --git a/tests/fixtures/ParseError.txt b/tests/fixtures/ParseError.txt new file mode 100644 index 0000000..e5ad361 --- /dev/null +++ b/tests/fixtures/ParseError.txt @@ -0,0 +1 @@ +Parsing of file(s) error!