DataIntegrityTestYML   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 27.18 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 5
dl 28
loc 103
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 28 68 17

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
class DataIntegrityTestYML extends BuildTask
5
{
6
7
    /**
8
     * list of files you want to check
9
     * @var array
10
     */
11
    private static $config_files = array("mysite/_config/config.yml");
12
13
    /**
14
     * list of classes that do not need to be checked
15
     * NB: they are all lowercase, as we test for them only lowercase!
16
     * @var array
17
     */
18
    private static $classes_to_skip = array("name", "before", "only", "after");
19
20
    /**
21
     * list of variables that do not need checking...
22
     * @var array
23
     */
24
    private static $variables_to_skip = array();
25
26
    /**
27
     * standard SS variable
28
     * @var String
29
     */
30
    protected $title = "Check your config files for rogue entries.";
31
32
    /**
33
     * standard SS variable
34
     * @var String
35
     */
36
    protected $description = "Checks a selection of yml files to see if there are any entries that may be incorrect.";
37
38
    public function run($request)
39
    {
40
        ini_set('max_execution_time', 3000);
41
        require_once 'thirdparty/spyc/spyc.php';
42
        $filesArray = Config::inst()->get("DataIntegrityTestYML", "config_files");
43
        $classesToSkip = Config::inst()->get("DataIntegrityTestYML", "classes_to_skip");
44
        $variablesToSkip = Config::inst()->get("DataIntegrityTestYML", "variables_to_skip");
45
        foreach ($filesArray as $folderAndFileLocation) {
0 ignored issues
show
Bug introduced by
The expression $filesArray of type array|integer|double|string|boolean 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...
46
            db::alteration_message("<h2>Checking $folderAndFileLocation</h2>");
47
            $fixtureFolderAndFile = Director::baseFolder().'/'. $folderAndFileLocation;
48
            if (!file_exists($fixtureFolderAndFile)) {
49
                user_error('No custom configuration has been setup here : "' . $fixtureFolderAndFile . '" set the files here: DataIntegrityTestYML::config_files', E_USER_NOTICE);
50
            }
51
            $parser = new Spyc();
52
            $arrayOfSettings = $parser->loadFile($fixtureFolderAndFile);
53
            foreach ($arrayOfSettings as $className => $variables) {
54
                if (in_array(strtolower($className), $classesToSkip)) {
55
                    db::alteration_message("$className : skipped");
56
                } else {
57
                    echo "<br /><br />";
58
                    if (!class_exists($className)) {
59
                        db::alteration_message("$className does not exist", "deleted");
60
                    } else {
61
                        db::alteration_message("$className", "created");
62
                        foreach ($variables as $variable => $setting) {
63
                            if ($variable == "icon") {
64
                                $fileLocationForOthers = Director::baseFolder().'/'.$setting;
65
                                $fileLocationForSiteTree = Director::baseFolder().'/'.$setting.'-file.gif';
66
                                if ($className::create() instanceof SiteTree) {
67 View Code Duplication
                                    if (!file_exists($fileLocationForSiteTree)) {
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...
68
                                        db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> icon $fileLocationForSiteTree can not be found", "deleted");
69
                                    } else {
70
                                        db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> icon $fileLocationForSiteTree exists", "created");
71
                                    }
72 View Code Duplication
                                } else {
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...
73
                                    if (!file_exists($fileLocationForOthers)) {
74
                                        db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> icon $fileLocationForOthers can not be found", "deleted");
75
                                    } else {
76
                                        db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> icon $fileLocationForOthers exists", "created");
77
                                    }
78
                                }
79
                            } elseif ($variable == "extensions") {
80
                                if (!is_array($setting)) {
81
                                    db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> extensions should be set as an array.", "deleted");
82 View Code Duplication
                                } else {
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...
83
                                    foreach ($setting as $extensionClassName) {
84
                                        if (!class_exists($extensionClassName)) {
85
                                            db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> extension class <u>$extensionClassName</u> does not exist", "deleted");
86
                                        } else {
87
                                            db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> extension class <u>$extensionClassName</u> found", "created");
88
                                        }
89
                                    }
90
                                }
91
                            } elseif (in_array($variable, $variablesToSkip)) {
92
                                db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> skipped");
93 View Code Duplication
                            } else {
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...
94
                                if (!property_exists($className, $variable)) {
95
                                    db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> does not exist", "deleted");
96
                                } else {
97
                                    db::alteration_message("&nbsp; &nbsp; &nbsp; <u>$className.$variable</u> found", "created");
98
                                }
99
                            }
100
                        }
101
                    }
102
                }
103
            }
104
        }
105
    }
106
}
107