Passed
Push — master ( 7db294...f869f0 )
by
unknown
27s queued 10s
created

VersionChecks   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkVerXoops() 0 22 4
A checkVerPhp() 0 19 4
B checkVerModule() 0 50 10
1
<?php
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
     *
24
     * Verifies XOOPS version meets minimum requirements for this module
25
     * @static
26
     * @param \XoopsModule|null $module
27
     *
28
     * @param null|string       $requiredVer
29
     * @return bool true if meets requirements, false if not
30
     */
31
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
32
    {
33
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
34
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
35
        if (null === $module) {
36
            $module = \XoopsModule::getByDirname($moduleDirName);
37
        }
38
        xoops_loadLanguage('admin', $moduleDirName);
39
40
        //check for minimum XOOPS version
41
        $currentVer = mb_substr(XOOPS_VERSION, 6); // get the numeric part of string
42
        if (null === $requiredVer) {
43
            $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

43
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
44
        }
45
        $success = true;
46
47
        if (version_compare($currentVer, $requiredVer, '<')) {
48
            $success = false;
49
            $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
50
        }
51
52
        return $success;
53
    }
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
        xoops_loadLanguage('admin', $moduleDirName);
68
        // check for minimum PHP version
69
        $success = true;
70
71
        $verNum = PHP_VERSION;
72
        $reqVer =& $module->getInfo('min_php');
0 ignored issues
show
Bug introduced by
The method getInfo() does not exist on null. ( Ignorable by Annotation )

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

72
        $reqVer =& $module->/** @scrutinizer ignore-call */ getInfo('min_php');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
        if (false !== $reqVer && '' !== $reqVer) {
75
            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

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

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

129
					$moduleVersion = (/** @scrutinizer ignore-type */ $helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status'));
Loading history...
130
					//"PHP-standardized" version
131
					$moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion));
132
					//$moduleVersion = '1.0'; //for testing only
133
					//$moduleDirName = 'publisher'; //for testing only
134
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