InuitForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A perform_inuit_fieldlist_transformation() 0 4 1
A performInuitTransformation() 0 5 1
A performInuitFieldListTransformation() 0 6 1
A forTemplate() 0 6 1
1
<?php namespace StudioBonito\SilverStripe\Inuit\Forms;
2
3
use FieldList;
4
5
/**
6
 * A base form class that transforms all fields to use inuit style extensions.
7
 *
8
 * @author       Tom Densham <[email protected]>
9
 * @copyright    Studio Bonito Ltd.
10
 */
11
class InuitForm extends \Form
12
{
13
    /**
14
     * Set the forms template.
15
     *
16
     * @var string
17
     */
18
    protected $template = 'InuitForm';
19
20
    /**
21
     * Transform all the {@link \FormField} objects in
22
     * the {@link \FieldList} to use inuit form templates.
23
     *
24
     * @param \FieldList $fields
25
     */
26
    public static function perform_inuit_fieldlist_transformation(FieldList $fields)
27
    {
28
        $fields->performInuitTransformation();
0 ignored issues
show
Bug introduced by
The method performInuitTransformation() does not exist on FieldList. Did you maybe mean transform()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
29
    }
30
31
    /**
32
     * Transform all the {@link \FormField} objects in
33
     * the {@link \Form} field and action {@link \FieldList}
34
     * objects to use inuit form templates.
35
     */
36
    public function performInuitTransformation()
37
    {
38
        $this->performInuitFieldListTransformation($this->Fields());
0 ignored issues
show
Bug introduced by
It seems like $this->Fields() can be null; however, performInuitFieldListTransformation() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
        $this->performInuitFieldListTransformation($this->Actions());
0 ignored issues
show
Bug introduced by
It seems like $this->Actions() can be null; however, performInuitFieldListTransformation() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
    }
41
42
    /**
43
     * Transform all the {@link \FormField} objects in
44
     * the {@link \FieldList} to use inuit form templates.
45
     *
46
     * @param \FieldList $fields
47
     *
48
     * @return $this
49
     */
50
    protected function performInuitFieldListTransformation(FieldList $fields)
51
    {
52
        self::perform_inuit_fieldlist_transformation($fields);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Apply the transformation before rendering the form HTML output.
59
     *
60
     * @return mixed
61
     */
62
    public function forTemplate()
63
    {
64
        $this->performInuitTransformation();
65
66
        return parent::forTemplate();
67
    }
68
}