CallToActionPageExtension::updateCMSFields()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
cc 5
eloc 30
nc 6
nop 1
1
<?php
2
3
/**
4
 *@author nicolaas [at] sunnysideup.co.nz
5
 *
6
 *
7
 **/
8
9
class CallToActionPageExtension extends DataExtension
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...
10
{
11
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one 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...
12
        'CallToAction' => 'CallToAction'
13
    ];
14
15
    private static $field_labels = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels 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...
16
        'CallToAction' => 'Call to action'
17
    ];
18
19
    private static $field_labels_right = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right 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...
20
        'CallToAction' => 'Big image with optional text'
21
    ];
22
23
    public function updateCMSFields(FieldList $fields)
24
    {
25
        $fieldLabels = $this->owner->FieldLabels();
0 ignored issues
show
Unused Code introduced by
$fieldLabels is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $callToAction = CallToAction::singleton();
27
        $fieldLabelsRight = Config::inst()->get('CallToActionPageExtension', 'field_labels_right');
0 ignored issues
show
Unused Code introduced by
$fieldLabelsRight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
        $tabTitle0 = $callToAction->i18n_singular_name();
29
        $link1 = '/admin/calltoaction/';
30
        $tabTitle1 = _t('CallToActionPageExtension.CREATE_NEW', 'view all');
31
        if ($this->owner->CallToActionID) {
32
            $callToAction = $this->owner->CallToAction();
33
            if ($callToAction && $callToAction->exists()) {
34
                $link2 = $this->owner->CallToAction()->CMSEditLink();
35
                $tabTitle2 = _t('CallToActionPageExtension.EDIT_CURRENT', 'edit').' '.$callToAction->getTitle();
36
            }
37
        }
38
        if (! isset($link2)) {
39
            $link2 = $callToAction->CMSAddLink();
40
            $tabTitle2 = _t('CallToActionPageExtension.CREATE_NEW', 'create new').' '.$callToAction->i18n_singular_name();
41
        }
42
        $fields->addFieldsToTab(
43
            'Root.'.$tabTitle0,
44
            [
45
                DropdownField::create(
46
                    'CallToActionID',
47
                    _t('CallToActionPageExtension.SELECT', 'select'),
48
                    [0 => _t('CallToActionPageExtension.PLEASE_SELECT', '-- please select --')] +CallToAction::get()->map()->toArray()
49
                ),
50
                LiteralField::create(
51
                    'CallToActionList',
52
                    '<h2>
53
54
                        <a href="'.$link1.'" target="_blank">'.$tabTitle1.'</a> |
55
                        <a href="'.$link2.'" target="_blank">'.$tabTitle2.'</a>
0 ignored issues
show
Bug introduced by
The variable $tabTitle2 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
                    <h2>'
57
                ),
58
                LiteralField::create(
59
                    'CallToActionEdit',
60
                    ''
61
                )
62
            ]
63
        );
64
        return $fields;
65
    }
66
}
67