Completed
Push — master ( ce2dd0...10a6ac )
by Nicolaas
04:02
created

OrderModifierForm::saveDataToSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @description: this class is the base class for modifier forms in the checkout form... we could do with more stuff here....
5
 *
6
 *
7
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
8
 * @package: ecommerce
9
 * @sub-package: forms
10
 * @inspiration: Silverstripe Ltd, Jeremy
11
 **/
12
class OrderModifierForm extends Form
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...
13
{
14
    /**
15
     * @var Order
16
     */
17
    protected $order;
18
19
    /**
20
     *NOTE: we semi-enforce using the OrderModifier_Controller here to deal with the submission of the OrderModifierForm
21
     * You can use your own modifiers or an extension of OrderModifier_Controller by setting the first parameter (optionalController)
22
     * to your own controller.
23
     *
24
     *@param $optionalController Controller
25
     *@param $name String
26
     *@param $fields FieldList
27
     *@param $actions FieldList
28
     *@param $validator SS_Validator
29
     **/
30
    public function __construct(
31
        Controller $optionalController = null,
32
        $name,
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
33
        FieldList $fields,
34
        FieldList $actions,
35
        Validator $optionalValidator = null
36
    ) {
37
        if (!$optionalController) {
38
            $controllerClassName = EcommerceConfig::get('OrderModifierForm', 'controller_class');
39
            $optionalController = new $controllerClassName();
40
        }
41
        if (!$optionalValidator) {
42
            $validatorClassName = EcommerceConfig::get('OrderModifierForm', 'validator_class');
43
            $optionalValidator = new $validatorClassName();
44
        }
45
        parent::__construct($optionalController, $name, $fields, $actions, $optionalValidator);
46
47
        //extension point
48
        $this->extend('updateFields', $fields);
49
        $this->setFields($fields);
50
        $this->extend('updateActions', $actions);
51
        $this->setActions($actions);
52
        $this->extend('updateValidator', $optionalValidator);
53
        $this->setValidator($optionalValidator);
54
55
        $this->setAttribute('autocomplete', 'off');
56
        Requirements::themedCSS($this->ClassName, 'ecommerce');
0 ignored issues
show
Documentation introduced by
The property ClassName does not exist on object<OrderModifierForm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
        $this->addExtraClass($this->myLcFirst(ucwords($name)));
0 ignored issues
show
Documentation introduced by
$this->myLcFirst(ucwords($name)) is of type null|array<integer,string,{"0":"string"}>, but the function expects a string.

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...
58
        Requirements::javascript(THIRDPARTY_DIR.'/jquery-form/jquery.form.js');
59
        //add JS for the modifier - added in modifier
60
        $oldData = Session::get("FormInfo.{$this->FormName()}.data");
61
        if ($oldData && (is_array($oldData) || is_object($oldData))) {
62
            $this->loadDataFrom($oldData);
0 ignored issues
show
Bug introduced by
It seems like $oldData can also be of type object; however, Form::loadDataFrom() does only seem to accept array|object<DataObject>, 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...
63
        }
64
        $this->extend('updateOrderModifierForm', $this);
65
    }
66
67
    protected function myLcFirst($str)
68
    {
69
        if (function_exists('lcfirst') === false) {
70
            function lcfirst($str)
71
            {
72
                $str[0] = strtolower($str[0]);
73
74
                return $str;
75
            }
76
        } else {
77
            return lcfirst($str);
78
        }
79
    }
80
81
    /**
82
     * @param string $status
83
     * @param string $message
84
     */
85
    public function redirect($status = 'success', $message = '')
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        //return ShoppingCart::singleton()->addmessage($status, $message);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
88
    }
89
90
    /**
91
     * @param array  $data
92
     * @param Form   $form
93
     * @param string $status
94
     * @param string $message
95
     *
96
     * @return ShoppingCart Response (JSON / Redirect Back)
97
     */
98
    public function submit(array $data, Form $form, $message = 'order updated', $status = 'good')
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        //to do - add other checks here...
101
         return ShoppingCart::singleton()->setMessageAndReturn($message, $status);
102
    }
103
104
    /**
105
     * saves the form into session.
106
     *
107
     * @param array $data - data from form.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
108
     */
109
    public function saveDataToSession()
110
    {
111
        $data = $this->getData();
112
        Session::set("FormInfo.{$this->FormName()}.data", $data);
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a string.

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...
113
    }
114
}
115