SearchPlusSearchForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 37
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B SearchPlusForm() 0 34 6
1
<?php
2
3
/*
4
 * @author: nicolaas[at]sunnysideup.co.nz
5
 * @description: adds a SearchForm to all Page Controller classes.
6
 *   so that you can add a search form to all pages
7
 *   the page is submitted to the SearchPlusPage.
8
 *   This is different from a "standard" search form,
9
 *   which is always submitted to the page it was submitted from.
10
 *
11
 *
12
 *
13
 **/
14
15
class SearchPlusSearchForm extends Extension
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...
16
{
17
    public function SearchPlusForm($name = "SearchForm", $fieldName = "Search", $fieldLabel = '')
0 ignored issues
show
Coding Style introduced by
SearchPlusForm uses the super-global variable $_REQUEST 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...
18
    {
19
        $action = $this->getRequest()->param("Action");
0 ignored issues
show
Bug introduced by
The method getRequest() does not seem to exist on object<SearchPlusSearchForm>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
$action 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...
20
        $page = SearchPlusPage::get()->first();
21
        if ($page) {
22
            //!in_array($action, array("login", "logout")) &&
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
            if (!$fieldLabel) {
24
                if (isset($_REQUEST[$fieldName])) {
25
                    $searchText = $_REQUEST[$fieldName];
26
                    //we set $_REQUEST["Search"] below because it is also used by things like Text::ContextSummary
27
                    $_REQUEST["Search"] = $_REQUEST[$fieldName];
28
                } elseif (isset($_REQUEST["Search"])) {
29
                    $searchText = $_REQUEST["Search"];
30
                } else {
31
                    $searchText = 'Search';
32
                }
33
            } else {
34
                $searchText = '';
35
            }
36
            $field = new TextField($fieldName, $fieldLabel, $searchText);
37
            $fields = new FieldList($field);
38
            $actions = new FieldList(
39
                new FormAction('results', 'Search')
40
            );
41
            $form = new SearchForm($this, $name, $fields, $actions);
0 ignored issues
show
Documentation introduced by
$this is of type this<SearchPlusSearchForm>, but the function expects a object<Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
43
            $form->setFormAction($page->Link()."results/");
44
            $form->setPageLength(Config::inst()->get("SearchPlusPage", "result_length"));
45
            $form->unsetValidator();
46
            return $form;
47
        } elseif (!$page) {
48
            user_error("You need to create a SearchPlusPage to have a search box", E_USER_NOTICE);
49
        }
50
    }
51
}
52