I18n   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 27.59 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 32
loc 116
rs 10
wmc 9
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 21 2
A nget() 0 16 2
A cget() 16 16 2
A cnget() 16 16 2
A sprintf() 0 5 1

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
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    I18n
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
/**
17
 * Enlazando al textdomain de la aplicacion
18
 */
19
bindtextdomain('default', APP_PATH . 'locale/');
20
textdomain('default');
21
22
/**
23
 * Implementación para internacionalización
24
 *
25
 * @category   Kumbia
26
 * @package    I18n
27
 */
28
class I18n
29
{
30
31
    /**
32
     * Efectua una traducción. Cuando se pasan argumentos adicionales se remplaza con sprintf
33
     *
34
     * @example
35
     *   $saludo = I18n::get('Hola %s', 'Emilio')
36
     *
37
     * @param string
38
     * @return string
39
     * */
40
    public static function get($sentence)
41
    {
42
        /**
43
         * Obtengo la traduccion
44
         * */
45
        $sentence = gettext($sentence);
46
47
        /**
48
         * Si se pasan multiples parametros
49
         * */
50
        if (func_num_args() > 1) {
51
            $args = func_get_args();
52
            /**
53
             * Se remplaza con vsprintf
54
             * */
55
            unset($args[0]);
56
            $sentence = vsprintf($sentence, $args);
57
        }
58
59
        return $sentence;
60
    }
61
62
    /**
63
     * Obtiene una traduccion al plural, cuando se pasan argumentos adicionales se remplaza con sprintf
64
     *
65
     * @param string $sentence1 mensaje en singular
66
     * @param string $sentence2 mensaje en plural
67
     * @param int $n conteo
68
     * @return string
69
     * */
70
    public static function nget($sentence1, $sentence2, $n)
71
    {
72
        /**
73
         * Obtengo la traduccion
74
         * */
75
        $sentence = ngettext($sentence1, $sentence2, $n);
76
77
        /**
78
         * Si se pasan multiples parametros
79
         * */
80
        if (func_num_args() > 3) {
81
            $sentence = $sentence = self::sprintf($sentence, func_get_args(), 3);
82
        }
83
84
        return $sentence;
85
    }
86
87
    /**
88
     * Obtiene una traduccion por categoria, cuando se pasan argumentos adicionales se remplaza con sprintf
89
     *
90
     * @param string $sentence
91
     * @param int $category categoria del mensaje (LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_ALL)
92
     * @return string
93
     * */
94 View Code Duplication
    public static function cget($sentence, $category)
0 ignored issues
show
Duplication introduced by Emilio Silveira
This method seems to be duplicated in 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...
95
    {
96
        /**
97
         * Obtengo la traduccion
98
         * */
99
        $sentence = dcgettext(textdomain(null), $sentence, $category);
100
101
        /**
102
         * Si se pasan multiples parametros
103
         * */
104
        if (func_num_args() > 2) {
105
            $sentence = $sentence = self::sprintf($sentence, func_get_args(), 2);
106
        }
107
108
        return $sentence;
109
    }
110
111
    /**
112
     * Obtiene una traduccion al plural por categoria, cuando se pasan argumentos adicionales se remplaza con sprintf
113
     *
114
     * @param string $sentence1 mensaje en singular
115
     * @param string $sentence2 mensaje en plural
116
     * @param int $n conteo
117
     * @param int $category categoria del mensaje (LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_ALL)
118
     * @return string
119
     * */
120 View Code Duplication
    public static function cnget($sentence1, $sentence2, $n, $category)
0 ignored issues
show
Duplication introduced by Joan Miquel
This method seems to be duplicated in 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...
121
    {
122
        /**
123
         * Obtengo la traduccion en funcion del dominio
124
         * */
125
        $sentence = dcngettext(textdomain(null), $sentence1, $sentence2, $n, $category);
126
127
        /**
128
         * Si se pasan multiples parametros
129
         * */
130
        if (func_num_args() > 4) {
131
            $sentence = self::sprintf($sentence, func_get_args(), 4);
132
        }
133
134
        return $sentence;
135
    }
136
    
137
    
138
    private static function sprintf($sentence, $args, $offset)
139
    {
140
        return vsprintf( $sentence, array_slice( $args, $offset));
141
        
142
    }
143
}
144