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

ComposerJson::updateJsonFile()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 26
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
    /**
6
     *
7
     * @var array|null
8
     */
9
    protected $jsonData = null;
10
11
    public function __construct($gitHubModuleInstance)
12
    {
13
        if (! $gitHubModuleInstance) {
14
            user_error("CheckComposerJson needs an instance of GitHubModule");
15
        }
16
        $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...
17
        $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...
18
    }
19
20
    private function readJsonFromFile()
21
    {
22
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
23
        $filename = $folder . '/' . $this->moduleName . '/composer.json';
24
        set_error_handler(array($this, 'catchFopenWarning'), E_WARNING);
25
        $json = file_get_contents($filename);
26
        restore_error_handler();
27
        if ($json) {
28
            $this->jsonData = json_decode($json, true);
29
        } else {
30
            UpdateModules::addUnsolvedProblem($this->moduleName, 'Could not open composer.json file...');
31
        }
32
        return (is_array($this->jsonData));
33
    }
34
35
36
    /**
37
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39
    public function getJsonData()
40
    {
41
        return $this->jsonData;
42
    }
43
44
    /**
45
     * @param array $array [description]
46
     */
47
    public function setJsonData(array $array)
48
    {
49
        $this->jsonData = $array;
50
    }
51
52
53
54
    public function updateJsonFile()
55
    {
56
        if (! is_array($this->jsonData)) {
57
            $this->readJsonFromFile();
58
        }
59
60
61
        if (is_array($this->jsonData)) {
62
            GeneralMethods::outputToScreen("<li> Updating composer.json </li>");
63
            $composerUpdates = ClassInfo::subclassesFor('UpdateComposer');
64
65
            //remove base class
66
            array_shift($composerUpdates);
67
68
            //get all updates and if they exists then get the ones that we need to do ...
69
            $limitedComposerUpdates = $this->Config()->get('updates');
70
            if ($limitedComposerUpdates === 'none') {
71
                $composerUpdates = [];
72
            } elseif (is_array($limitedComposerUpdates) && count($limitedComposerUpdates)) {
73
                $composerUpdates = array_intersect($composerUpdates, $limitedComposerUpdates);
74
            }
75
76
77
            foreach ($composerUpdates as $composerUpdate) {
78
                $obj = $composerUpdate::create($this);
79
                $obj->run();
80
            }
81
            if ($this->writeJsonToFile()) {
82
                GeneralMethods::outputToScreen("<li> Updated JSON </li>");
83
            } else {
84
                UpdateModules::addUnsolvedProblem($this->moduleName, 'Could not write JSON');
85
            }
86
        } else {
87
            //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...
88
89
            UpdateModules::addUnsolvedProblem($this->moduleName, 'No composer.json');
90
        }
91
    }
92
93
94
    protected function writeJsonToFile()
95
    {
96
        if (! is_array($this->jsonData)) { //if not loaded
97
            return false;
98
        }
99
100
        $folder = GitHubModule::Config()->get('absolute_temp_folder');
101
        $filename = $folder . '/' . $this->gitHubModuleInstance->ModuleName . '/composer.json';
102
        $value = file_put_contents($filename, json_encode($this->jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
103
104
        return  $value ? true : false;
105
    }
106
107
    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...
108
    {
109
        if (! is_array($this->jsonData)) { //if not loaded
110
            return 'no json data';
111
        }
112
113
        return isset($this->jsonData['description']) ? $this->jsonData['description'] : 'description tba';
114
    }
115
116
117
    private function catchFopenWarning()
118
    {
119
        user_error('Can not open composer file ....');
120
    }
121
}
122