Completed
Push — master ( 15fcad...6aa080 )
by Nicolaas
02:08
created

CMSEditLinkAPI   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 65
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C find_edit_link_for_object() 0 44 13
1
<?php
2
3
4
class CMSEditLinkAPI 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...
5
{
6
7
    private static $_cache = array();
8
9
    /**
10
     * common usage ...
11
     *
12
     *         public function CMSEditLink($action = null)
13
     *         {
14
     *             return CMSEditLinkAPI::find_edit_link_for_object($this, $action);
15
     *         };
16
     *
17
     * @param  DataObject | string $objectOrClassName
18
     * @param  string              $action
0 ignored issues
show
Documentation introduced by
Should the type for parameter $action 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...
19
     *
20
     * @return string
21
     */
22
    public static function find_edit_link_for_object($objectOrClassName, $action = null)
23
    {
24
        if($objectOrClassName instanceof DataObject && $objectOrClassName->exists()) {
25
            $modelName = $objectOrClassName->ClassName;
26
            $id = $objectOrClassName->ID;
27
        } else {
28
            $modelName = $objectOrClassName;
29
            $objectOrClassName = Injector::inst()->get($modelName);
0 ignored issues
show
Unused Code introduced by
$objectOrClassName 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
            $id = 0;
31
        }
32
        $key = $modelName.'_'.$action;
33
        if(!isset(self::$_cache[$key])) {
34
            foreach(ClassInfo::subclassesFor('ModelAdmin') as $i => $class) {
0 ignored issues
show
Bug introduced by
The expression \ClassInfo::subclassesFor('ModelAdmin') of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
35
                if($class == 'ModelAdmin') {continue;}
36
                if(ClassInfo::classImplements($class, 'TestOnly')) {continue;}
37
                $myAdminClassName = $class;
38
                $modelAdminclassObject = Injector::inst()->get($myAdminClassName);
39
                $models = $modelAdminclassObject->getManagedModels();
40
                foreach($models as $key => $model) {
41
                    if($key === $modelName || $model === $modelName) {
42
                        $myManagerClass = $class;
43
                        $myManagerObject = $modelAdminclassObject;
44
                        break 2;
45
                    }
46
                }
47
            }
48
            if(isset($myManagerClass) && isset($myManagerObject)) {
49
                if(!$action) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|null is loosely compared to false; 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...
50
                    $action = $modelName.'/EditForm/field/'.$modelName.'/item/0/';
51
                    self::$_cache[$key] = Controller::join_links(
52
                        Director::baseURL(),
53
                        $myManagerObject->Link($action)
54
                    );
55
                } else {
56
                    return Controller::join_links(
57
                        Director::baseURL(),
58
                        $myManagerObject->Link($action)
59
                    );
60
                }
61
            }
62
        }
63
64
        return str_replace('item/0/', 'item/'.$id.'/', self::$_cache[$key]);
65
    }
66
67
68
}
69