Skip to content

Commit

Permalink
Merge pull request #32 from Aeliot-Tm/add_transform_linter
Browse files Browse the repository at this point in the history
Add transform linter
  • Loading branch information
Aeliot-Tm authored Oct 12, 2021
2 parents b936abb + 6d4169e commit 5aa520c
Show file tree
Hide file tree
Showing 31 changed files with 410 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.idea

/report/
!/report/**/**/.gitkeep
/translations/*
!/translations/.gitkeep
/vendor
.phpunit.result.cache
composer.lock
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
CHANGELOG
=========

2.5.0
-----
* Add linter "file_transformed" for check if file structure is normalised for all YAML files.
* Updated parameter data type declaration.
* Updated phpDocs.
* Add ability to call commands in the isolated dev environment (without installing into application).
* Add ability of services testing.

2.4.0
-----
* Implement usage of Google Translate API to translate missed keys.

2.3.2
-----
* Fix getting of project directory path for Symfony >=5.0
* Fix getting of project directory path for Symfony >=5.0.

2.3.1
-----
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"symfony/filesystem": ">=3.4",
"symfony/finder": ">=3.4",
"symfony/http-kernel": ">=3.4",
"symfony/translation": ">=3.4",
"symfony/translation-contracts": "^v2.0.0",
"symfony/yaml": ">=3.4"
},
Expand All @@ -33,7 +32,9 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"symfony/framework-bundle": ">=3.4",
"symfony/translation": "^5.3"
},
"scripts": {
"test": "vendor/bin/phpunit"
Expand Down
5 changes: 5 additions & 0 deletions docs/lint/lint_yaml_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ The order of passed keys has no matter.
- **keys_duplicated** - then check if project has duplicated keys in each used locale of each domain.
- **keys_missed** - then check if key mentioned in any locale of domain presented in all of them.

#### Extend linters

- **file_transformed** - then check if translation files transformed (has normalised structure).
- **key_pattern** - then check if translation keys match configured pattern.


### Examples of reports

Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
>
<php>
<ini name="error_reporting" value="-1" />
<ini name="memory_limit" value="-1" />
<server name="KERNEL_CLASS" value="Aeliot\Bundle\TransMaintain\Test\Kernel" />
</php>

<testsuites>
Expand Down
21 changes: 20 additions & 1 deletion src/Command/LintYamlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@
final class LintYamlCommand extends Command
{
private LinterRegistry $linterRegistry;
private ?string $yamlKeyPattern;

public function __construct(LinterRegistry $linterRegistry)
public function __construct(LinterRegistry $linterRegistry, ?string $yamlKeyPattern)
{
parent::__construct('aeliot_trans_maintain:lint:yaml');

$this->linterRegistry = $linterRegistry;
$this->yamlKeyPattern = $yamlKeyPattern;
}

public function getHelp(): string
{
$presets = implode(', ', $this->linterRegistry->getExistingPresets());
$help = "There are available presets: $presets.\n";

$lintersKeys = implode(', ', $this->linterRegistry->getRegisteredLintersKeys());
$help .= "There are available linters: $lintersKeys.\n";

if ($this->yamlKeyPattern) {
$help .= "\nThere is configured pattern for the checking of translation keys: $this->yamlKeyPattern\n";
} else {
$help .= "\nKey pattern is not configured. You have not to use such linter.\n";
}

return $help;
}

protected function configure(): void
Expand Down
4 changes: 3 additions & 1 deletion src/Command/SortFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ protected function configure(): void
$this->addArgument('output', InputArgument::OPTIONAL, 'Output file path');
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$pathIn = (string) $input->getArgument('file');
$yaml = $this->fileManipulator->parse($pathIn);
$yaml = $this->keySorter->transform($yaml);
$pathOut = $input->getArgument('output') ?: $pathIn;
$this->fileManipulator->dump($pathOut, $yaml);

return 0;
}
}
4 changes: 3 additions & 1 deletion src/Command/TransformYamlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ protected function configure(): void
$this->addArgument('output', InputArgument::OPTIONAL, 'Output file path. Optional. Incoming file will be updated if the argument omitted.');
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$pathIn = (string) $input->getArgument('file');
$pathOut = $input->getArgument('output') ?: $pathIn;

$this->fileManager->update($pathIn, $pathOut);

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function process(ContainerBuilder $container): void
}

/**
* @return array<string>
* @return array<int,string>
*/
private function getDirectories(ContainerBuilder $container): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Model/FilesMissedLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class FilesMissedLine implements ReportLineInterface
{
private string $domain;
/**
* @var array<string>
* @var array<int,string>
*/
private array $omittedLanguages;

Expand All @@ -19,15 +19,15 @@ public function __construct(string $domain, array $omittedLanguages)
}

