Completed
Push — master ( 1385c4...94a8d3 )
by Nicolaas
01:48
created

ConfigYML::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
//require ('../modulechecks/vendor/autoload.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class ConfigYML 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...
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 introduced by
The property gitHubModuleInstance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $this->moduleName = $gitHubModuleInstance->ModuleName;
0 ignored issues
show
Bug introduced by
The property moduleName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        $this->yaml_data = null;
0 ignored issues
show
Bug introduced by
The property yaml_data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
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 introduced by
The property filename does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    public function reWrite()
23
    {
24
        if (! $this->readYMLFromFile()) {
25
            return false;
26
        }
27
        if (! $this->writeYAMLToFile()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->writeYAMLToFile();.
Loading history...
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;
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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));
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
52
            UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to parse the YAML string: " .$e->getMessage());
53
54
            //trigger_error ("Error in YML file");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
56
            $this->replaceFaultyYML();
57
58
            return false;
59
        }
60
61
62
        return $this->yaml_data;
63
    }
64
65
66
    public function replaceFaultyYML()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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
if (file_exists($this->f...e { return false; } does not seem to be 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 = '';
0 ignored issues
show
Unused Code introduced by
$replacment 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...
79
80
            foreach ($lines as $index=>$line) {
81
                if (strpos($line, "After:") !==false) {
82
                    $replacment = "After:";
83
                    $listitems = explode(',', $line);
84
                    //print_r ($listitems);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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");
0 ignored issues
show
Unused Code introduced by
$file 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...
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