Completed
Push — master ( 3528bf...88e290 )
by Nicolaas
03:35
created

ModelAdminEcommerceBaseClass::getManagedModels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @see: http://doc.silverstripe.org/framework/en/reference/ModelAdmin
5
 *
6
 * @author Nicolaas [at] sunnyside up . co .nz
7
 */
8
class ModelAdminEcommerceBaseClass extends ModelAdmin
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...
9
{
10
    /**
11
     * @return array Map of class name to an array of 'title' (see {@link $managed_models})
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
12
     */
13
    public function getManagedModels()
14
    {
15
        if($this->class === 'ModelAdminEcommerceBaseClass') {
16
            //never used
17
            return array('Order' => array('title' => 'All Orders'));
18
        }
19
        return parent::getManagedModels();
20
    }
21
22
    /**
23
     * Change this variable if you don't want the Import from CSV form to appear.
24
     * This variable can be a boolean or an array.
25
     * If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClasstwo').
26
     */
27
    public $showImportForm = false;
28
29
    /**
30
     *
31
     * @param DataObject $record
32
     *
33
     * @return Form
0 ignored issues
show
Documentation introduced by
Should the return type not be SS_HTTPResponse|null|CMSForm?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
34
     */
35
    function oneItemForm($record)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        Config::inst()->update('LeftAndMain', 'tree_class', $record->ClassName);
38
        $form = LeftAndMain::getEditForm($record);
0 ignored issues
show
Documentation introduced by
$record is of type object<DataObject>, but the function expects a integer|null.

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...
39
        $idField = HiddenField::create('ID')->setValue($record->ID);
40
        $cssField = LiteralField::create(
41
            'oneItemFormCSS',
42
            '
43
                <style>
44
                    .cms-content-view .ui-tabs-nav {
45
                        margin-left: 0!important;
46
                    }
47
                    .cms-content-view .Actions {
48
                        position: fixed;
49
                        bottom: 16px;
50
                        right:  16px;
51
                    }
52
                </style>
53
            '
54
        );
55
        $form->Fields()->push($idField);
0 ignored issues
show
Bug introduced by
The method Fields does only exist in CMSForm, but not in SS_HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
        $form->Fields()->push($cssField);
57
        return $form;
58
    }
59
60
    /**
61
     * Define which fields are used in the {@link getEditForm} GridField export.
62
     * By default, it uses the summary fields from the model definition.
63
     *
64
     * @return array
65
     */
66
    public function getExportFields() {
67
        $obj = singleton($this->modelClass);
68
        if($obj->hasMethod('getExportFields')) {
69
            return $obj->getExportFields();
70
        }
71
        return $obj->summaryFields();
72
    }
73
74
75
}
76