DiffTransformation   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 0 37 8
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Forms;
4
5
use BadMethodCallException;
6
use SilverStripe\Forms\CompositeField;
7
use SilverStripe\Forms\FormField;
8
use SilverStripe\Forms\FormTransformation;
9
use SilverStripe\Forms\LiteralField;
10
11
class DiffTransformation extends FormTransformation
12
{
13
    public function transform(FormField $field)
14
    {
15
        if ($field->isComposite()) {
16
            /** @var CompositeField $field */
17
            // There isn't an interface for child fields so this is unfortunately just a best guess.
18
            $newKids = $field->getChildren()->transform($this);
19
            $newField = clone $field;
20
            $newField->setChildren($newKids);
21
            return $newField;
22
        }
23
24
        if (!$field->hasData()) {
25
            // No data; no value.
26
            return clone $field;
27
        }
28
29
        $name = $field->getName();
30
31
        // Do not compare generated security data
32
        if (
33
            ($form = $field->getForm())
34
            && ($securityToken = $form->getSecurityToken())
35
            && ($securityTokenName = $securityToken->getName())
36
            && $securityTokenName === $name
37
        ) {
38
            return LiteralField::create($name, '');
39
        }
40
41
        try {
42
            // First check if a field implements performDiffTransformation()
43
            $diffField = parent::transform($field);
44
        } catch (BadMethodCallException $e) {
45
            $diffField = $field->castedCopy(DiffField::class);
46
            $diffField->addExtraClass("history-viewer__version-detail-diff");
47
            $diffField->setComparisonField($field);
48
        }
49
        return $diffField;
50
    }
51
}
52