Skip to content

Commit

Permalink
modified files for mentor checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmuS2024 committed Nov 4, 2024
1 parent 4eac5b1 commit 25d891f
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 84 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
XDEBUG_MODE=coverage composer exec phpunit tests -- --coverage-html build/over
13 changes: 4 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
64 changes: 7 additions & 57 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ parameters:
level: 8
paths:
- src
- formatters
- bin

checkMissingIterableValueType: false
Expand Down
3 changes: 1 addition & 2 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 14 additions & 12 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 18 additions & 0 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/fixtures/ParseError.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parsing of file(s) error!

0 comments on commit 25d891f

Please sign in to comment.