ConfigYML::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
//require ('../modulechecks/vendor/autoload.php');
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class ConfigYML extends Object
8
{
9
    public function __construct($gitHubModuleInstance)
10
    {
11
        if (! $gitHubModuleInstance) {
12
            user_error("ConfigYML needs an instance of GitHubModule");
13
        }
14
        $this->gitHubModuleInstance = $gitHubModuleInstance;
0 ignored issues
show
Bug Best Practice introduced by
The property gitHubModuleInstance does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
        $this->moduleName = $gitHubModuleInstance->ModuleName;
0 ignored issues
show
Bug Best Practice introduced by
The property moduleName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->yaml_data = null;
0 ignored issues
show
Bug Best Practice introduced by
The property yaml_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
18
19
        $this->filename = $folder . '/' . $this->moduleName . '/_config/config.yml';
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    public function reWrite()
23
    {
24
        if (! $this->readYMLFromFile()) {
25
            return false;
26
        }
27
        if (! $this->writeYAMLToFile()) {
28
            return false;
29
        }
30
        return true;
31
    }
32
33
    public function readYMLFromFile()
34
    {
35
        GeneralMethods::output_to_screen("reading config yml ...  ", 'updating');
36
37
        if (! file_exists($this->filename)) {
38
            GeneralMethods::output_to_screen("<li>Unable to load: " . $this->filename, 'updated') ;
39
            //UpdateModules::$unsolvedItems[$this->gitHubModuleInstance->ModuleName] = "Unable to load " . $this->filename;
40
41
            UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to load " . $this->filename);
42
            return false;
43
        }
44
45
        try {
46
            $this->yaml_data = Yaml::parse(file_get_contents($this->filename));
0 ignored issues
show
Bug Best Practice introduced by
The property yaml_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        } catch (Exception $e) {
48
            GeneralMethods::output_to_screen("<li>Unable to parse the YAML string: " .$e->getMessage(). " <li>", 'updated') ;
49
50
            //UpdateModules::$unsolvedItems[$this->gitHubModuleInstance->ModuleName] = "Unable to parse the YAML string: " .$e->getMessage();
51
52
            UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to parse the YAML string: " .$e->getMessage());
53
54
            //trigger_error ("Error in YML file");
55
56
            $this->replaceFaultyYML();
57
58
            return false;
59
        }
60
61
62
        return $this->yaml_data;
63
    }
64
65
66
    public function replaceFaultyYML()
67
    {
68
        return false;
69
70
        /**function broken do not use**/
71
72
73
        if (file_exists($this->filename)) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
74
            $rawYML = file_get_contents($this->filename);
75
76
            $lines = explode("\n", $rawYML);
77
78
            $replacment = '';
79
80
            foreach ($lines as $index=>$line) {
81
                if (strpos($line, "After:") !==false) {
82
                    $replacment = "After:";
83
                    $listitems = explode(',', $line);
84
                    //print_r ($listitems);
85
                    foreach ($listitems as $item) {
86
                        if (! trim($item)) {
87
                            continue;
88
                        }
89
90
                        $item = str_replace('After: ', '', $item);
91
                        $replacment .= '  - '. trim($item) . "";
92
                    }
93
                    $lines[$index] = $replacment;
94
                }
95
            }
96
            $newYML = implode('', $lines);
97
98
            GeneralMethods::output_to_screen("Updating config.YML to correct syntax ... ", 'updating');
99
100
            $file = fopen($this->filename, "w");
101
102
103
104
            file_put_contents($this->filename, $newYML);
105
        } else {
106
            return false;
107
        }
108
    }
109
    public function writeYAMLToFile()
110
    {
111
        GeneralMethods::output_to_screen("Writing config yml ... ", 'updating');
112
113
        if (!$this->yaml_data) {
114
            return false;
115
        }
116
117
        $yaml = Yaml::dump($this->yaml_data);
118
        file_put_contents($this->filename, $yaml);
119
        return true;
120
    }
121
122
    private function catchFopenWarning()
123
    {
124
    }
125
}
126