Skip to content

Commit

Permalink
new table layout and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Feb 23, 2024
1 parent afce6ab commit 051c157
Show file tree
Hide file tree
Showing 33 changed files with 505 additions and 566 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@ using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [Unreleased]

### Added

- Add new option to **ignore** some keys from case style validation. Sometimes you are bound to the platform you are
using for some keys.
- Add new **table layout** for all errors on CLI. This makes it easier to read and understand the errors.

### Changed

- Validators will now show a new **table layout** for all errors. This makes it easier to read and understand the
errors.

### Fixed

- Positive test results from mess validations where missing in reports.
- Case Style validation always showed up in reports even though not configured.

## [1.17.0]

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ValidateAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function execute(InputInterface $input, OutputInterface $output): int

$translationSetCLI = new TranslationSetCliFacade($io);
$coverageCLI = new CoverageCliFacade($io);
$validatorsCLI = new ValidatorCliFacade($io);
$validatorsCLI = new ValidatorCliFacade($io, $output);
$reporterCLI = new ReporterCliFacade($io);

# -----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ValidateStructureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$validators[] = new MissingStructureValidator();

$translationSetCLI = new TranslationSetCliFacade($io);
$validatorsCLI = new ValidatorCliFacade($io);
$validatorsCLI = new ValidatorCliFacade($io, $output);
$reporterCLI = new ReporterCliFacade($io);

