Translator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 75.86 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 44
loc 58
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translateConfig() 14 14 1
A translateKeys() 0 8 1
A translateNestedValues() 30 30 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 Flynt\Features\CustomTaxonomies;
4
5
use Flynt;
6
7
class Translator
8
{
9 View Code Duplication
    public static function translateConfig($config)
0 ignored issues
show
Duplication introduced by
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...
10
    {
11
        $config = apply_filters(
12
            'Flynt/Features/CustomTaxonomies/TranslateConfig',
13
            self::translateKeys($config)
14
        );
15
16
        $config = apply_filters(
17
            "Flynt/Features/CustomTaxonomies/TranslateConfig?name={$config['name']}",
18
            $config
19
        );
20
21
        return $config;
22
    }
23
24
    protected static function translateKeys($config)
25
    {
26
        $config['label'] = self::translateNestedValues($config, 'label');
27
        $config['labels'] = self::translateNestedValues($config, 'labels');
28
        $config['description'] = self::translateNestedValues($config, 'description');
29
30
        return $config;
31
    }
32
33
    // ...$args works in PHP 5.6+
34 View Code Duplication
    protected static function translateNestedValues(...$args)
0 ignored issues
show
Duplication introduced by
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...
35
    {
36
        if (count($args) === 0) {
37
            trigger_error('Invalid argument count for translation!', E_USER_WARNING);
38
            return [];
39
        }
40
41
        if (count($args) === 1) {
42
            return $args[0];
43
        }
44
45
        $value = Flynt\Helpers::extractNestedDataFromArray($args);
46
47
        if (empty($value)) {
48
            return null;
49
        }
50
51
        if (is_array($value)) {
52
            // Loop through array and apply translations while keeping keys intact
53
            // NOTE: assuming it's a single dimensional array
54
            return array_reduce(array_keys($value), function ($carry, $key) use ($value) {
55
                return array_merge($carry, [
56
                    $key => _x($value[$key], $key, 'flynt-starter-theme')
57
                ]);
58
            }, []);
59
        } else {
60
            $context = array_pop($args);
61
            return _x($value, $context, 'flynt-starter-theme');
62
        }
63
    }
64
}
65