Passed
Pull Request — master (#88)
by Michael
02:56
created

VersionChecks::checkVerPhp()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 6
nop 1
dl 0
loc 31
rs 9.4888
c 5
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright   XOOPS Project (https://xoops.org)
17
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @author      mamba <[email protected]>
19
 */
20
trait VersionChecks
21
{
22
    /**
23
     * Verifies XOOPS version meets minimum requirements for this module
24
     * @static
25
     *
26
     * @param \XoopsModule|null $module
27
     * @param null|string       $requiredVer
28
     * @return bool true if meets requirements, false if not
29
     */
30
    public static function checkVerXoops(?\XoopsModule $module = null, $requiredVer = null)
31
    {
32
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
33
34
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
35
36
        if (null === $module) {
37
            $module = \XoopsModule::getByDirname($moduleDirName);
38
        }
39
40
        \xoops_loadLanguage('admin', $moduleDirName);
41
42
        //check for minimum XOOPS version
43
        $currentVer = \mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
44
        if (null === $requiredVer) {
45
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
0 ignored issues
show
Bug introduced by
Are you sure $module->getInfo('min_xoops') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

45
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
46
        }
47
48
        $success = true;
49
50
        if (\version_compare($currentVer, $requiredVer, '<')) {
51
            $success = false;
52
53
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
54
        }
55
56
        return $success;
57
    }
58
59
    /**
60
     * Verifies PHP version meets minimum requirements for this module
61
     * @static
62
     *
63
     * @param \XoopsModule|null $module
64
     * @return bool true if meets requirements, false if not
65
     */
66
    public static function checkVerPhp(?\XoopsModule $module = null)
67
    {
68
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
69
70
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
71
72
        if (null === $module) {
73
            $module = \XoopsModule::getByDirname($moduleDirName);
74
        }
75
76
        \xoops_loadLanguage('admin', $moduleDirName);
77
78
        \xoops_loadLanguage('common', $moduleDirName);
79
80
        // check for minimum PHP version
81
82
        $success = true;
83
84
        $verNum = \PHP_VERSION;
85
86
        $reqVer = &$module->getInfo('min_php');
87
88
        if (false !== $reqVer && '' !== $reqVer) {
89
            if (\version_compare($verNum, $reqVer, '<')) {
0 ignored issues
show
Bug introduced by
It seems like $reqVer can also be of type array; however, parameter $version2 of version_compare() does only seem to accept string, 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

89
            if (\version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
90
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
0 ignored issues
show
Bug introduced by
It seems like $reqVer can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

90
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
91
92
                $success = false;
93
            }
94
        }
95
96
        return $success;
97
    }
98
99
    /**
100
     * compares current module version with latest GitHub release
101
     * @static
102
     * @param \Xmf\Module\Helper $helper
103
     * @param string|null        $source
104
     * @param string|null        $default
105
     *
106
     * @return string|array info about the latest module version, if newer
107
     */
108
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
109
    {
110
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
111
112
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
113
114
        $update = '';
115
116
        $repository = 'XoopsModules25x/' . $moduleDirName;
117
118
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
119
120
        $ret = '';
121
122
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
123
124
        if ('github' === $source) {
125
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
126
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
127
128
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
129
130
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
131
132
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
133
134
                $curlReturn = \curl_exec($curlHandle);
135
136
                if (false === $curlReturn) {
137
                    \trigger_error(\curl_error($curlHandle));
138
                } elseif (false !== \mb_strpos($curlReturn, 'Not Found')) {
0 ignored issues
show
Bug introduced by
It seems like $curlReturn can also be of type true; however, parameter $haystack of mb_strpos() does only seem to accept string, 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

138
                } elseif (false !== \mb_strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
139
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
140
                } else {
141
                    $file = \json_decode($curlReturn, false);
0 ignored issues
show
Bug introduced by
It seems like $curlReturn can also be of type true; however, parameter $json of json_decode() does only seem to accept string, 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

141
                    $file = \json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
142
143
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
144
145
                    $latestVersion = $file[0]->tag_name;
146
147
                    $prerelease = $file[0]->prerelease;
148
149
                    if ('master' !== $latestVersionLink) {
150
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
151
                    }
152
153
                    //"PHP-standardized" version
154
155
                    $latestVersion = \mb_strtolower($latestVersion);
156
157
                    if (false !== \mb_strpos($latestVersion, 'final')) {
158
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
159
160
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
161
                    }
162
163
                    $moduleVersion = $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status');
0 ignored issues
show
Bug introduced by
Are you sure $helper->getModule()->getInfo('version') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

163
                    $moduleVersion = /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status');
Loading history...
164
165
                    //"PHP-standardized" version
166
167
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
168
169
                    //                    $moduleVersion = '1.0'; //for testing only
170
171
                    //                    $moduleDirName = 'publisher'; //for testing only
172
173
                    if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
174
                        $ret = [];
175
176
                        $ret[] = $update;
177
178
                        $ret[] = $latestVersionLink;
179
                    }
180
                }
181
182
                \curl_close($curlHandle);
183
            }
184
        }
185
186
        return $ret;
187
    }
188
}
189