BrowseRegionsPage   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 139
Duplicated Lines 10.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 14
loc 139
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 5 1
A GeoLevelName() 0 4 1
A GeoLevelNumber() 0 4 1
C requireDefaultRecords() 14 23 11
C CreateRegion() 0 35 7

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
class BrowseRegionsPage extends BrowseAbstractPage
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
        /**
7
     * Standard SS Static
8
     **/
9
    public static $icon = "geobrowser/images/treeicons/BrowseRegionsPage";
10
11
        /**
12
     * Standard SS Static
13
     **/
14
    public static $allowed_children = array("BrowseCitiesPage");
15
16
        /**
17
     * Standard SS Static
18
     **/
19
    public static $default_child = "BrowseCitiesPage";
20
21
        /**
22
     * Standard SS Static
23
     **/
24
    public static $default_parent = "BrowseCountriesPage";
25
26
        /**
27
     * Standard SS Static
28
     **/
29
    public static $can_be_root = false;
30
31
        /**
32
     * Standard SS Static
33
     **/
34
    public static $db = array(
35
        "Code" => "Varchar(8)",
36
    );
37
38
    /**
39
     * Standard SS Static
40
     **/
41
    public static $defaults = array(
42
        "ShowInMenus" => false
43
    );
44
    
45
    /**
46
     * Standard SS Static
47
     **/
48
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50
        $fields = parent::getCMSFields();
51
        return $fields;
52
    }
53
54
55
    /**
56
     * Name of the level
57
     **/
58
    public function GeoLevelName()
59
    {
60
        return "Regions";
61
    }
62
63
    /**
64
     * Number of the level
65
     **/
66
    public function GeoLevelNumber()
67
    {
68
        return 2;
69
    }
70
71
72
    /**
73
     * Setup pages...
74
     **/
75
    public function requireDefaultRecords()
0 ignored issues
show
Coding Style introduced by
requireDefaultRecords uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
    {
77
        parent::requireDefaultRecords();
78
        $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
79
        $parents = DataObject::get("BrowseRegionsPage");
80
        if ($parents && isset($_GET["geobuild"]) && $_GET["geobuild"] && $this->allowBrowseChildren()) {
81 View Code Duplication
            foreach ($parents as $parent) {
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...
82
                if ($parent->CreateChildren && $parent->HiddenDataID) {
83
                    echo "<li>creating cities for ".$parent->Title."<ul>";
84
                    $cities = $this->getDataFromTable("cities", "RegionID = ".$parent->HiddenDataID, "City");
85
                    foreach ($cities as $city) {
86
                        if (!DataObject::get_one("BrowseCitiesPage", "{$bt}BrowseAbstractPage{$bt}.{$bt}HiddenDataID{$bt} = ".$city["CityID"])) {
87
                            $page = new BrowseCitiesPage();
88
                            $page->CreateCity($city, $parent);
89
                            $page->destroy();
90
                        }
91
                    }
92
                    echo "</ul></li>";
93
                }
94
            }
95
            $parents->destroy();
96
        }
97
    }
98
99
    /**
100
     * Creates a region... called from BrowseCountriesPage
101
     *
102
     *@param Array $region - array of region details
103
     *@param Object $parent - a BrowseCountriesPage object
104
     *
105
     **/
106
    public function CreateRegion(array $region, BrowseCountriesPage $parent)
0 ignored issues
show
Coding Style introduced by
CreateRegion uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
107
    {
108
        if ($parent && isset($region["Region"])) {
109
            $name = htmlentities($region["Region"]);
110
            if ($name) {
111
                if (isset($_GET["geobuild"])) {
112
                    echo "<li>creating ".$name."</li>";
113
                }
114
                $this->ParentID = $parent->ID;
115
                $this->Title = $name;
116
                $this->MetaTitle = $name;
117
                $this->MenuTitle = $name;
118
                $this->HiddenDataID = $region["RegionID"];
119
120
                $this->Code = $region["Code"];
121
122
                $this->CreateChildren = $parent->CreateAllChildren;
123
                $this->CreateAllChildren = $parent->CreateAllChildren;
124
125
                $this->URLSegment = $this->generateURLSegment($this->Title);
126
127
                $this->writeToStage('Stage');
128
                $this->publish('Stage', 'Live');
129
                $this->flushCache();
130
            } else {
131
                if (isset($_GET["geobuild"])) {
132
                    debug::show("region does not exist");
133
                }
134
            }
135
        } else {
136
            if (isset($_GET["geobuild"])) {
137
                debug::show("Parent does not exist");
138
            }
139
        }
140
    }
141
}
142
143
class BrowseRegionsPage_Controller extends BrowseAbstractPage_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
144
{
145
}
146