GoogleCustomSearchPage_Controller   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 5
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 26 4
A SearchPhrase() 0 8 2
A getTitle() 0 8 2
A recordsearch() 0 6 3
1
<?php
2
3
/**
4
 * @package googlesitesearch
5
 */
6
class GoogleCustomSearchPage extends Page
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...
7
{
8
    private static $icon = "googlecustomsearch/images/treeicons/GoogleCustomSearchPage";
0 ignored issues
show
Unused Code introduced by
The property $icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
10
    private static $allowed_children = "none";
0 ignored issues
show
Unused Code introduced by
The property $allowed_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    private static $can_be_root = true;
0 ignored issues
show
Unused Code introduced by
The property $can_be_root is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    private static $description = "Page to search via Google and display search results.";
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    /**
17
     * Standard SS variable.
18
     */
19
    private static $singular_name = "Google Search Results Page";
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
    public function i18n_singular_name()
21
    {
22
        return _t("GoogleCustomSearchPage.SINGULAR_NAME", "Google Search Results Page");
23
    }
24
25
    /**
26
     * Standard SS variable.
27
     */
28
    private static $plural_name = "Google Search Results Pages";
0 ignored issues
show
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
    public function i18n_plural_name()
30
    {
31
        return _t("GoogleCustomSearchPage.PLURAL_NAME", "Google Search Results Pages");
32
    }
33
34
    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...
35
    {
36
        $fields = parent::getCMSFields();
37
        $fields->addFieldToTab("Root.Searches",
38
            new GoogleCustomSearchPage_RecordField("stats", "Search History Last 100 Days")
39
        );
40
        return $fields;
41
    }
42
43
    public function requireDefaultRecords()
44
    {
45
        if ($this->canCreate()) {
46
            DB::alteration_message("Creating a GoogleCustomSearchPage", "created");
47
            $page = new GoogleCustomSearchPage();
48
            $page->writeToStage('Stage');
49
            $page->publish('Stage', 'Live');
50
        }
51
    }
52
53
    public function populateDefaults()
54
    {
55
        parent::populateDefaults();
56
        $this->Title = "Search";
57
        $this->MenuTitle = "Search";
58
        $this->ShowInMenus = 0;
59
        $this->ShowInSearch = 0;
60
        $this->URLSegment = "search";
61
    }
62
63
    public function canCreate($member = null)
64
    {
65
        return GoogleCustomSearchPage::get()->count() ?  false : true;
66
    }
67
}
68
69
class GoogleCustomSearchPage_Controller extends Page_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...
70
{
71
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
72
        "recordsearch"
73
    );
74
75
    public function init()
0 ignored issues
show
Coding Style introduced by
init 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::init();
78
        //register any search
79
        if (isset($_GET["search"])) {
80
            $searchString = Convert::raw2sql($_GET["search"]);
81
            $forwardto = "";
82
            if (isset($_GET["forwardto"])) {
83
                $forwardto = Convert::raw2sql($_GET["forwardto"]);
84
            }
85
            GoogleCustomSearchPage_Record::add_entry($searchString, $forwardto);
0 ignored issues
show
Bug introduced by
It seems like $searchString defined by \Convert::raw2sql($_GET['search']) on line 80 can also be of type array; however, GoogleCustomSearchPage_Record::add_entry() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $forwardto defined by \Convert::raw2sql($_GET['forwardto']) on line 83 can also be of type array; however, GoogleCustomSearchPage_Record::add_entry() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
        }
87
        if ($this->request->param("Action") != "recordsearch") {
88
            Requirements::themedCSS('GoogleCustomSearchPage');
89
            Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
90
            Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearchPage.js');
91
            $cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
92
            Requirements::customScript("
93
					GoogleCustomSearchPage.cxKey = '".$cxKey."';
94
				",
95
                "GoogleCustomSearchPage"
96
            );
97
        } else {
98
            echo "registered ...";
99
        }
100
    }
101
102
    /**
103
     * template function,
104
     *
105
     * @ return String
106
     */
107
    public function SearchPhrase()
0 ignored issues
show
Coding Style introduced by
SearchPhrase 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...
108
    {
109
        $string = "";
110
        if (isset($_GET['search'])) {
111
            $string = $_GET['search'];
112
        }
113
        return DBField::create_field('HTMLText', $string);
114
    }
115
116
    /**
117
     *
118
     * @return String
119
     */
120
    public function getTitle()
121
    {
122
        if ($searchPhrase = $this->SearchPhrase()->forTemplate()) {
123
            return $this->dataRecord->Title._t("GoogleCustomSearchPage.FOR", " for ").$searchPhrase;
124
        } else {
125
            return $this->dataRecord->Title;
126
        }
127
    }
128
129
    public function recordsearch($request)
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...
Coding Style introduced by
recordsearch 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...
130
    {
131
        if (isset($_GET["forwardto"]) && $_GET["forwardto"]) {
132
            return $this->redirect($_GET["forwardto"]);
133
        }
134
    }
135
}
136