Passed
Pull Request — master (#264)
by
unknown
05:25
created

ExtensionModel::localList()   C

Complexity

Conditions 9
Paths 306

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 42
c 0
b 0
f 0
nc 306
nop 0
dl 0
loc 57
rs 5.9657

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Models\Babel;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Models\Eloquent\OJModel;
7
use PharIo\Version\Version;
8
use Throwable;
9
10
class ExtensionModel extends Model
11
{
12
13
    public static $status=[
14
        "-1"=>"Extension Fatal Error",
15
        "0"=>"Not Downloaded",
16
        "1"=>"Not Installed",
17
        "2"=>"Installed",
18
    ];
19
20
    public static function localList()
21
    {
22
        $ret=[];
23
        $marketspaceRaw=self::getRemote();
24
        $marketspace=[];
25
        foreach($marketspaceRaw["packages"] as $extension){
26
            $marketspace[$extension["name"]]=$extension;
27
        }
28
29
        $localList=self::getLocal();
30
31
        foreach($localList as $extension){
32
            $temp=[
33
                "details"=>$extension,
34
                "status"=>0,
35
                "version"=>null,
36
                "updatable"=>false,
37
                "settings"=>null,
38
                "available"=>null
39
            ];
40
            $temp["details"]["typeParsed"]=$temp["details"]["type"]=="virtual-judge"?"VirtualJudge":"OnlineJudge";
41
            try {
42
                if ($extension["version"]=='__cur__') {
43
                    $extension["version"]=explode("-", version())[0];
44
                }
45
                $downloadedVersion=new Version($extension["version"]);
46
47
                if(isset($marketspace[$extension["name"]])){
48
                    //remote extension, else is local extension
49
                    $remoteVersion=new Version($marketspace[$extension["name"]]["version"]);
50
                    $temp["updatable"]=$remoteVersion->isGreaterThan($downloadedVersion);
51
                    $temp["details"]["official"]=$marketspace[$extension["name"]]["official"];
52
                } else{
53
                    $temp["updatable"]=false;
54
                    $temp["details"]["official"]=0;
55
                }
56
57
                $installedConfig=OJModel::where(["ocode"=>$extension["code"]])->first();
58
                if (is_null($installedConfig)){
59
                    $temp["status"]=1;
60
                } else {
61
                    $temp["version"]=$installedConfig->version; // local installed version
62
                    $installedVersion=new Version($temp["version"]);
63
                    if ($downloadedVersion->isGreaterThan($installedVersion)){
64
                        $temp["status"]=1;
65
                    } else {
66
                        $temp["status"]=2;
67
                    }
68
                    $temp["settings"]=false;
69
                    $temp["available"]=$installedConfig->status;
70
                }
71
            }catch (Throwable $e){
72
                continue;
73
            }
74
            $ret[]=$temp;
75
        }
76
        return $ret;
77
    }
78
79
    public static function list()
80
    {
81
        $ret=[];
82
        $marketspaceRaw=self::getRemote();
83
        if(empty($marketspaceRaw)) return [];
84
        foreach($marketspaceRaw["packages"] as $extension){
85
            $temp=[
86
                "details"=>$extension,
87
                "status"=>0,
88
                "version"=>null,
89
                "updatable"=>false,
90
                "settings"=>null,
91
                "available"=>null
92
            ];
93
            $temp["details"]["typeParsed"]=$temp["details"]["type"]=="virtual-judge"?"VirtualJudge":"OnlineJudge";
94
            try {
95
                try {
96
                    $BabelConfig=json_decode(file_get_contents(babel_path("Extension/{$extension['code']}/babel.json")), true);
97
                }catch (Throwable $e){
98
                    $BabelConfig=[];
99
                }
100
                if (!empty($BabelConfig)) {
101
                    if ($BabelConfig["version"]=='__cur__') {
102
                        $BabelConfig["version"]=explode("-", version())[0];
103
                    }
104
                    $downloadedVersion=new Version($BabelConfig["version"]);
105
                    $remoteVersion=new Version($extension["version"]);
106
                    $temp["updatable"]=$remoteVersion->isGreaterThan($downloadedVersion);
107
108
                    $installedConfig=OJModel::where(["ocode"=>$extension["code"]])->first();
109
                    if (is_null($installedConfig)){
110
                        $temp["status"]=1;
111
                    } else {
112
                        $temp["version"]=$installedConfig->version; // local installed version
113
                        $installedVersion=new Version($temp["version"]);
114
                        if ($downloadedVersion->isGreaterThan($installedVersion)){
115
                            $temp["status"]=1;
116
                        } else {
117
                            $temp["status"]=2;
118
                        }
119
                        $temp["settings"]=false;
120
                        $temp["available"]=$installedConfig->status;
121
                    }
122
                }
123
            }catch (Throwable $e){
124
                continue;
125
            }
126
            $ret[]=$temp;
127
        }
128
129
        return $ret;
130
    }
131
132
    public static function getLocal()
133
    {
134
        $ret=[];
135
        $dirs = array_filter(glob(babel_path("Extension/*")), 'is_dir');
0 ignored issues
show
Bug introduced by
It seems like glob(babel_path('Extension/*')) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $dirs = array_filter(/** @scrutinizer ignore-type */ glob(babel_path("Extension/*")), 'is_dir');
Loading history...
136
        foreach($dirs as $d){
137
            $extension=basename($d);
138
            $BabelConfig=json_decode(file_get_contents(babel_path("Extension/$extension/babel.json")), true);
139
            if($extension==$BabelConfig["code"]) $ret[]=$BabelConfig;
140
        }
141
        return $ret;
142
    }
143
144
    public static function getRemote()
145
    {
146
        try {
147
            return json_decode(file_get_contents(config('babel.mirror')."/babel.json"), true);
148
        }catch(Throwable $e){
149
            return [];
150
        }
151
    }
152
153
    public static function remoteDetail($code)
154
    {
155
        $babelConfig=self::getRemote();
156
        if(empty($babelConfig)) return [];
157
        $babelConfigPackages=$babelConfig["packages"];
158
        foreach($babelConfigPackages as $package) {
159
            if($package["code"]==$code) return $package;
160
        }
161
        return [];
162
    }
163
}
164