Completed
Push — master ( 92d109...8f13c5 )
by
unknown
03:44
created

OrderForm_Feedback   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 36 5
B dofeedback() 0 25 2
A saveDataToSession() 0 5 1
B getValueFromOrderConfirmationPage() 0 12 5
A getOrderConfirmationPage() 0 6 2
1
<?php
2
3
4
class OrderForm_Feedback 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...
5
{
6
7
    protected $order = null;
8
9
    public function __construct(Controller $controller, $name, Order $order)
10
    {
11
        $this->order = $order;
12
        $values = $this->getValueFromOrderConfirmationPage('FeedbackValuesOptions');
13
        $values = explode(',', $values);
14
        $newValues = array();
15
        foreach($values as $value) {
16
            $value = trim($value);
17
            $newValues[Convert::raw2att($value)] = $value;
18
        }
19
        $fields = FieldList::create(
20
            array(
21
                HeaderField::create('FeedbackHeading', $this->getValueFromOrderConfirmationPage('FeedbackHeader'), 3),
22
                OptionsetField::create(
23
                    'Rating',
24
                    $this->getValueFromOrderConfirmationPage('FeedbackValuesFieldLabel'),
25
                    $newValues
26
                ),
27
                TextareaField::create('Note', $this->getValueFromOrderConfirmationPage('FeedbackNotesFieldLabel'))
28
            )
29
        );
30
        $actions = FieldList::create(
31
            FormAction::create('dofeedback', $this->getValueFromOrderConfirmationPage('FeedbackFormSubmitLabel'))
32
        );
33
        $requiredFields = array(
34
            'FeedbackValue'
35
        );
36
        $validator = OrderForm_Feedback_Validator::create($requiredFields);
37
        parent::__construct($controller, $name, $fields, $actions, $validator);
38
39
        $oldData = Session::get("FormInfo.{$this->FormName()}.data");
40
        if ($oldData && (is_array($oldData) || is_object($oldData))) {
41
            $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...
42
        }
43
        $this->extend('updateOrderForm_Feedback', $this);
44
    }
45
46
    /**
47
     * @param array        $data The form request data submitted
48
     * @param Form         $form The {@link Form} this was submitted on
49
     * @param HTTPRequest  $request The {@link Form} this was submitted on
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be SS_HTTPRequest?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
     */
51
    public function dofeedback(array $data, Form $form, SS_HTTPRequest $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...
Unused Code introduced by
The parameter $request 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...
52
    {
53
        $SQLData = Convert::raw2sql($data);
54
        if ($this->order) {
55
            $object = OrderFeedback::create($SQLData);
56
            $object->OrderID = $this->order->ID;
0 ignored issues
show
Documentation introduced by
The property OrderID does not exist on object<OrderFeedback>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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
            $objet->write();
0 ignored issues
show
Bug introduced by
The variable $objet does not exist. Did you mean $object?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
58
            $form->sessionMessage(
59
                $this->getValueFromOrderConfirmationPage('FeedbackFormThankYou'),
60
                'good'
61
            );
62
63
            return $this->controller->redirectBack();
64
        }
65
        $form->sessionMessage(
66
            _t(
67
                'OrderForm_Feedback.COULD_NOT_RECORD_FEEDBACK',
68
                'Sorry, order feedback could not be recorded.'
69
            ),
70
            'bad'
71
        );
72
73
        return $this->controller->redirectBack();
74
75
    }
76
77
    /**
78
     * saves the form into session.
79
     *
80
     * @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...
81
     */
82
    public function saveDataToSession()
83
    {
84
        $data = $this->getData();
85
        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...
86
    }
87
88
    protected function getValueFromOrderConfirmationPage($value)
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...
89
    {
90
        if($page = $this->getOrderConfirmationPage()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $page is correct as $this->getOrderConfirmationPage() (which targets OrderForm_Feedback::getOrderConfirmationPage()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
            return $page->$value;
92
        } else {
93
            $defaults = Config::inst()->get('OrderConfirmationPage', 'defaults');
94
            if($defaults && is_array($defaults) && isset($defaults[$value])) {
95
                return $defaults[$value];
96
            }
97
            return _t('OrderForm_Feedback.'.$value, 'OrderForm_Feedback.'.$value.' value not set in translations');
98
        }
99
    }
100
101
    protected $_orderConfirmationPage = null;
102
103
    protected function getOrderConfirmationPage()
104
    {
105
        if(! $this->_orderConfirmationPage) {
106
            $this->_orderConfirmationPage = OrderConfirmationPage::get()->first();
107
        }
108
    }
109
110
}
111