Completed
Pull Request — master (#31)
by Lhalaa
04:10
created

UserDefinedFormControllerExtension::onAfterInit()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 10
cp 0.7
rs 9.0777
c 0
b 0
f 0
cc 6
nc 4
nop 0
crap 6.972
1
<?php
2
3
namespace Firesphere\PartialUserforms\Extensions;
4
5
use Firesphere\PartialUserforms\Controllers\PartialSubmissionController;
6
use SilverStripe\Control\NullHTTPRequest;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\UserForms\Control\UserDefinedFormController;
9
use SilverStripe\View\Requirements;
10
11
/**
12
 * Class UserDefinedFormControllerExtension
13
 *
14
 * @package Firesphere\PartialUserforms\Extensions
15
 * @property UserDefinedFormController|UserDefinedFormControllerExtension $owner
16
 */
17
class UserDefinedFormControllerExtension extends Extension
18
{
19
    /**
20
     * Add required javascripts
21
     */
22 6
    public function onBeforeInit()
23
    {
24 6
        Requirements::javascript('firesphere/partialuserforms:client/dist/main.js');
25 6
    }
26
27
    /**
28
     * Start a clean session if the user visits the original form
29
     */
30 5
    public function onAfterInit()
31
    {
32 5
        $request = $this->owner->getRequest();
0 ignored issues
show
Bug introduced by
The method getRequest does only exist in SilverStripe\UserForms\C...erDefinedFormController, but not in Firesphere\PartialUserfo...FormControllerExtension.

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...
33 5
        if ($request instanceof NullHTTPRequest) {
34 2
            return;
35
        }
36
37 5
        $url = $this->owner->getRequest()->getURL();
38
        // This should never run on the 'partial' or 'ping' URL
39 5
        if (strpos($url, 'partial') === false && strpos($url, 'ping') === false) {
40
            // Clear session on start
41
            $session = $this->owner->getRequest()->getSession();
42
            if ($session && $session->get(PartialSubmissionController::SESSION_KEY)) {
43
                $session->clear(PartialSubmissionController::SESSION_KEY);
44
            }
45
        }
46 5
    }
47
}
48