Completed
Push — master ( 93aa4d...115aec )
by Jack
01:55
created

ConfigYML::replaceFaultyYML()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require ('../modulechecks/vendor/autoload.php');
3
use Symfony\Component\Yaml\Yaml;
4
5
6
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...
7
8
    public function ConfigYML($gitHubModuleInstance) {
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
9
        if (! $gitHubModuleInstance) {
10
            user_error ("ConfigYML needs an instance of GitHubModule");
11
        }
12
        $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...
13
        $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...
14
        $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...
15
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
16
17
        $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...
18
    }
19
    
20
    public function reWrite(){
21
		if (! $this->readYMLFromFile()) 
22
		{
23
			return false;
24
		}
25
		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...
26
		{
27
			return false;
28
		}
29
		return true;		
30
	}
31
32
    public function readYMLFromFile() {
33
34
		GeneralMethods::output_to_screen("reading config yml ...  ",'updating');
35
		
36
		if (! file_exists ($this->filename)) 
37
		{
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
            
48
            
49
            
50
            
51
        } catch (Exception $e) {
52
            GeneralMethods::output_to_screen("<li>Unable to parse the YAML string: " .$e->getMessage(). " <li>", 'updated') ;
53
            
54
            //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...
55
            
56
            UpdateModules::addUnsolvedProblem($this->gitHubModuleInstance->ModuleName, "Unable to parse the YAML string: " .$e->getMessage());
57
            
58
			//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...
59
	
60
			$this->replaceFaultyYML();
61
	
62
			return false;
63
        }
64
65
66
        return $this->yaml_data;
67
68
    }
69
    
70
    
71
    public function replaceFaultyYML() {
72
           
73
    
74
		if (file_exists ($this->filename)) {
75
			
76
			
77
			$rawYML = file_get_contents($this->filename);
78
			
79
			$lines = explode("\n", $rawYML);
80
			
81
			$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...
82
			
83
			foreach ($lines as $index=>$line) {
84
				if (strpos ($line, "After:" ) !==false) {
85
					$replacment = "After: \r\n";
86
					$listitems = explode (',', $line);
87
					//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...
88
					foreach ($listitems as $item)
89
					{
90
						$item = str_replace('After: ', '', $item);
91
						$replacment .= '  - '. $item . "\r\n";
92
					}
93
					$lines[$index] = $replacment;
94
				}
95
			}
96
			$newYML = implode("\r\n", $lines);
97
			file_put_contents($this->filename, $newYML);
98
			
99
			die("check $this->filename!!");
0 ignored issues
show
Coding Style Compatibility introduced by
The method replaceFaultyYML() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
100
		}
101
		else
102
		{
103
			return false;
104
		}
105
           
106
    }
107
    public function writeYAMLToFile() {
108
		
109
		GeneralMethods::output_to_screen("Writing config yml ... ",'updating');
110
		
111
		if (!$this->yaml_data) {
112
			return false;
113
		}
114
		
115
		$yaml = Yaml::dump($this->yaml_data);
116
		file_put_contents($this->filename, $yaml);
117
		return true;
118
		
119
		
120
	}
121
122
    private function catchFopenWarning() {
123
124
    }
125
126
}
127
128