Failed Conditions
Pull Request — master (#136)
by
unknown
02:48
created

DownloaderComment   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 16.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 3
dl 11
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInfo() 0 11 2
A getMagentoYear() 6 11 3
A getMagentoVersion() 5 9 3
A getMagentoEditionByYearAndVersion() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Unused Code introduced by
The parameter $year is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $version is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        return 'Unknown';
97
    }
98
}
99