Passed
Pull Request — master (#5)
by Michael
02:50
created

VersionChecks   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkVerXoops() 0 21 4
A checkVerPhp() 0 17 4
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