updateItemRequestClass()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 5
1
<?php
2
3
namespace SilverStripe\GridFieldAddOns;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
7
8
/**
9
 * Allow DataObjects to specify a custom GridFieldDetailForm_ItemRequest class
10
 * via SilverStripe config.
11
 *
12
 * This class will then be used to build the GridFieldDetailForm automatically
13
 */
14
class GridFieldDetailFormExtension extends Extension
15
{
16
17
    /**
18
     * @param string $class
19
     * @param GridField $gridField
20
     * @param DataObject $record
21
     * @param RequestHandler $requestHandler
22
     * @param string $assignedClass Name of class explicitly assigned to this component
0 ignored issues
show
Documentation introduced by
Should the type for parameter $assignedClass not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     */
24
    public function updateItemRequestClass(&$class, $gridField, $record, $requestHandler, $assignedClass = null)
0 ignored issues
show
Unused Code introduced by
The parameter $gridField is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $requestHandler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        // Avoid overriding explicitly assigned class name if set using setItemRequestClass()
27
        if ($assignedClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $assignedClass of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            return;
29
        }
30
31
        $custom = $record->config()->get('gridfield_request_class');
32
33
        // if custom is a valid class, switch item request class
34
        if (isset($custom)
35
            && is_subclass_of($custom, GridFieldDetailForm_ItemRequest::class)
36
        ) {
37
            $class = $custom;
38
        }
39
    }
40
}
41