Passed
Push — master ( eec3d3...7b6eed )
by Michael
02:45
created

VersionChecks   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkVerXoops() 0 23 4
A checkVerPhp() 0 24 5
C checkVerModule() 0 49 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Xoopsheadline\Common;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
/**
18
 * @copyright   XOOPS Project (https://xoops.org)
19
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @author      mamba <[email protected]>
21
 */
22
23
use Xmf\Module\Helper;
24
25
trait VersionChecks
26
{
27
    /**
28
     * Verifies XOOPS version meets minimum requirements for this module
29
     * @static
30
     *
31
     */
32
    public static function checkVerXoops(?\XoopsModule $module = null, ?string $requiredVer = null): bool
33
    {
34
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
35
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
36
        if (null === $module) {
37
            $module = \XoopsModule::getByDirname($moduleDirName);
38
        }
39
        \xoops_loadLanguage('admin', $moduleDirName);
40
        \xoops_loadLanguage('common', $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 = (string)$module->getInfo('min_xoops'); //making sure it's a string
46
        }
47
        $success = true;
48
49
        if (\version_compare($currentVer, $requiredVer, '<')) {
50
            $success = false;
51
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
52
        }
53
54
        return $success;
55
    }
56
57
    /**
58
     * Verifies PHP version meets minimum requirements for this module
59
     * @static
60
     *
61
     * @return bool true if meets requirements, false if not
62
     */
63
    public static function checkVerPhp(?\XoopsModule $module = null): bool
64
    {
65
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
66
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
67
        if (null === $module) {
68
            $module = \XoopsModule::getByDirname($moduleDirName);
69
        }
70
        \xoops_loadLanguage('admin', $moduleDirName);
71
        \xoops_loadLanguage('common', $moduleDirName);
72
73
        // check for minimum PHP version
74
        $success = true;
75
76
        $verNum = \PHP_VERSION;
77
        $reqVer = &$module->getInfo('min_php');
78
79
        if (false !== $reqVer && '' !== $reqVer) {
80
            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

80
            if (\version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
81
                $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

81
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
82
                $success = false;
83
            }
84
        }
85
86
        return $success;
87
    }
88
89
    /**
90
     *
91
     * compares current module version with the latest GitHub release
92
     * @static
93
     *
94
     */
95
96
    public static function checkVerModule(Helper $helper, ?string $source = 'github', ?string $default = 'master'): ?array
97
    {
98
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
99
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
100
        $update             = '';
101
        $repository         = 'XoopsModules25x/' . $moduleDirName;
102
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
103
        $ret             = null;
104
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
105
        if ('github' === $source) {
106
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
107
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
108
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
109
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
110
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
111
                $curlReturn = \curl_exec($curlHandle);
112
                if (false === $curlReturn) {
113
                    \trigger_error(\curl_error($curlHandle));
114
                } elseif (\is_string($curlReturn) && false !== \strpos($curlReturn, 'Not Found')) {
115
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
116
                } elseif (\is_string($curlReturn)) {
117
                    $file              = json_decode($curlReturn, false);
118
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
119
                    $latestVersion     = $file[0]->tag_name;
120
                    $prerelease        = $file[0]->prerelease;
121
                    if ('master' !== $latestVersionLink) {
122
                        $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
123
                    }
124
                    //"PHP-standardized" version
125
                    $latestVersion = \mb_strtolower($latestVersion);
126
                    if (false !== mb_strpos($latestVersion, 'final')) {
127
                        $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
128
                        $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
129
                    }
130
                    $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

130
                    $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

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