LinkThroughInCMS::add_links_to_fields()   C
last analyzed

Complexity

Conditions 15
Paths 12

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 5.9385
c 0
b 0
f 0
cc 15
eloc 35
nc 12
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class LinkThroughInCMS extends Object
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...
4
{
5
    private static $excluded_has_one_classes = [
0 ignored issues
show
Unused Code introduced by
The property $excluded_has_one_classes 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...
6
        "Image",
7
        "File"
8
    ];
9
10
    private static $completion_array = [];
11
12
    private static $tab_name_for_link = "Root.Links";
0 ignored issues
show
Unused Code introduced by
The property $tab_name_for_link 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...
13
14
    private static $tab_name_for_calc = "Root.Main";
0 ignored issues
show
Unused Code introduced by
The property $tab_name_for_calc 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...
15
16
    private static $heading_for_casted_variables = "Calculated Values";
0 ignored issues
show
Unused Code introduced by
The property $heading_for_casted_variables 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...
17
18
    private static $excluded_casted_variables = [
0 ignored issues
show
Unused Code introduced by
The property $excluded_casted_variables 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...
19
        "Title",
20
        "CSSClasses"
21
    ];
22
23
    public static function add_links_to_fields($obj, $fields)
24
    {
25
        if (!isset(self::$completion_array[$obj->ClassName])) {
26
            self::$completion_array[$obj->ClassName] = true;
27
            if ($obj->exists()) {
28
                $fieldLabels = array_merge($obj->FieldLabels(), (array)$obj->Config()->get("field_labels"));
29
                $hasOneLinks = $obj->Config()->get("has_one");
30
                if (is_array($hasOneLinks) && count($hasOneLinks)) {
31
                    $linkTabName = Config::inst()->get("LinkThroughInCMS", "tab_name_for_link");
32
                    $fields->addFieldToTab($linkTabName, new LiteralField("YouAreLookingAt", '<h2>This objects links to ...</h2>'));
33
                    foreach ($hasOneLinks as $methodName => $className) {
34
                        $dbFieldName = $methodName."ID";
35
                        $label = isset($fieldLabels[$methodName]) ? $fieldLabels[$methodName] : null;
36
                        if ($obj->$dbFieldName && $relationObject = $obj->$methodName()) {
37
                            if (! in_array($relationObject->ClassName, Config::inst()->get("LinkThroughInCMS", "excluded_has_one_classes"))) {
38
                                $fields->addFieldToTab(
39
                                    $linkTabName,
40
                                    LiteralField::create(
41
                                        "LinkThroughFor".$relationObject->ClassName.$relationObject->ID,
42
                                        '<h3><a href="'.$relationObject->CMSEditLink().'" target="_blank">open related '.$label.'</a></h3>'
43
                                    )
44
                                );
45
                            }
46
                        }
47
                    }
48
                }
49
                $castedVariables = $obj->Config()->get("casting");
50
                if (is_array($castedVariables) && count($castedVariables)) {
51
                    $calcTabName = Config::inst()->get("LinkThroughInCMS", "tab_name_for_calc");
52
                    $fields->addFieldToTab(
53
                        $calcTabName,
54
                        LiteralField::create(
55
                            "CalculatedValuesHeader",
56
                            '<h2>'.Config::inst()->get("LinkThroughInCMS", "heading_for_casted_variables").'</h2>'
57
                        )
58
                    );
59
                    foreach ($castedVariables as $castedVariableName => $castedVariableType) {
60
                        if (!in_array($castedVariableName, Config::inst()->get("LinkThroughInCMS", "excluded_casted_variables"))) {
61
                            $label = isset($fieldLabels[$castedVariableName]) ? $fieldLabels[$castedVariableName] : null;
62
                            $fields->addFieldToTab(
63
                                $calcTabName,
64
                                ReadonlyField::create(
65
                                    $castedVariableName,
66
                                    $label
67
                                )
68
                            );
69
                        }
70
                    }
71
                }
72
            }
73
        }
74
    }
75
}
76