Passed
Push — master ( 33c64b...8d1135 )
by Michael
03:38
created

VersionChecks::checkVerPhp()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 1
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Newbb\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 $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
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 = '' . $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

42
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
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
     * @param \XoopsModule|null $module
58
     *
59
     * @return bool true if meets requirements, false if not
60
     */
61
    public static function checkVerPhp(\XoopsModule $module = null)
62
    {
63
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
64
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
65
        xoops_loadLanguage('admin', $moduleDirName);
66
        // check for minimum PHP version
67
        $success = true;
68
69
        $verNum = PHP_VERSION;
70
        $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

70
        $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...
71
72
        if (false !== $reqVer && '' !== $reqVer) {
73
            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

73
            if (version_compare($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 $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

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
     *
84
     * compares current module version with latest GitHub release
85
     * @static
86
     * @param \Xmf\Module\Helper $helper
87
     * @param string|null        $source
88
     * @param string|null        $default
89
     *
90
     * @return string|array info about the latest module version, if newer
91
     */
92
93
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
94
    {
95
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
96
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
97
        $update             = '';
98
        $repository         = 'XoopsModules25x/' . $moduleDirName;
99
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
100
        $ret             = '';
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, false);
107
                curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("User-Agent:Publisher\r\n"));
108
                $curlReturn = curl_exec($curlHandle);
109
                if (false === $curlReturn) {
110
                    trigger_error(curl_error($curlHandle));
111
                } else {
112
                    $file              = json_decode($curlReturn, false);
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 && version_compare($moduleVersion, $latestVersion, '<')) {
131
                        $ret   = [];
132
                        $ret[] = $update;
133
                        $ret[] = $latestVersionLink;
134
                    }
135
                }
136
                curl_close($curlHandle);
137
            }
138
        }
139
        return $ret;
140
    }
141
}
142