VersionChecks::checkVerPhp()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 24
rs 9.4555
1
<?php
2
3
namespace XoopsModules\Pedigree\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
use XoopsModule;
16
17
/**
18
 * @copyright   XOOPS Project (https://xoops.org)
19
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @author      mamba <[email protected]>
21
 */
22
trait VersionChecks
23
{
24
    /**
25
     * Verifies XOOPS version meets minimum requirements for this module
26
     *
27
     * @param \XoopsModule|null $module
28
     * @param null|string       $requiredVer
29
     * @return bool true if meets requirements, false if not
30
     */
31
    public static function checkVerXoops(?\XoopsModule $module = null, ?string $requiredVer = null): bool
32
    {
33
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
34
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
35
        if (null === $module) {
36
            $module = \XoopsModule::getByDirname($moduleDirName);
37
        }
38
        \xoops_loadLanguage('admin', $moduleDirName);
39
        \xoops_loadLanguage('common', $moduleDirName);
40
41
        //check for minimum XOOPS version
42
        $currentVer = \mb_substr(\XOOPS_VERSION, 6); // get the numeric part of string
43
        if (null === $requiredVer) {
44
            $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

44
            $requiredVer = '' . /** @scrutinizer ignore-type */ $module->getInfo('min_xoops'); //making sure it's a string
Loading history...
45
        }
46
        $success = true;
47
48
        if (\version_compare($currentVer, $requiredVer, '<')) {
49
            $success = false;
50
            $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));
0 ignored issues
show
Bug introduced by
The method setErrors() does not exist on XoopsModule. ( Ignorable by Annotation )

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

50
            $module->/** @scrutinizer ignore-call */ 
51
                     setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer));

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...
51
        }
52
53
        return $success;
54
    }
55
56
    /**
57
     * Verifies PHP version meets minimum requirements for this module
58
     * @static
59
     *
60
     * @param \XoopsModule|null $module
61
     * @return bool true if meets requirements, false if not
62
     */
63
    public static function checkVerPhp(?\XoopsModule $module = null): bool
64
    {
65
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
66
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
67
        if (null === $module) {
68
            $module = \XoopsModule::getByDirname($moduleDirName);
69
        }
70
        \xoops_loadLanguage('admin', $moduleDirName);
71
        \xoops_loadLanguage('common', $moduleDirName);
72
73
        // check for minimum PHP version
74
        $success = false; // assume it fails until it passes test(s)
75
        $verNum  = \PHP_VERSION;
76
        $reqVer  = &$module->getInfo('min_php');
77
78
        if (false !== $reqVer && '' !== $reqVer) {
79
            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

79
            if (\version_compare($verNum, /** @scrutinizer ignore-type */ $reqVer, '<')) {
Loading history...
80
                $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 $values of sprintf() does only seem to accept double|integer|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

80
                $module->setErrors(\sprintf(\constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), /** @scrutinizer ignore-type */ $reqVer, $verNum));
Loading history...
81
            } else {
82
                $success = true;
83
            }
84
        }
85
86
        return $success;
87
    }
88
89
    /**
90
     * Compares current module version with latest GitHub release
91
     *
92
     * @static
93
     * @param \Xmf\Module\Helper $helper  module helper object
94
     * @param string|null        $source  repository location
95
     * @param string|null        $default repository branch to check
96
     *
97
     * @return array info about the latest module version, if newer
98
     */
99
    public static function checkVerModule(
100
        \Xmf\Module\Helper $helper,
101
        ?string $source = 'github',
102
        ?string $default = 'master'
103
    ): array {
104
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
105
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
106
        $update             = '';
107
        $repository         = 'XoopsModules25x/' . $moduleDirName;
108
        //$repository = 'XoopsModules25x/publisher'; //for testing only
109
        $ret             = [];
110
        $infoReleasesUrl = "https://api.github.com/repos/$repository/releases";
111
        if ('github' === $source) {
112
            if (\function_exists('curl_init') && false !== ($curlHandle = \curl_init())) {
113
                \curl_setopt($curlHandle, \CURLOPT_URL, $infoReleasesUrl);
114
                \curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
115
                \curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, true); //TODO: how to avoid an error when 'Peer's Certificate issuer is not recognized'
116
                \curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, ["User-Agent:Publisher\r\n"]);
117
                $curlReturn = \curl_exec($curlHandle);
118
                if (false === $curlReturn) {
119
                    \trigger_error(\curl_error($curlHandle));
120
                } elseif (false !== \mb_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 mb_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

120
                } elseif (false !== \mb_strpos(/** @scrutinizer ignore-type */ $curlReturn, 'Not Found')) {
Loading history...
121
                    \trigger_error('Repository Not Found: ' . $infoReleasesUrl);
122
                } else {
123
                    $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

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

136
                    $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

136
                    $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . /** @scrutinizer ignore-type */ $helper->getModule()->getInfo('module_status'));
Loading history...
137
                    //"PHP-standardized" version
138
                    $moduleVersion = \str_replace(' ', '', \mb_strtolower($moduleVersion));
139
                    //                    $moduleVersion = '1.0'; //for testing only
140
                    //                    $moduleDirName = 'publisher'; //for testing only
141
                    if (!$prerelease && \version_compare($moduleVersion, $latestVersion, '<')) {
142
                        $ret   = [];
143
                        $ret[] = $update;
144
                        $ret[] = $latestVersionLink;
145
                    }
146
                }
147
                \curl_close($curlHandle);
148
            }
149
        }
150
151
        return $ret;
152
    }
153
}
154