Completed
Push — master ( 3c0477...229994 )
by Jack
02:01
created

ComposerJson::checkOrAddExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
class ComposerJson 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...
4
{
5
    public function ComposerJson($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...
6
    {
7
        if (! $gitHubModuleInstance) {
8
            user_error("CheckComposerJson needs an instance of GitHubModule");
9
        }
10
        $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...
11
        $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...
12
    }
13
14
    private function readJsonFromFile()
15
    {
16
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
17
        $filename = $folder . '/' . $this->moduleName . '/composer.json';
18
19
        set_error_handler(array($this, 'catchFopenWarning'), E_WARNING);
20
        $file = fopen($filename, 'r');
21
        restore_error_handler();
22
        if ($file) {
23
            $json = fread($file, filesize($filename));
24
            $array = json_decode($json);
25
            $this->jsonData = $array;
0 ignored issues
show
Bug introduced by
The property jsonData 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...
26
            fclose($file);
27
        }
28
29
30
        return (isset($this->jsonData));
31
    }
32
33
    public function updateJsonData()
34
    {
35
        if (! isset($this->jsonData)) {
36
            $this->readJsonFromFile();
37
        }
38
        
39
40
        if (isset($this->jsonData)) {
41
            GeneralMethods::outputToScreen("<li> Updating composer.json </li>");
42
            $composerUpdates = ClassInfo::subclassesFor('UpdateComposer');
43
            array_shift($composerUpdates);
44
45
            $limitedComposerUpdates = $this->Config()->get('updates');
46
            
47
            if ($limitedComposerUpdates && count($limitedComposerUpdates)) {
48
                $composerUpdates = array_intersect($composerUpdates, $limitedComposerUpdates);
49
            }
50
51
52
            foreach ($composerUpdates as $composerUpdate) {
53
                $obj = $composerUpdate::create($this);
54
                $obj->run();
55
            }
56
57
            $this->writeJsonToFile();
58
        } else {
59
            GeneralMethods::outputToScreen('<li style = "color: red;"> ' . $this->moduleName. '  has no composer.json !!!</li>');
60
            //UpdateModules::$unsolvedItems[$this->moduleName] = 'No composer.json';
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
61
            
62
            UpdateModules::addUnsolvedProblem($this->moduleName, 'No composer.json');
63
        }
64
    }
65
66
    protected function checkOrAddExtra() {
67
		
68
	}
69
    
70
    protected function writeJsonToFile()
71
    {
72
        if (! $this->jsonData) { //if not loaded
73
            return false;
74
        }
75
        
76
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
77
        $filename = $folder . '/' . $this->gitHubModuleInstance->ModuleName . '/composer.json';
78
79
        
80
81
        $file = fopen($filename, 'w');
82
        if ($file) {
83
            fwrite($file, json_encode($this->jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
84
        }
85
        fclose($file);
86
87
        return true;
88
    }
89
90
    public function getDescription()
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...
91
    {
92
        if (! property_exists ($this, 'jsonData') || ! $this->jsonData) { //if not loaded
93
            return false;
94
        }
95
96
        return $this->jsonData->description;
97
    }
98
99
100
    private function catchFopenWarning()
101
    {
102
    }
103
}
104