Passed
Pull Request — master (#224)
by
unknown
02:40
created

performReadonlyTransformation()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 31
nc 1
nop 0
dl 0
loc 51
rs 8.8981
c 0
b 0
f 0

How to fix   Long Method   

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
namespace DNADesign\Elemental\Forms;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\CompositeField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\TabSet;
10
use SilverStripe\Forms\ReadonlyTransformation;
11
12
class ElementalAreaField extends GridField
13
{
14
    public function performReadonlyTransformation()
15
    {
16
        $readOnlyField = $this->castedCopy(CompositeField::class);
17
        
18
        $blockReducer = function ($element) {
19
            $parentName = 'Element' . $element->ID;
20
            $elementFields = $element->getCMSFields();
21
            // Obtain highest impact fields for a summary (e.g. Title & Content)
22
            foreach ($elementFields as $field) {
23
                if (is_object($field) && $field instanceof TabSet) {
24
                    // Assign the fields of the first Tab in the TabSet - most regularly 'Root.Main'
25
                    $elementFields = $field->FieldList()->first()->FieldList();
26
                    break;
27
                }
28
            }
29
            // Set values (before names don't match anymore)
30
            $elementFields->setValues($element->getQueriedDatabaseFields());
31
            // Ensure field names are unique between elements on parent form
32
            $elementFields->recursiveWalk(function ($field) use ($parentName) {
33
                $field->setName($parentName . '_' . $field->getName());
34
            });
35
            // Combine into an appropriately named group
36
            $elementGroup = FieldGroup::create($elementFields);
37
            $elementGroup->setName($parentName);
38
            $elementGroup->addExtraClass('elementalarea__element--historic');
39
            // Also set the important data for the rendering Component
40
            // $elementGroup->setSchemaComponent('HistoricElementView');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
            $elementGroup->setSchemaData([
42
                'data' => [
43
                    'ElementID' => $element->ID,
44
                    'ElementType' => $element->getType(),
45
                    'ElementIcon' => $element->config()->icon,
46
                    'ElementTitle' => $element->Title,
47
                    'ElementEditLink' => $element->CMSEditLink(),
48
                    'extraContext' => 'HistoricElementView'
49
                ]
50
            ]);
51
            
52
            return $elementGroup;
53
        };
54
        
55
        $readOnlyField->setChildren(
56
            FieldList::create(
57
                array_map($blockReducer, $this->getList()->toArray())
58
            )
59
        );
60
        $readOnlyField = $readOnlyField->transform(new ReadonlyTransformation());
61
        $readOnlyField->setReadOnly(true);
62
        $readOnlyField->setName($this->getName());
63
        $readOnlyField->addExtraClass('elementalarea--read-only');
64
        return $readOnlyField;
65
    }
66
}
67