HasManyGridFieldHack   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateItemEditForm() 0 17 4
1
<?php
2
3
/**
4
 * this->owner mother of all ugly hacks ensures
5
 * that if you open a Has Many Entry from an Has One Parent Data Object
6
 * in modeladmin (e.g. adding a city to a country) that the
7
 * city field is pre-completed.
8
 *
9
 *
10
 *
11
 *
12
 */
13
14
class HasManyGridFieldHack_HasManyList extends Extension
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...
15
{
16
    public function getForeignKey()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
17
    {
18
        return $this->owner->foreignKey;
19
    }
20
}
21
22
class HasManyGridFieldHack extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
23
{
24
    public function updateItemEditForm($form)
25
    {
26
        $list = $this->owner->gridField->getList();
27
        if ($list && $list instanceof HasManyList) {
28
            $foreignKey = $list->getForeignKey();
29
            $dataClass = $list->dataClass();
0 ignored issues
show
Unused Code introduced by
$dataClass 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...
30
            $dataQuery = $list->dataQuery();
0 ignored issues
show
Unused Code introduced by
$dataQuery 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...
31
            $foreignID = $list->getForeignID();
32
            $fields = $form->Fields();
33
            $field = $fields->dataFieldByName($foreignKey);
34
            if ($field) {
35
                $field->setValue($foreignID);
36
                $fields->replaceField($field->Name, $field->performDisabledTransformation());
37
            }
38
            $this->owner->record->$foreignKey = $foreignID;
39
        }
40
    }
41
}
42