Passed
Push — master ( 680d04...f51dc3 )
by Michael
03:21
created

VersionChecks   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

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

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\Adslight\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
trait VersionChecks
23
{
24
    /**
25
     * Verifies XOOPS version meets minimum requirements for this module
26
     * @static
27
     *
28
     */
29
    public static function checkVerXoops(?\XoopsModule $module = null, ?string $requiredVer = null): bool
30
    {
31
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
32
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
33
        if (null === $module) {
34
            $module = \XoopsModule::getByDirname($moduleDirName);
35
        }
36
        \xoops_loadLanguage('admin', $moduleDirName);
37
        \xoops_loadLanguage('common', $moduleDirName);
38
39
        //check for minimum XOOPS version
40
        $currentVer = \mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
41
        if (null === $requiredVer) {
42
            $requiredVer = (string)$module->getInfo('min_xoops'); //making sure it's a string
43
        }
44
        $success = true;
45
46
        if (\version_compare($currentVer, $requiredVer, '<')) {
47
            $success = false;
48
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
49
        }
50
51
        return $success;
52
    }
53
54
    /**
55
     * Verifies PHP version meets minimum requirements for this module
56
     * @static
57
     *
58
     * @return bool true if meets requirements, false if not
59
     */
60
    public static function checkVerPhp(?\XoopsModule $module = null): bool
61
    {
62
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
63
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
64
        if (null === $module) {
65
            $module = \XoopsModule::getByDirname($moduleDirName);
66
        }
67
        \xoops_loadLanguage('admin', $moduleDirName);
68
        \xoops_loadLanguage('common', $moduleDirName);
69
70
        // check for minimum PHP version
71
        $success = true;
72
73
        $verNum = \PHP_VERSION;
74
        $reqVer = &$module->getInfo('min_php');
75
76
        if (false !== $reqVer && '' !== $reqVer) {
77
            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

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

78
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
79
            $success = false;
80
        }
81
        }
82
83
        return $success;
84
    }
85
86
    /**
87
     *
88
     * compares current module version with the latest GitHub release
89
     * @static
90
     *
91
     */
92
93
    public static function checkVerModule(Helper $helper, ?string $source = 'github', ?string $default = 'master'): ?array
0 ignored issues
show
Bug introduced by
The type XoopsModules\Adslight\Common\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
    {
95
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
96
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
97
        $update             = '';
98
        $repository         = 'XoopsModules25x/' . $moduleDirName;
99
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
100
        $ret             = null;
101
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
102
        if ('github' === $source) {
103
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
104
            \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
105
            \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
106
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
107
            \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
108
            $curlReturn = \curl_exec($curlHandle);
109
            if (false === $curlReturn) {
110
                \trigger_error(\curl_error($curlHandle));
111
                } elseif (\is_string($curlReturn) && false !== \strpos($curlReturn, 'Not Found')) {
112
                \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
113
                } elseif (\is_string($curlReturn)) {
114
                    $file              = json_decode($curlReturn, false);
115
                    $latestVersionLink = \sprintf("https://github.com/$repository/archive/%s.zip", $file ? \reset($file)->tag_name : $default);
116
                $latestVersion     = $file[0]->tag_name;
117
                $prerelease        = $file[0]->prerelease;
118
                if ('master' !== $latestVersionLink) {
119
                    $update = \constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
120
                }
121
                //"PHP-standardized" version
122
                $latestVersion = \mb_strtolower($latestVersion);
123
                    if (false !== mb_strpos($latestVersion, 'final')) {
124
                    $latestVersion = \str_replace('_', '', \mb_strtolower($latestVersion));
125
                    $latestVersion = \str_replace('final', '', \mb_strtolower($latestVersion));
126
                }
127
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
128
                //"PHP-standardized" version
129
                $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
130
                //                    $moduleVersion = '1.0'; //for testing only
131
                //                    $moduleDirName = 'publisher'; //for testing only
132
                if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
133
                    $ret   = [];
134
                    $ret[] = $update;
135
                    $ret[] = $latestVersionLink;
136
                }
137
            }
138
            \curl_close($curlHandle);
139
            }
140
        }
141
        return $ret;
142
    }
143
}
144