ForumCountryDropdownField   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 67
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A locale() 0 7 3
B __construct() 0 22 5
B Field() 0 18 7
1
<?php
2
3
/**
4
 * A simple extension to dropdown field, pre-configured to list countries.
5
 * It will default to the country of the current visitor.
6
 *
7
 * @package forms
8
 * @subpackage fields-relational
9
 */
10
class ForumCountryDropdownField extends DropdownField
11
{
12
13
    /**
14
     * @var bool - Should we default the dropdown to the region determined from the user's locale?
15
     */
16
    private static $default_to_locale = true;
17
18
    /**
19
     * @var string - The region code to default to if default_to_locale is set to false, or we can't determine a region from a locale
20
     */
21
    private static $default_country = 'NZ';
22
23
    /**
24
     * Get the locale of the Member, or if we're not logged in or don't have a locale, use the default one
25
     * @return string
26
     */
27
    protected function locale()
28
    {
29
        if (($member = Member::currentUser()) && $member->Locale) {
30
            return $member->Locale;
31
        }
32
        return i18n::get_locale();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \i18n::get_locale(); (array|integer|double|string|boolean) is incompatible with the return type documented by ForumCountryDropdownField::locale of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
33
    }
34
35
    public function __construct($name, $title = null, $source = null, $value = "", $form = null)
36
    {
37
        if (!is_array($source)) {
38
            // Get a list of countries from Zend
39
            $source = Zend_Locale::getTranslationList('territory', $this->locale(), 2);
40
41
            // We want them ordered by display name, not country code
42
43
            // PHP 5.3 has an extension that sorts UTF-8 strings correctly
44
            if (class_exists('Collator') && ($collator = Collator::create($this->locale()))) {
45
                $collator->asort($source);
46
            } // Otherwise just put up with them being weirdly ordered for now
47
            else {
48
                asort($source);
49
            }
50
51
            // We don't want "unknown country" as an option
52
            unset($source['ZZ']);
53
        }
54
55
        parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form);
0 ignored issues
show
Security Bug introduced by
It seems like $source defined by \Zend_Locale::getTransla...y', $this->locale(), 2) on line 39 can also be of type false; however, DropdownField::__construct() does only seem to accept array|object<ArrayAccess>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
56
    }
57
58
    public function Field($properties = array())
59
    {
60
        $source = $this->getSource();
61
62
        if (!$this->value || !isset($source[$this->value])) {
63
            if ($this->config()->get('default_to_locale') && $this->locale()) {
64
                $locale = new Zend_Locale();
65
                $locale->setLocale($this->locale());
66
                $this->value = $locale->getRegion();
67
            }
68
        }
69
70
        if (!$this->value || !isset($source[$this->value])) {
71
            $this->value = $this->config()->get('default_country');
72
        }
73
74
        return parent::Field();
75
    }
76
}
77