BrowseWorldPage   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 17.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 6 1
C requireDefaultRecords() 14 22 9

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
4
5
class BrowseWorldPage 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...
6
{
7
8
    /**
9
     * Standard SS Static
10
     **/
11
    public static $icon = "geobrowser/images/treeicons/BrowseWorldPage";
12
13
    /**
14
     * Standard SS static
15
     **/
16
    public static $allowed_children = array("BrowseContinentsPage");
17
18
    /**
19
     * Standard SS Static
20
     **/
21
    public static $default_child = "BrowseContinentsPage";
22
23
    /**
24
     * Standard SS Static
25
     **/
26
    public static $db = array(
27
        "LevelOfDetail" => "Int",
28
    );
29
    
30
    /**
31
     * Standard SS Static
32
     **/
33
    public static $defaults = array(
34
        "CreateChildren" => true,
35
        "LevelOfDetail" => 2,
36
        "ShowInMenus" => true
37
    );
38
    
39
    /**
40
     * @var Array
41
     * Sets the level of detail in terms of the pages that are automatically created.
42
     **/
43
    protected static $LevelOfDetailArray = array(
44
        "0" => "Continents",
45
        "1" => "Countries",
46
        "2" => "Regions",
47
        "3" => "Cities",
48
        "4" => "Suburbs"
49
    );
50
    
51
52
    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...
53
    {
54
        $fields = parent::getCMSFields();
55
        $fields->addFieldToTab("Root.Content.AddSubRegion", new DropdownField("LevelOfDetail", "Greatest Level of Detail in pages shown ...", self::$LevelOfDetailArray));
56
        return $fields;
57
    }
58
59
    /**
60
     * Standard SS function
61
     * Creating level of detail
62
     * Note that the _GET variable "geobuild" needs to be turned on.
63
     **/
64
    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...
65
    {
66
        parent::requireDefaultRecords();
67
        $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
68
        $parents = DataObject::get("BrowseWorldPage");
69
        if ($parents && isset($_GET["geobuild"]) && $_GET["geobuild"]) {
70 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...
71
                if ($parent->CreateChildren) {
72
                    echo "<li>creating continents for ".$parent->Title."<ul>";
73
                    $continents = $this->getDataFromTable("continents", null, "Continent");
74
                    foreach ($continents as $continent) {
75
                        if (!DataObject::get("BrowseContinentsPage", "{$bt}BrowseAbstractPage{$bt}.{$bt}HiddenDataID{$bt} = ".$continent["ContinentID"])) {
76
                            $page = new BrowseContinentsPage();
77
                            $page->CreateContinent($continent, $parent);
78
                            $page->destroy();
79
                        }
80
                    }
81
                    echo "</ul></li>";
82
                }
83
            }
84
        }
85
    }
86
}
87
88
class BrowseWorldPage_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...
89
{
90
}
91