Completed
Push — v4.0-dev ( 627b53...5ca120 )
by Oscar
02:44
created

Jed   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
c 2
b 1
f 0
lcom 1
cbo 2
dl 6
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 14 4
B buildMessages() 6 22 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
7
class Jed extends Generator implements GeneratorInterface
8
{
9
    public static $options = JSON_PRETTY_PRINT;
10
11
    /**
12
     * {@parentDoc}.
13
     */
14
    public static function toString(Translations $translations)
15
    {
16
        $domain = $translations->getDomain() ?: 'messages';
17
18
        return json_encode([
19
            $domain => [
20
                '' => [
21
                    'domain' => $domain,
22
                    'lang' => $translations->getLanguage() ?: 'en',
23
                    'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);',
24
                ]
25
            ] + self::buildMessages($translations)
26
        ], static::$options);
27
    }
28
29
    /**
30
     * Generates an array with all translations.
31
     * 
32
     * @param Translations $translations
33
     *
34
     * @return array
35
     */
36
    private static function buildMessages(Translations $translations)
37
    {
38
        $pluralForm = $translations->getPluralForms();
39
        $pluralLimit = is_array($pluralForm) ? ($pluralForm[0] - 1) : null;
40
        $messages = [];
41
        $context_glue = '\u0004';
42
43
        foreach ($translations as $translation) {
44
            $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '').$translation->getOriginal();
45
46 View Code Duplication
            if ($translation->hasPluralTranslations()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                $message = $translation->getPluralTranslations($pluralLimit);
48
                array_unshift($message, $translation->getTranslation());
49
            } else {
50
                $message = [$translation->getTranslation()];
51
            }
52
53
            $messages[$key] = $message;
54
        }
55
56
        return $messages;
57
    }
58
}
59