Passed
Push — master ( 40654a...b847d6 )
by Michael
06:43 queued 10s
created

VersionChecks::checkVerXoops()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 23
rs 9.7998
1
<?php namespace XoopsModules\Pedigree\Common;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright   XOOPS Project (https://xoops.org)
15
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @author      mamba <[email protected]>
17
 */
18
trait VersionChecks
19
{
20
    /**
21
     * Verifies XOOPS version meets minimum requirements for this module
22
     * @static
23
     * @param \XoopsModule|null $module
24
     *
25
     * @param null|string       $requiredVer
26
     * @return bool true if meets requirements, false if not
27
     */
28
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
29
    {
30
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
31
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
32
        if (null === $module) {
33
            $module = \XoopsModule::getByDirname($moduleDirName);
34
        }
35
        xoops_loadLanguage('admin', $moduleDirName);
36
        xoops_loadLanguage('common', $moduleDirName);
37
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
45
        if (version_compare($currentVer, $requiredVer, '<')) {
46
            $success = false;
47
            $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
48
        }
49
50
        return $success;
51
    }
52
53
    /**
54
     * Verifies PHP version meets minimum requirements for this module
55
     * @static
56
     * @param \XoopsModule|null $module
57
     *
58
     * @return bool true if meets requirements, false if not
59
     */
60
    public static function checkVerPhp(\XoopsModule $module = null)
61
    {
62
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
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 && !is_array($reqVer)) {
77
            if (version_compare($verNum, $reqVer, '<')) {
78
                $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum));
79
                $success = false;
80
            }
81
        }
82
83
        return $success;
84
    }
85
86
    /**
87
     *
88
     * compares current module version with latest GitHub release
89
     * @static
90
     * @param \Xmf\Module\Helper $helper
91
     * @param string|null        $source
92
     * @param string|null        $default
93
     *
94
     * @return string|array info about the latest module version, if newer
95
     */
96
97
    public static function checkVerModule($helper, $source = 'github', $default = 'master')
98
    {
99
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
100
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
101
        $update             = '';
102
        $repository         = 'XoopsModules25x/' . $moduleDirName;
103
        //        $repository         = 'XoopsModules25x/publisher'; //for testing only
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
        $ret             = '';
105
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
106
        if ('github' === $source) {
107
            if (function_exists('curl_init') && false !== ($curlHandle = curl_init())) {
108
                curl_setopt($curlHandle, CURLOPT_URL, $infoReleasesUrl);
109
                curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
110
                curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
111
                curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
112
                $curlReturn = curl_exec($curlHandle);
113
                if (false === $curlReturn) {
114
                    trigger_error(curl_error($curlHandle));
115
                } elseif (false !== 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 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

115
                } elseif (false !== strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
116
                    trigger_error('Repository Not Found: ' . $infoReleasesUrl);
117
                } else {
118
                    $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

118
                    $file              = json_decode(/** @scrutinizer ignore-type */ $curlReturn, false);
Loading history...
119
                    $latestVersionLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
120
                    $latestVersion     = $file[0]->tag_name;
121
                    $prerelease        = $file[0]->prerelease;
122
                    if ('master' !== $latestVersionLink) {
123
                        $update = constant('CO_' . $moduleDirNameUpper . '_' . 'NEW_VERSION') . $latestVersion;
124
                    }
125
                    //"PHP-standardized" version
126
                    $latestVersion = mb_strtolower($latestVersion);
127
                    if (false !== mb_strpos($latestVersion, 'final')) {
128
                        $latestVersion = str_replace('_', '', mb_strtolower($latestVersion));
129
                        $latestVersion = str_replace('final', '', mb_strtolower($latestVersion));
130
                    }
131
                    $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

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

131
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
132
                    //"PHP-standardized" version
133
                    $moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion));
134
                    //                    $moduleVersion = '1.0'; //for testing only
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
                    //                    $moduleDirName = 'publisher'; //for testing only
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
                    if (!$prerelease && version_compare($moduleVersion, $latestVersion, '<')) {
137
                        $ret   = [];
138
                        $ret[] = $update;
139
                        $ret[] = $latestVersionLink;
140
                    }
141
                }
142
                curl_close($curlHandle);
143
            }
144
        }
145
        return $ret;
146
    }
147
}
148