VersionChecks   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 65
c 1
b 0
f 0
dl 0
loc 121
rs 10
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkVerXoops() 0 21 4
A checkVerPhp() 0 21 5
B checkVerModule() 0 50 11
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Marquee\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     GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
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 null|string $requiredVer
27
     * @return bool true if meets requirements, false if not
28
     */
29
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
30
    {
31
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
32
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
33
        if (null === $module) {
34
            $module = \XoopsModule::getByDirname($moduleDirName);
35
        }
36
        \xoops_loadLanguage('admin', $moduleDirName);
37
        \xoops_loadLanguage('common', $moduleDirName);
38
        //check for minimum XOOPS version
39
        $currentVer = mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
40
        if (null === $requiredVer) {
41
            $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

41
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
42
        }
43
        $success = true;
44
        if ($module->versionCompare($currentVer, $requiredVer, '<')) {
45
            $success = false;
46
            $module->setErrors(\sprintf(\_AM_MARQUEE_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
47
        }
48
49
        return $success;
50
    }
51
52
    /**
53
     * Verifies PHP version meets minimum requirements for this module
54
     * @static
55
     * @param \XoopsModule|bool|null $module
56
     *
57
     * @return bool true if meets requirements, false if not
58
     */
59
    public static function checkVerPhp(\XoopsModule $module = null)
60
    {
61
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
62
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
63
        if (null === $module) {
64
            $module = \XoopsModule::getByDirname($moduleDirName);
65
        }
66
        \xoops_loadLanguage('admin', $moduleDirName);
67
        \xoops_loadLanguage('common', $moduleDirName);
68
        // check for minimum PHP version
69
        $success = true;
70
        $verNum  = \PHP_VERSION;
71
        $reqVer  = &$module->getInfo('min_php');
72
        if (false !== $reqVer && '' !== $reqVer) {
73
            if ($module->versionCompare($verNum, $reqVer, '<')) {
0 ignored issues
show
Bug introduced by
It seems like $reqVer can also be of type array; however, parameter $version2 of XoopsModule::versionCompare() 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

73
            if ($module->versionCompare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
74
                $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

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

109
                } elseif (false !== \mb_strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
110
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
111
                } else {
112
                    $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

112
                    $file              = json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
113
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
114
                    $latestVersion     = $file[0]->tag_name;
115
                    $prerelease        = $file[0]->prerelease;
116
                    if ('master' !== $latestVersionLink) {
117
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
118
                    }
119
                    //"PHP-standardized" version
120
                    $latestVersion = \mb_strtolower($latestVersion);
121
                    if (false !== \mb_strpos($latestVersion, 'final')) {
122
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
123
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
124
                    }
125
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
0 ignored issues
show
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

125
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
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

125
                    $moduleVersion = (/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
Loading history...
126
                    //"PHP-standardized" version
127
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
128
                    //                    $moduleVersion = '1.0'; //for testing only
129
                    //                    $moduleDirName = 'publisher'; //for testing only
130
                    if (!$prerelease && $module->versionCompare($moduleVersion, $latestVersion, '<')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module does not exist. Did you maybe mean $moduleDirNameUpper?
Loading history...
131
                        $ret   = [];
132
                        $ret[] = $update;
133
                        $ret[] = $latestVersionLink;
134
                    }
135
                }
136
                \curl_close($curlHandle);
137
            }
138
        }
139
140
        return $ret;
141
    }
142
}
143