Completed
Push — master ( 90a411...a748b4 )
by Simon
05:01
created

UserDefinedFormExtension::updateCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 22
nc 1
nop 1
1
<?php
2
3
namespace Firesphere\PartialUserforms\Extensions;
4
5
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
10
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
11
use SilverStripe\Forms\Tab;
12
use SilverStripe\ORM\DataExtension;
13
14
/**
15
 * Class \Firesphere\PartialUserforms\Extensions\UserDefinedFormExtension
16
 *
17
 * @property \DNADesign\ElementalUserForms\Model\ElementForm|\SilverStripe\UserForms\Model\UserDefinedForm|\Firesphere\PartialUserforms\Extensions\UserDefinedFormExtension $owner
18
 * @method \SilverStripe\ORM\DataList|\Firesphere\PartialUserforms\Models\PartialFormSubmission[] PartialSubmissions()
19
 */
20
class UserDefinedFormExtension extends DataExtension
21
{
22
23
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'ExportPartialSubmissions' => 'Boolean(true)',
25
    ];
26
    /**
27
     * @var array
28
     */
29
    private static $has_many = [
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        'PartialSubmissions' => PartialFormSubmission::class
31
    ];
32
33
    /**
34
     * @param FieldList $fields
35
     */
36
    public function updateCMSFields(FieldList $fields)
37
    {
38
        /** @var GridField $submissionField */
39
        $submissionField = $fields->dataFieldByName('Submissions');
40
        $list = $submissionField->getList()->exclude(['ClassName' => PartialFormSubmission::class]);
41
        $submissionField->setList($list);
42
        $fields->removeByName('PartialSubmissions');
43
        /** @var GridFieldConfig_RelationEditor $gridfieldConfig */
44
        $gridfieldConfig = GridFieldConfig_RelationEditor::create();
45
        $gridfieldConfig->removeComponentsByType(GridFieldAddNewButton::class);
46
47
        // We need to manually add the tab
48
        $fields->addFieldToTab(
49
            'Root',
50
            Tab::create('PartialSubmissions', _t(__CLASS__ . '.PartialSubmission', 'Partial submissions'))
51
        );
52
53
        $fields->addFieldToTab(
54
            'Root.PartialSubmissions',
55
            GridField::create(
56
                'PartialSubmissions',
57
                _t(__CLASS__ . '.PartialSubmission', 'Partial submissions'),
58
                $this->owner->PartialSubmissions()
0 ignored issues
show
Bug introduced by
The method PartialSubmissions does only exist in Firesphere\PartialUserfo...serDefinedFormExtension, but not in SilverStripe\UserForms\Model\UserDefinedForm.

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...
59
            )
60
        );
61
62
        $fields->insertAfter('DisableSaveSubmissions',
63
            $partialCheckbox = CheckboxField::create('ExportPartialSubmissions',
64
                _t(__CLASS__ . '.partialCheckboxLabel', 'Send partial submissions'))
65
        );
66
        $description = _t(__CLASS__ . '.partialCheckboxDescription',
67
            'The configuration and global export configuration can be set in the site Settings');
68
        $partialCheckbox->setDescription($description);
69
    }
70
}
71