VersionChecks::checkVerModule()   B
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 36
nc 12
nop 3
dl 0
loc 49
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

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 XoopsModules\Mastopgo2\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
     * @param \XoopsModule|null $module
26
     *
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
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
34
        if (null === $module) {
35
            $module = \XoopsModule::getByDirname($moduleDirName);
36
        }
37
        xoops_loadLanguage('admin', $moduleDirName);
38
        xoops_loadLanguage('common', $moduleDirName);
39
40
41
        //check for minimum XOOPS version
42
        $currentVer = mb_substr(XOOPS_VERSION, 6); // get the numeric part of string
43
        if (null === $requiredVer) {
44
            $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

44
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
45
        }
46
        $success = true;
47
48
        if (version_compare($currentVer, $requiredVer, '<')) {
49
            $success = false;
50
            $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
51
        }
52
53
        return $success;
54
    }
55
56
    /**
57
     * Verifies PHP version meets minimum requirements for this module
58
     * @static
59
     * @param \XoopsModule|null $module
60
     *
61
     * @return bool true if meets requirements, false if not
62
     */
63
    public static function checkVerPhp(\XoopsModule $module = null)
64
    {
65
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
66
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
67
        if (null === $module) {
68
            $module = \XoopsModule::getByDirname($moduleDirName);
69
        }
70
        xoops_loadLanguage('admin', $moduleDirName);
71
        // check for minimum PHP version
72
        $success = true;
73
74
        $verNum = PHP_VERSION;
75
        $reqVer = &$module->getInfo('min_php');
76
77
        if (false !== $reqVer && '' !== $reqVer) {
78
            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

78
            if (version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
79
                $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 $args of sprintf() 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

79
                $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
80
                $success = false;
81
            }
82
        }
83
84
        return $success;
85
    }
86
87
    /**
88
     *
89
     * compares current module version with latest GitHub release
90
     * @static
91
     * @param \Xmf\Module\Helper $helper
92
     * @param string|null        $source
93
     * @param string|null        $default
94
     *
95
     * @return string|array info about the latest module version, if newer
96
     */
97
98
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
99
    {
100
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
101
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
102
        $update             = '';
103
        $repository         = 'XoopsModules25x/' . $moduleDirName;
104
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
105
        $ret             = '';
106
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
107
        if ('github' === $source) {
108
            if (function_exists('curl_init') && false !== ($curlHandle = curl_init())) {
109
                curl_setopt($curlHandle, CURLOPT_URL, $infoReleasesUrl);
110
                curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
111
                curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
112
                curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
113
                $curlReturn = curl_exec($curlHandle);
114
                if (false === $curlReturn) {
115
                    trigger_error(curl_error($curlHandle));
116
                } elseif (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 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

116
                } elseif (strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
117
                    trigger_error('Repository Not Found: ' . $infoReleasesUrl);
118
                } else {
119
                    $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

119
                    $file              = json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
120
                    $latestVersionLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
121
                    $latestVersion     = $file[0]->tag_name;
122
                    $prerelease        = $file[0]->prerelease;
123
                    if ('master' !== $latestVersionLink) {
124
                        $update = constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
125
                    }
126
                    //"PHP-standardized" version
127
                    $latestVersion = mb_strtolower($latestVersion);
128
                    if (false !== mb_strpos($latestVersion, 'final')) {
129
                        $latestVersion = str_replace('_', '', mb_strtolower($latestVersion));
130
                        $latestVersion = str_replace('final', '', mb_strtolower($latestVersion));
131
                    }
132
                    $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

132
                    $moduleVersion = (/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
Loading history...
Bug introduced by
Are you sure $helper->getModule()->getInfo('module_status') 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

132
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
133
                    //"PHP-standardized" version
134
                    $moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion));
135
                    //                    $moduleVersion = '1.0'; //for testing only
136
                    //                    $moduleDirName = 'publisher'; //for testing only
137
                    if (!$prerelease && version_compare($moduleVersion, $latestVersion, '<')) {
138
                        $ret   = [];
139
                        $ret[] = $update;
140
                        $ret[] = $latestVersionLink;
141
                    }
142
                }
143
                curl_close($curlHandle);
144
            }
145
        }
146
        return $ret;
147
    }
148
}
149