/**
* @var array<string>
* @return array<int,string>
*/
public static function getHeaders(): array
{
return ['domain', 'omitted_languages'];
}

/**
* @var array<string>
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
35 changes: 35 additions & 0 deletions src/Model/FilesTransformedLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Aeliot\Bundle\TransMaintain\Model;

final class FilesTransformedLine implements ReportLineInterface
{
private string $domain;
private string $file;
private string $locale;

public function __construct(string $domain, string $locale, string $file)
{
$this->domain = $domain;
$this->file = $file;
$this->locale = $locale;
}

/**
* @return array<int,string>
*/
public static function getHeaders(): array
{
return ['domain', 'locale', 'file'];
}

/**
* @return array<string,string>
*/
public function jsonSerialize(): array
{
return ['domain' => $this->domain, 'locale' => $this->locale, 'file' => $this->file, ];
}
}
4 changes: 2 additions & 2 deletions src/Model/KeysDuplicatedLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public function __construct(string $domain, string $locale, string $languageId)
}

/**
* @var array<string>
* @return array<int,string>
*/
public static function getHeaders(): array
{
return ['domain', 'locale', 'duplicated_language_id'];
}

/**
* @var array<string>
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Model/KeysMissedLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class KeysMissedLine implements ReportLineInterface
private string $domain;
private string $languageId;
/**
* @var array<string>
* @var array<int,string>
*/
private array $omittedLanguages;

Expand All @@ -21,15 +21,15 @@ public function __construct(string $domain, string $languageId, array $omittedLa
}

/**
* @var array<string>
* @return array<int,string>
*/
public static function getHeaders(): array
{
return ['domain', 'language_id', 'omitted_languages'];
}

/**
* @var array<string>
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Model/KeysPatternLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class KeysPatternLine implements ReportLineInterface
private string $domain;
private string $languageId;
/**
* @var array<string>
* @var array<int,string>
*/
private array $locales;

Expand All @@ -21,15 +21,15 @@ public function __construct(string $domain, string $languageId, array $locales)
}

/**
* @var array<string>
* @return array<int,string>
*/
public static function getHeaders(): array
{
return ['domain', 'invalid_language_id', 'locales'];
}

/**
* @var array<string>
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
6 changes: 6 additions & 0 deletions src/Model/ReportBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ final class ReportBag
*/
private array $lines = [];

/**
* @var class-string
*/
private string $reportLineClass;

/**
* @param class-string $reportLineClass
*/
public function __construct(string $reportLineClass)
{
$this->reportLineClass = $reportLineClass;
Expand Down
4 changes: 2 additions & 2 deletions src/Service/DirectoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class DirectoryProvider
{
private string $defaultPath;
/**
* @var array<string>
* @var array<int,string>
*/
private array $dirs;

Expand All @@ -21,7 +21,7 @@ public function __construct(string $defaultPath, iterable $dirs)
}

/**
* @return array<string>
* @return array<int,string>
*/
public function getAll(): array
{
Expand Down
17 changes: 5 additions & 12 deletions src/Service/Yaml/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
namespace Aeliot\Bundle\TransMaintain\Service\Yaml;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

final class FileManipulator
{
private Filesystem $filesystem;
private int $yamlIndent;
private YamlContentHandler $yamlContentHandler;

public function __construct(Filesystem $filesystem, int $yamlIndent)
public function __construct(Filesystem $filesystem, YamlContentHandler $yamlContentHandler)
{
$this->filesystem = $filesystem;
$this->yamlIndent = $yamlIndent;
$this->yamlContentHandler = $yamlContentHandler;
}

public function dump(string $pathOut, array $yaml): void
{
$this->filesystem->mkdir(dirname($pathOut));

//THINK: how to escape single words?
$dumpFlags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK;
$content = Yaml::dump($yaml, 100, $this->yamlIndent, $dumpFlags);
$content = $this->yamlContentHandler->dump($yaml);
$this->filesystem->dumpFile($pathOut, $content);
}

Expand All @@ -39,10 +36,6 @@ public function parse(string $pathIn): array
throw new \InvalidArgumentException(\sprintf('Invalid path passed: "%s"', $pathIn));
}

/**
* NOTE: don't use Yaml::PARSE_CONSTANT like in {@see \Symfony\Component\Translation\Loader\YamlFileLoader::loadResource()}
* to leave values as is
*/
return Yaml::parseFile($pathIn, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
return $this->yamlContentHandler->parseFile($pathIn) ?? [];
}
}
Loading

0 comments on commit 5aa520c

Please sign in to comment.