Completed
Push — master ( e67fa1...90cabd )
by Nicolaas
01:26
created

ImagesWithStyleCMSAPI   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 75
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B add_links_to_folder_field() 0 21 6
A folder_change_start() 6 10 3
A folder_change_end() 0 9 1
C check_for_folder_changes_and_migrate_images() 0 26 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
5
class ImagesWithStyleCMSAPI 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...
6
{
7
    public static function add_links_to_folder_field($formField, $folderOrImagesWithStyleList)
8
    {
9
        $folder = null;
10
        if ($folderOrImagesWithStyleList instanceof ImagesWithStyleSelection) {
11
            if ($folderOrImagesWithStyleList->PlaceToStoreImagesID) {
0 ignored issues
show
Documentation introduced by
The property PlaceToStoreImagesID does not exist on object<ImagesWithStyleSelection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
12
                $folder = $folderOrImagesWithStyleList->PlaceToStoreImages();
0 ignored issues
show
Documentation Bug introduced by
The method PlaceToStoreImages does not exist on object<ImagesWithStyleSelection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
13
            }
14
        } elseif ($folderOrImagesWithStyleList instanceof Folder) {
15
            $folder = $folderOrImagesWithStyleList;
16
        } else {
17
            user_error('folderOrImagesWithStyleList param needs to be Folder or ImagesWithStyleSelection');
18
        }
19
        if ($folder && $folder->exists()) {
20
            $rightFieldTitle = $formField->RightTitle();
21
            $rightFieldTitle .= '
22
                <h5>Quick Links</h5>
23
                You can <a href="/admin/assets/add/?ID='.$folder->ID.'" target="_blank">add images directly</a> to the <a href="/admin/assets/show/'.$folder->ID.'/" target="_blank">folder</a>.
24
                <br /><strong>To see the latest updates to this folder, you may need to click save below.</strong>';
25
            $formField->setRightTitle($rightFieldTitle);
26
        }
27
    }
28
29
    private static $_folder_cache = [];
30
31
    public static function folder_change_start($object, $key, $folderName)
32
    {
33 View Code Duplication
        if (!isset(self::$_folder_cache[$object->ClassName.'_'.$object->ID])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            self::$_folder_cache[$object->ClassName.'_'.$object->ID] = [];
35
        }
36 View Code Duplication
        if (!isset(self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key] = [];
38
        }
39
        self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key]['before'] = $folderName;
40
    }
41
42
    public static function folder_change_end($object, $key, $folderName, $list = null)
43
    {
44
        self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key]['after'] = $folderName;
45
        self::check_for_folder_changes_and_migrate_images(
46
            self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key],
47
            $list
48
        );
49
        self::$_folder_cache[$object->ClassName.'_'.$object->ID][$key]['before'] = $folderName;
50
    }
51
52
53
    protected static function check_for_folder_changes_and_migrate_images($keyDetail, $list)
54
    {
55
        if ($keyDetail['before'] !== $keyDetail['after']) {
56
            $oldFolder = Folder::find_or_make($keyDetail['before']);
57
            $newFolder = Folder::find_or_make($keyDetail['after']);
58
            $oldImages = Image::get()->filter(['ParentID' => $oldFolder->ID]);
59
            if ($newFolder && $newFolder->exists()) {
60
                if ($list && $list->exists()) {
61
                    $list->PlaceToStoreImagesID = $newFolder->ID;
62
                    $list->write();
63
                }
64
                if ($oldFolder && $oldFolder->exists()) {
65
                    if ($oldImages && $oldImages->count()) {
66
                        foreach ($oldImages as $oldImage) {
67
                            $oldImage->ParentID = $newFolder->ID;
68
                            $oldImage->write();
69
                        }
70
                        $oldFilesAny = File::get()->filter(['ParentID' => $oldFolder->ID]);
71
                        if ($oldFilesAny->count()) {
72
                            $oldFilesAny->delete();
73
                        }
74
                    }
75
                }
76
            }
77
        }
78
    }
79
}
80