Failed Conditions
Push — master ( 7ad908...9f92fb )
by Steve
02:47
created

getMagentoEditionByYearAndVersion()   D

Complexity

Conditions 15
Paths 15

Size

Total Lines 99
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 99
rs 4.9121
cc 15
eloc 82
nc 15
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $year of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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
                    return Version::EDITION_COMMUNITY;
131
                }
132
                break;
133
            case 2010:
134
                if (in_array($version, [
135
                    '1.5.0.0',
136
                    '1.5.0.1',
137
                    '1.5.1.0',
138
                    '1.6.0.0',
139
                    '1.6.1.0',
140
                    '1.6.2.0',
141
                ])) {
142
                    return Version::EDITION_COMMUNITY;
143
                }
144
                break;
145
            case 2012:
146
                if (in_array($version, [
147
                    '1.7.0.0',
148
                    '1.7.0.1',
149
                    '1.7.0.2',
150
                ])) {
151
                    return Version::EDITION_COMMUNITY;
152
                }
153
                break;
154
            case 2013:
155
                if (in_array($version, [
156
                    '1.8.0.0',
157
                    '1.8.1.0',
158
                ])) {
159
                    return Version::EDITION_COMMUNITY;
160
                }
161
                break;
162
            case 2014:
163
                if (in_array($version, [
164
                    '1.9.0.0',
165
                    '1.9.0.1',
166
                    '1.9.1.0',
167
                    '1.9.1.1',
168
                ])) {
169
                    return Version::EDITION_COMMUNITY;
170
                }
171
                break;
172
            case 2015:
173
                if (in_array($version, [
174
                    '1.9.1.1',
175
                    '1.9.2.0',
176
                    '1.9.2.1',
177
                    '1.9.2.2',
178
                ])) {
179
                    return Version::EDITION_COMMUNITY;
180
                }
181
                break;
182
            case 2016:
183
                if (in_array($version, [
184
                    '1.9.2.3',
185
                    '1.9.2.4',
186
                ])) {
187
                    return Version::EDITION_COMMUNITY;
188
                }
189
                break;
190
        }
191
        return Version::EDITION_ENTERPRISE;
192
    }
193
}
194