Passed
Pull Request — master (#42)
by Michael
01:49
created

VersionChecks::checkVerModule()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 3
dl 0
loc 30
rs 9.3888
c 0
b 0
f 0
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
     * Verifies XOOPS version meets minimum requirements for this module
24
     * @static
25
     * @param \XoopsModule|null $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(__DIR__));
33
        if (null === $module) {
34
            $module = \XoopsModule::getByDirname($moduleDirName);
35
        }
36
        xoops_loadLanguage('admin', $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(_AM_WFL_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Tdmdownload..._AM_WFL_ERROR_BAD_XOOPS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
        }
49
50
        return $success;
51
    }
52
53
    /**
54
     * Verifies PHP version meets minimum requirements for this module
55
     * @static
56
     * @param \XoopsModule $module
57
     *
58
     * @return bool true if meets requirements, false if not
59
     */
60
    public static function checkVerPhp(\XoopsModule $module)
61
    {
62
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
63
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
64
        xoops_loadLanguage('admin', $module->dirname());
0 ignored issues
show
Bug introduced by
It seems like $module->dirname() can also be of type array and array; however, parameter $domain of xoops_loadLanguage() 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

64
        xoops_loadLanguage('admin', /** @scrutinizer ignore-type */ $module->dirname());
Loading history...
65
        // check for minimum PHP version
66
        $success = true;
67
        $verNum  = PHP_VERSION;
68
        $reqVer  = $module->getInfo('min_php');
69
        if (false !== $reqVer && '' !== $reqVer) {
70
            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

70
            if (version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
71
                $module->setErrors(sprintf(_AM_WFL_ERROR_BAD_PHP, $reqVer, $verNum));
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Tdmdownload...n\_AM_WFL_ERROR_BAD_PHP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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

71
                $module->setErrors(sprintf(_AM_WFL_ERROR_BAD_PHP, /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
72
                $success = false;
73
            }
74
        }
75
76
        return $success;
77
    }
78
79
    /**
80
     *
81
     * compares current module version with latest GitHub release
82
     * @static
83
     * @param \Xmf\Module\Helper $helper
84
     * @param string             $location
85
     * @param string             $default
86
     *
87
     * @return string link to the latest module version, if newer
88
     */
89
90
    public static function checkVerModule($helper, $location = 'github', $default = 'master')
91
    {
92
        $moduleDirName = basename(dirname(dirname(__DIR__)));
93
        $update        = '';
94
        $repository    = 'XoopsModules25x/' . $moduleDirName;
95
        //        $repository    = 'XoopsModules25x/publisher'; //uncomment for testing
96
        $ret = '';
97
98
        if ('github' === $location) {
99
            $file              = @json_decode(@file_get_contents("https://api.github.com/repos/$repository/releases", false, stream_context_create(['http' => ['header' => "User-Agent:Publisher\r\n"]])));
100
            $latestReleaseLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default);
101
            $latestRelease     = substr(strrchr($latestReleaseLink, "/"), 1, -4);
102
            if ('master' !== $latestRelease) {
103
                $update = '<span><strong> Latest Release: </strong>' . '   <a href="' . $latestReleaseLink . '">' . $latestRelease . '</a> </span><br><br>';
104
            }
105
106
            $moduleVersion = round($helper->getConfig('version') / 100, 2);
107
            //        $moduleVersion = '3.0'; //uncomment for testing
108
109
            $ret .= "<div align='center'>";
110
111
            //            $ret .= "<a href='https://xoops.org/'><img src='../assets/images/icons/32/xoopsmicrobutton.gif'></a><br>";
112
            if (version_compare($moduleVersion, $latestRelease, '<')) {
113
                //                $ret .= "| ";
114
                $ret .= " <span style='color: #FF0000; font-size:11px' " . 'Latest Release: </span>' . $update . "<a href='https://github.com/XoopsModules25x/$moduleDirName/releases/'>";
115
                $ret .= "<img src='https://img.shields.io/github/release/XoopsModules25x/$moduleDirName.svg?style=flat'></a>";
116
            }
117
        }
118
        $GLOBALS['xoopsTpl']->assign('latestModRelease', $ret);
119
        return $ret;
120
    }
121
}
122