# -----------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/Components/Reporter/Model/ReportResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ class ReportResult
{

/**
* @var TranslationSetResult[]
* @var ReportSetResult[]
*/
private $suites = [];


/**
* @param TranslationSetResult $result
* @param ReportSetResult $result
* @return void
*/
public function addTranslationSet(TranslationSetResult $result): void
public function addTranslationSet(ReportSetResult $result): void
{
$this->suites[] = $result;
}

/**
* @return TranslationSetResult[]
* @return ReportSetResult[]
*/
public function getSuites(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PHPUnuhi\Components\Reporter\Model;

class TranslationSetResult
class ReportSetResult
{

/**
Expand All @@ -11,7 +11,7 @@ class TranslationSetResult
private $name;

/**
* @var TestResult[]
* @var ReportTestResult[]
*/
private $tests = [];

Expand All @@ -32,16 +32,16 @@ public function getName(): string
}

/**
* @param TestResult $result
* @param ReportTestResult $result
* @return void
*/
public function addTestResult(TestResult $result): void
public function addTestResult(ReportTestResult $result): void
{
$this->tests[] = $result;
}

/**
* @return TestResult[]
* @return ReportTestResult[]
*/
public function getTests(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PHPUnuhi\Components\Reporter\Model;

class TestResult
class ReportTestResult
{

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Components/Reporter/Service/ReportTestResultConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PHPUnuhi\Components\Reporter\Service;

use PHPUnuhi\Components\Reporter\Model\ReportTestResult;
use PHPUnuhi\Components\Validator\Model\ValidationTest;

class ReportTestResultConverter
{

/**
* @param ValidationTest $test
* @return ReportTestResult
*/
public function toTestResult(ValidationTest $test): ReportTestResult
{
return new ReportTestResult(
$test->getTitle(),
$test->getTranslationKey(),
basename($test->getFilename()),
$test->getLineNumber(),
$test->getClassification(),
$test->getFailureMessage(),
$test->isSuccess()
);
}
}
106 changes: 49 additions & 57 deletions src/Components/Validator/CaseStyleValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnuhi\Bundles\Storage\StorageInterface;
use PHPUnuhi\Components\Validator\CaseStyle\CaseStyleValidatorFactory;
use PHPUnuhi\Components\Validator\CaseStyle\Exception\CaseStyleNotFoundException;
use PHPUnuhi\Components\Validator\Model\ValidationError;
use PHPUnuhi\Components\Validator\Model\ValidationResult;
use PHPUnuhi\Components\Validator\Model\ValidationTest;
use PHPUnuhi\Models\Configuration\CaseStyle\CaseStyle;
Expand Down Expand Up @@ -37,88 +36,81 @@ public function validate(TranslationSet $set, StorageInterface $storage): Valida
$hierarchy = $storage->getHierarchy();

$tests = [];
$errors = [];

$caseStyles = $set->getCasingStyleSettings()->getCaseStyles();
$ignoreKeys = $set->getCasingStyleSettings()->getIgnoreKeys();

foreach ($set->getLocales() as $locale) {
foreach ($locale->getTranslations() as $translation) {
$isKeyCaseValid = true;
$invalidKeyPart = '';
$isCurrentKeyValid = true;
$allowedCaseStylesText = implode(', ', array_map(function (CaseStyle $caseStyle): string {
return $caseStyle->getName();
}, $caseStyles));

# we have a full key like root.sub.lblTitle
# and we want to split it into parts and separately check the cases of the hierarchy levels
# sample: root.sub.lblTitle => [root, sub, lblTitle]
$keyParts = $this->getKeyParts($translation->getKey(), $hierarchy);
if ($caseStyles !== []) {
foreach ($set->getLocales() as $locale) {
foreach ($locale->getTranslations() as $translation) {
$isKeyCaseValid = true;
$invalidKeyPart = '';
$isCurrentKeyValid = true;

$partLevel = 0;
# we have a full key like root.sub.lblTitle
# and we want to split it into parts and separately check the cases of the hierarchy levels
# sample: root.sub.lblTitle => [root, sub, lblTitle]
$keyParts = $this->getKeyParts($translation->getKey(), $hierarchy);

foreach ($keyParts as $part) {
$isPartValid = $this->verifyLevel($part, $partLevel, $caseStyles);
$partLevel = 0;

if (!$isPartValid) {
$invalidKeyPart = $part;
$isCurrentKeyValid = false;
break;
}
foreach ($keyParts as $part) {
$isPartValid = $this->verifyLevel($part, $partLevel, $caseStyles);

$partLevel++;
}
if (!$isPartValid) {
$invalidKeyPart = $part;
$isCurrentKeyValid = false;
break;
}

$partLevel++;
}


# if it's somehow not valid
# then make sure to also check ouf ignore list
# then it might be valid :)
if (!$isCurrentKeyValid) {
# sample: $part => root.sub.IGNORE_THIS (fully qualified)
# also check ignore list
foreach ($ignoreKeys as $ignoreKey) {
if ($ignoreKey->isFullyQualifiedPath()) {
if ($translation->getKey() === $ignoreKey->getKey()) {
# if it's somehow not valid
# then make sure to also check ouf ignore list
# then it might be valid :)
if (!$isCurrentKeyValid) {
# sample: $part => root.sub.IGNORE_THIS (fully qualified)
# also check ignore list
foreach ($ignoreKeys as $ignoreKey) {
if ($ignoreKey->isFullyQualifiedPath()) {
if ($translation->getKey() === $ignoreKey->getKey()) {
$isCurrentKeyValid = true;
break;
}
} elseif ($this->stringDoesContain($translation->getKey(), $ignoreKey->getKey())) {
$isCurrentKeyValid = true;
break;
}
} elseif ($this->stringDoesContain($translation->getKey(), $ignoreKey->getKey())) {
$isCurrentKeyValid = true;
break;
}
}
}

if (!$isCurrentKeyValid) {
$isKeyCaseValid = false;
}


$testPassed = $isKeyCaseValid;
if (!$isCurrentKeyValid) {
$isKeyCaseValid = false;
}

$tests[] = new ValidationTest(
$translation->getKey(),
$locale->getName(),
'Test case-style of key: ' . $translation->getKey(),
$locale->getFilename(),
$locale->findLineNumber($translation->getKey()),
$this->getTypeIdentifier(),
'Translation key ' . $translation->getKey() . ' has part with invalid case-style: ' . $invalidKeyPart . ' at level: ' . $partLevel,
$testPassed
);
$testPassed = $isKeyCaseValid;

if (!$testPassed) {
$errors[] = new ValidationError(
$this->getTypeIdentifier(),
'Invalid case-style for key: ' . $invalidKeyPart . ' at level: ' . $partLevel,
$tests[] = new ValidationTest(
$translation->getKey(),
$locale->getName(),
"Test case-style of key '" . $translation->getKey() . "' to be one of: " . $allowedCaseStylesText,
$locale->getFilename(),
$translation->getKey(),
$locale->findLineNumber($translation->getKey())
$locale->findLineNumber($translation->getKey()),
$this->getTypeIdentifier(),
"Invalid case-style for part '" . $invalidKeyPart . "' in key '" . $translation->getKey() . "' at level: " . $partLevel,
$testPassed
);
}
}
}

return new ValidationResult($tests, $errors);
return new ValidationResult($tests);
}

/**
Expand Down
52 changes: 25 additions & 27 deletions src/Components/Validator/EmptyContentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use PHPUnuhi\Bundles\Storage\StorageInterface;
use PHPUnuhi\Components\Validator\EmptyContent\AllowEmptyContent;
use PHPUnuhi\Components\Validator\Model\ValidationError;
use PHPUnuhi\Components\Validator\Model\ValidationResult;
use PHPUnuhi\Components\Validator\Model\ValidationTest;
use PHPUnuhi\Models\Translation\Locale;
use PHPUnuhi\Models\Translation\Translation;
use PHPUnuhi\Models\Translation\TranslationSet;

class EmptyContentValidator implements ValidatorInterface
Expand Down Expand Up @@ -44,7 +45,6 @@ public function getTypeIdentifier(): string
public function validate(TranslationSet $set, StorageInterface $storage): ValidationResult
{
$tests = [];
$errors = [];

foreach ($set->getLocales() as $locale) {
foreach ($locale->getTranslations() as $translation) {
Expand All @@ -61,39 +61,37 @@ public function validate(TranslationSet $set, StorageInterface $storage): Valida
}
}

$tests[] = new ValidationTest(
$translation->getKey(),
$locale->getName(),
'Test existing translation of key: ' . $translation->getKey(),
$locale->getFilename(),
$locale->findLineNumber($translation->getKey()),
$this->getTypeIdentifier(),
'Translation for key ' . $translation->getKey() . ' does not have a value',
$testPassed
);


if ($testPassed) {
continue;
}

if ($translation->getGroup() !== '') {
$identifier = $translation->getGroup() . ' (group) => ' . $translation->getKey();
} else {
$identifier = $translation->getID();
}

$errors[] = new ValidationError(
$this->getTypeIdentifier(),
'Found empty translation',
$locale->getName(),
$locale->getFilename(),
$identifier,
$locale->findLineNumber($identifier)
);
$tests[] = $this->buildValidationTest($identifier, $locale, $translation, $testPassed);
}
}

return new ValidationResult($tests, $errors);
return new ValidationResult($tests);
}

/**
* @param string $identifier
* @param Locale $locale
* @param Translation $translation
* @param bool $testPassed
* @return ValidationTest
*/
private function buildValidationTest(string $identifier, Locale $locale, Translation $translation, bool $testPassed): ValidationTest
{
return new ValidationTest(
$identifier,
$locale->getName(),
'Test existing translation for key: ' . $translation->getKey(),
$locale->getFilename(),
$locale->findLineNumber($translation->getKey()),
$this->getTypeIdentifier(),
'Translation for key ' . $translation->getKey() . ' does not have a value',
$testPassed
);
}
}
Loading

0 comments on commit 051c157

Please sign in to comment.