BrowseContinentsPage   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 115
Duplicated Lines 12.17 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A GeoLevelName() 0 4 1
A GeoLevelNumber() 0 4 1
C requireDefaultRecords() 14 22 11
C CreateContinent() 0 30 8

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 BrowseContinentsPage 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/BrowseContinentsPage";
10
11
12
    /**
13
     * Standard SS static
14
     **/
15
    public static $allowed_children = array("BrowseCountriesPage");
16
17
    /**
18
     * Standard SS static
19
     **/
20
    public static $default_child = "BrowseCountriesPage";
21
22
    /**
23
     * Standard SS static
24
     **/
25
    public static $default_parent = "BrowseWorldPage";
26
27
        /**
28
     * Standard SS Static
29
     **/
30
    public static $can_be_root = false;
31
    
32
    /**
33
     * Standard SS Static
34
     **/
35
    public static $defaults = array(
36
        "ShowInMenus" => true
37
    );
38
    
39
    /**
40
     * name of the level
41
     **/
42
    public function GeoLevelName()
43
    {
44
        return "Continents";
45
    }
46
47
    /**
48
     * number of the level
49
     **/
50
    public function GeoLevelNumber()
51
    {
52
        return 0;
53
    }
54
55
    /**
56
     * Standard SS Method
57
     * setup records
58
     **/
59
    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...
60
    {
61
        $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
62
        parent::requireDefaultRecords();
63
        $parents = DataObject::get("BrowseContinentsPage");
64
        if ($parents && isset($_GET["geobuild"]) && $_GET["geobuild"] && $this->allowBrowseChildren()) {
65 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...
66
                if ($parent->CreateChildren && $parent->HiddenDataID) {
67
                    echo "<li>creating countries for ".$parent->Title."<ul>";
68
                    $countries = $this->getDataFromTable("countries", "ContinentID = ".$parent->HiddenDataID, "Country");
69
                    foreach ($countries as $country) {
70
                        if (!DataObject::get_one("BrowseCountriesPage", "{$bt}HiddenDataID{$bt} = ".$country["CountryID"])) {
71
                            $page = new BrowseCountriesPage();
72
                            $page->CreateCountry($country, $parent);
73
                            $page->destroy();
74
                        }
75
                    }
76
                    echo "</ul></li>";
77
                }
78
            }
79
        }
80
    }
81
    
82
    /**
83
     * create a continent
84
     * @param array - $contentint, continent data
85
     * @param Object - $parent, a BrowseWorldPage object that will be the parent page of the Continent.
86
     **/
87
    public function CreateContinent(array $continent, BrowseWorldPage $parent)
0 ignored issues
show
Coding Style introduced by
CreateContinent 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...
88
    {
89
        $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
0 ignored issues
show
Unused Code introduced by
$bt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        if ($parent && isset($continent["Continent"])) {
91
            $name = htmlentities($continent["Continent"]);
92
            if ($name) {
93
                if (isset($_GET["geobuild"])) {
94
                    echo "<li>creating ".$name."</li>";
95
                }
96
                $this->ParentID = $parent->ID;
97
                $this->Title = $name;
98
                $this->MetaTitle = $name;
99
                $this->MenuTitle = $name;
100
                $this->HiddenDataID = $continent["ContinentID"];
101
                $this->CreateChildren = $parent->CreateAllChildren;
102
                $this->CreateAllChildren = $parent->CreateAllChildren;
103
                $this->writeToStage('Stage');
104
                $this->publish('Stage', 'Live');
105
                $this->flushCache();
106
            } else {
107
                if (isset($_GET["geobuild"])) {
108
                    debug::show("name does not exist");
109
                }
110
            }
111
        } else {
112
            if (isset($_GET["geobuild"])) {
113
                debug::show("Parent does not exist");
114
            }
115
        }
116
    }
117
}
118
119
class BrowseContinentsPage_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...
120
{
121
}
122