LanguageDropdownField   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 113
Duplicated Lines 8.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 1
dl 10
loc 113
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
F __construct() 10 61 20
A Type() 0 4 1
A getAttributes() 0 7 1
A getLocaleForObject() 0 16 6

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
 * An extension to dropdown field, pre-configured to list languages.
4
 * The languages already used in the site will be on top.
5
 * 
6
 * @package translatable
7
 */
8
class LanguageDropdownField extends GroupedDropdownField
9
{
10
    private static $allowed_actions = array(
11
        'getLocaleForObject'
12
    );
13
    
14
    /**
15
     * Create a new LanguageDropdownField
16
     * @param string $name
17
     * @param string $title
18
     * @param array $excludeLocales List of locales that won't be included
19
     * @param string $translatingClass Name of the class with translated instances 
20
     *               where to look for used languages
21
     * @param string $list Indicates the source language list. 
22
     *               Can be either Common-English, Common-Native, Locale-English, Locale-Native
23
     */
24
    public function __construct($name, $title, $excludeLocales = array(),
25
        $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null
26
    ) {
27
        $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
28
        $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
29
30
        if ('Common-English' == $list) {
31
            $allLocalesWithTitle = i18n::get_common_languages();
32
        } elseif ('Common-Native' == $list) {
33
            $allLocalesWithTitle = i18n::get_common_languages(true);
34
        } elseif ('Locale-English' == $list) {
35
            $allLocalesWithTitle = i18n::get_common_locales();
36
        } elseif ('Locale-Native' == $list) {
37
            $allLocalesWithTitle = i18n::get_common_locales(true);
38
        } else {
39
            $allLocalesWithTitle = i18n::config()->all_locales;
40
        }
41
42
        if (isset($allLocales[Translatable::default_locale()])) {
0 ignored issues
show
Bug introduced by
The variable $allLocales does not exist. Did you mean $allLocalesWithTitle?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
43
            unset($allLocales[Translatable::default_locale()]);
44
        }
45
        
46
        // Limit to allowed locales if defined
47
        // Check for canTranslate() if an $instance is given
48
        $allowedLocales = Translatable::get_allowed_locales();
49
        foreach ($allLocalesWithTitle as $locale => $localeTitle) {
50
            if (
51
                ($allowedLocales && !in_array($locale, $allowedLocales))
0 ignored issues
show
Bug Best Practice introduced by
The expression $allowedLocales of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
                || ($excludeLocales && in_array($locale, $excludeLocales))
0 ignored issues
show
Bug Best Practice introduced by
The expression $excludeLocales of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
                || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle))
0 ignored issues
show
Bug Best Practice introduced by
The expression $usedLocalesWithTitle of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
            ) {
55
                unset($allLocalesWithTitle[$locale]);
56
            }
57
        }
58
        // instance specific permissions
59 View Code Duplication
        foreach ($allLocalesWithTitle as $locale => $localeTitle) {
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...
60
            if ($instance && !$instance->canTranslate(null, $locale)) {
61
                unset($allLocalesWithTitle[$locale]);
62
            }
63
        }
64 View Code Duplication
        foreach ($usedLocalesWithTitle as $locale => $localeTitle) {
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...
65
            if ($instance && !$instance->canTranslate(null, $locale)) {
66
                unset($usedLocalesWithTitle[$locale]);
67
            }
68
        }
69
70
        // Sort by title (array value)
71
        asort($allLocalesWithTitle);
72
        
73
        if (count($usedLocalesWithTitle)) {
74
            asort($usedLocalesWithTitle);
75
            $source = array(
76
                _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle,
77
                _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle
78
            );
79
        } else {
80
            $source = $allLocalesWithTitle;
81
        }
82
83
        parent::__construct($name, $title, $source);
84
    }
85
86
    public function Type()
87
    {
88
        return 'languagedropdown dropdown';
89
    }
90
    
91
    public function getAttributes()
92
    {
93
        return array_merge(
94
            parent::getAttributes(),
95
            array('data-locale-url' => $this->Link('getLocaleForObject'))
96
        );
97
    }
98
    
99
    /**
100
     * Get the locale for an object that has the Translatable extension.
101
     * 
102
     * @return locale
103
     */
104
    public function getLocaleForObject()
105
    {
106
        $id = (int)$this->getRequest()->requestVar('id');
107
        $class = Convert::raw2sql($this->getRequest()->requestVar('class'));
108
        $locale = Translatable::get_current_locale();
109
        if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) {
110
            // temporarily disable locale filter so that we won't filter out the object 
111
            Translatable::disable_locale_filter();
112
            $object = DataObject::get_by_id($class, $id);
113
            Translatable::enable_locale_filter();
114
            if ($object) {
115
                $locale = $object->Locale;
116
            }
117
        }
118
        return $locale;
119
    }
120
}
121