Skip to content

Commit

Permalink
improve complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 16, 2020
1 parent 5b7f1e6 commit f46483d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
"jakub-onderka/php-parallel-lint": "^1.0",
"phpstan/phpstan": "^0.12.14",
"phpunit/phpunit": "^8.5",
"rector/rector": "^0.7.3",
"rector/rector": "^0.7.6",
"symplify/changelog-linker": "^7.2",
"symplify/easy-coding-standard": "^7.2",
"symplify/phpstan-extensions": "^7.2",
"phpstan/phpstan-doctrine": "^0.12.4",
"phpstan/phpstan-phpunit": "^0.12.2",
"symplify/changelog-linker": "^7.2"
"phpstan/phpstan-phpunit": "^0.12.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ parameters:
PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer: null
Symplify\CodingStandard\Sniffs\ControlStructure\SprintfOverContactSniff: null
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenStaticFunctionSniff: null
Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff: null
# Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff: null
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenReferenceSniff: null
Symplify\CodingStandard\Sniffs\Architecture\ExplicitExceptionSniff: null
Symplify\CodingStandard\Sniffs\Architecture\DuplicatedClassShortNameSniff: null
Expand Down
25 changes: 16 additions & 9 deletions src/Model/Loggable/LoggableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ public function getUpdateLogMessage(array $changeSets = []): string
}
}

if ($changeSet[0] !== $changeSet[1]) {
$message[] = sprintf(
'%s #%d : property "%s" changed from "%s" to "%s"',
self::class,
$this->getId(),
$property,
! is_array($changeSet[0]) ? $changeSet[0] : 'an array',
! is_array($changeSet[1]) ? $changeSet[1] : 'an array'
);
if ($changeSet[0] === $changeSet[1]) {
continue;
}

$message[] = $this->createChangeSetMessage($property, $changeSet);
}

return implode("\n", $message);
Expand All @@ -42,4 +37,16 @@ public function getRemoveLogMessage(): string
{
return sprintf('%s #%d removed', self::class, $this->getId());
}

private function createChangeSetMessage(string $property, array $changeSet): string
{
return sprintf(
'%s #%d : property "%s" changed from "%s" to "%s"',
self::class,
$this->getId(),
$property,
! is_array($changeSet[0]) ? $changeSet[0] : 'an array',
! is_array($changeSet[1]) ? $changeSet[1] : 'an array'
);
}
}
31 changes: 18 additions & 13 deletions src/Model/Translatable/TranslatableMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,14 @@ protected function doTranslate(?string $locale = null, bool $fallbackToDefault =
}

$translation = $this->findTranslationByLocale($locale);
if ($translation and ! $translation->isEmpty()) {
if ($translation && ! $translation->isEmpty()) {
return $translation;
}

if ($fallbackToDefault) {
$fallbackLocale = $this->computeFallbackLocale($locale);

if ($fallbackLocale) {
$translation = $this->findTranslationByLocale($fallbackLocale);
if ($translation) {
return $translation;
}
}

$defaultTranslation = $this->findTranslationByLocale($this->getDefaultLocale(), false);
if ($defaultTranslation) {
return $defaultTranslation;
$fallbackTranslation = $this->resolveFallbackTranslation($locale);
if ($fallbackTranslation !== null) {
return $fallbackTranslation;
}
}

Expand Down Expand Up @@ -235,4 +226,18 @@ private function ensureIsIterableOrCollection($translations): void
'$translations parameter must be iterable or %s', Collection::class)
);
}

private function resolveFallbackTranslation(?string $locale): ?TranslationInterface
{
$fallbackLocale = $this->computeFallbackLocale($locale);

if ($fallbackLocale) {
$translation = $this->findTranslationByLocale($fallbackLocale);
if ($translation) {
return $translation;
}
}

return $this->findTranslationByLocale($this->getDefaultLocale(), false);
}
}

0 comments on commit f46483d

Please sign in to comment.