Completed
Push — dev ( 6c4435...afedfe )
by Steve
15:24
created

DownloaderComment::getMagentoYear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 6
Ratio 54.55 %

Importance

Changes 0
Metric Value
dl 6
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Mage Scan
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageScan
8
 * @package   MageScan
9
 * @author    Dardo Guidobono <[email protected]>
10
 * @copyright 2016 Dardo Guidobono
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magescan
13
 */
14
15
namespace MageScan\Check\Version;
16
17
use GuzzleHttp\Psr7\Response;
18
use MageScan\Check\AbstractCheck;
19
use MageScan\Check\Version;
20
21
/**
22
 * Scan for Magento edition and version via downloader url
23
 *
24
 * @category  MageScan
25
 * @package   MageScan
26
 * @author    Dardo Guidobono <[email protected]>
27
 * @copyright 2016 Dardo Guidobono
28
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
29
 * @link      https://github.com/steverobbins/magescan
30
 */
31
class DownloaderComment extends AbstractCheck
32
{
33
    /**
34
     * Guess magento edition and version
35
     *
36
     * @return array|boolean
37
     */
38
    public function getInfo()
39
    {
40
        $response = $this->getRequest()->get('downloader');
41
        $year = $this->getMagentoYear($response);
42
        if ($year) {
43
            $version = $this->getMagentoVersion($response);
44
            $edition = $this->getMagentoEditionByYearAndVersion($year, $version);
0 ignored issues
show
Security Bug introduced by
It seems like $version defined by $this->getMagentoVersion($response) on line 43 can also be of type false; however, MageScan\Check\Version\D...itionByYearAndVersion() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
45
            return [$edition, $version];
46
        }
47
        return false;
48
    }
49
50
    /**
51
     * Guess Magento year from downloader url
52
     *
53
     * @param Response $response
54
     *
55
     * @return string|boolean
56
     */
57
    public function getMagentoYear(Response $response)
58
    {
59
60 View Code Duplication
        if ($response->getStatusCode() == 200) {
61
            preg_match('/([0-9]{4}).*Magento/', $response->getBody(), $match);
62
            if (isset($match[1])) {
63
                return $match[1];
64
            }
65
        }
66
        return false;
67
    }
68
69
    /**
70
     * Guess Magento version from downloader body
71
     *
72
     * @param Response $response
73
     *
74
     * @return string|boolean
75
     */
76
    public function getMagentoVersion(Response $response)
77
    {
78 View Code Duplication
        if ($response->getStatusCode() == 200) {
79
            if (preg_match('/([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}(\.[0-9]{1,2})?)/', $response->getBody(), $match)) {
80
                return $match[1];
81
            }
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Guess Magento edition from copyright year and version
88
     *
89
     * @param string $year
90
     * @param string $version
91
     *
92
     * @return string
93
     */
94
    protected function getMagentoEditionByYearAndVersion($year, $version)
95
    {
96
        switch ($year) {
97
            case 2008:
98
                if (in_array($version, [
99
                    '1',
100
                    '1.1.1',
101
                    '1.1.2',
102
                    '1.1.3',
103
                    '1.1.4',
104
                    '1.1.5',
105
                    '1.1.6',
106
                    '1.1.7',
107
                    '1.1.8',
108
                    '1.2.0',
109
                    '1.2.0.1',
110
                    '1.2.0.2',
111
                    '1.2.0.3',
112
                    '1.2.1',
113
                    '1.2.1.1',
114
                    '1.2.1.2',
115
                    '1.3.0',
116
                    '1.3.1',
117
                    '1.3.1.1',
118
                    '1.3.2',
119
                    '1.3.2.1',
120
                    '1.3.2.2',
121
                    '1.3.2.3',
122
                    '1.3.2.4',
123
                    '1.3.3.0',
124
                    '1.4.0.0',
125
                    '1.4.0.1',
126
                    '1.4.1.0',
127
                    '1.4.1.1',
128
                    '1.4.2.0',
129
                ])
130
                ) {
131
                    return Version::EDITION_COMMUNITY;
132
                }
133
                break;
134
            case 2010:
135
                if (in_array($version, [
136
                    '1.5.0.0',
137
                    '1.5.0.1',
138
                    '1.5.1.0',
139
                    '1.6.0.0',
140
                    '1.6.1.0',
141
                    '1.6.2.0',
142
                ])
143
                ) {
144
                    return Version::EDITION_COMMUNITY;
145
                }
146
                break;
147
            case 2012:
148
                if (in_array($version, [
149
                    '1.7.0.0',
150
                    '1.7.0.1',
151
                    '1.7.0.2',
152
                ])
153
                ) {
154
                    return Version::EDITION_COMMUNITY;
155
                }
156
                break;
157
            case 2013:
158
                if (in_array($version, [
159
                    '1.8.0.0',
160
                    '1.8.1.0',
161
                ])
162
                ) {
163
                    return Version::EDITION_COMMUNITY;
164
                }
165
                break;
166
            case 2014:
167
                if (in_array($version, [
168
                    '1.9.0.0',
169
                    '1.9.0.1',
170
                    '1.9.1.0',
171
                    '1.9.1.1',
172
                ])
173
                ) {
174
                    return Version::EDITION_COMMUNITY;
175
                }
176
                break;
177
            case 2015:
178
                if (in_array($version, [
179
                    '1.9.1.1',
180
                    '1.9.2.0',
181
                    '1.9.2.1',
182
                    '1.9.2.2',
183
                ])
184
                ) {
185
                    return Version::EDITION_COMMUNITY;
186
                }
187
                break;
188
            case 2016:
189
                if (in_array($version, [
190
                    '1.9.2.3',
191
                    '1.9.2.4',
192
                ])
193
                ) {
194
                    return Version::EDITION_COMMUNITY;
195
                }
196
                break;
197
        }
198
        return Version::EDITION_ENTERPRISE;
199
    }
200
}
201