ShaStrategyAbstract   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 14
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 14 14 2
A getVersionUrl() 0 4 1
A validateAllowedUrl() 0 7 2
A setPharUrl() 0 9 2
A getPharUrl() 0 4 1
A setVersionUrl() 0 9 2

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
 * Humbug
4
 *
5
 * @category   Humbug
6
 * @package    Humbug
7
 * @copyright  Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
8
 * @license    https://github.com/padraic/phar-updater/blob/master/LICENSE New BSD License
9
 *
10
 * This class is partially patterned after Composer's self-update.
11
 */
12
13
namespace Humbug\SelfUpdate\Strategy;
14
15
use Humbug\SelfUpdate\Exception\HttpRequestException;
16
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
17
use Humbug\SelfUpdate\Updater;
18
use function Humbug\get_contents;
19
20
abstract class ShaStrategyAbstract implements StrategyInterface
21
{
22
    /** @private */
23
    const SUPPORTED_SCHEMES = [
24
        'http',
25
        'https',
26
        'file',
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    protected $versionUrl;
33
34
    /**
35
     * @var string
36
     */
37
    protected $pharUrl;
38
39
    /**
40
     * Download the remote Phar file.
41
     *
42
     * @param Updater $updater
43
     * @return void
44
     */
45 View Code Duplication
    public function download(Updater $updater)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        /** Switch remote request errors to HttpRequestExceptions */
48
        set_error_handler(array($updater, 'throwHttpRequestException'));
49
        $result = get_contents($this->getPharUrl());
50
        restore_error_handler();
51
        if (false === $result) {
52
            throw new HttpRequestException(sprintf(
53
                'Request to URL failed: %s', $this->getPharUrl()
54
            ));
55
        }
56
57
        file_put_contents($updater->getTempPharFile(), $result);
58
    }
59
60
    /**
61
     * Set URL to phar file
62
     *
63
     * @param string $url
64
     */
65
    public function setPharUrl($url)
66
    {
67
        if (!$this->validateAllowedUrl($url)) {
68
            throw new InvalidArgumentException(
69
                sprintf('Invalid url passed as argument: %s.', $url)
70
            );
71
        }
72
        $this->pharUrl = $url;
73
    }
74
75
    /**
76
     * Get URL for phar file
77
     *
78
     * @return string
79
     */
80
    public function getPharUrl()
81
    {
82
        return $this->pharUrl;
83
    }
84
85
    /**
86
     * Set URL to version file
87
     *
88
     * @param string $url
89
     */
90
    public function setVersionUrl($url)
91
    {
92
        if (!$this->validateAllowedUrl($url)) {
93
            throw new InvalidArgumentException(
94
                sprintf('Invalid url passed as argument: %s.', $url)
95
            );
96
        }
97
        $this->versionUrl = $url;
98
    }
99
100
    /**
101
     * Get URL for version file
102
     *
103
     * @return string
104
     */
105
    public function getVersionUrl()
106
    {
107
        return $this->versionUrl;
108
    }
109
110
    protected function validateAllowedUrl($url)
111
    {
112
        return (
113
            filter_var($url, FILTER_VALIDATE_URL)
114
            && in_array(parse_url($url, PHP_URL_SCHEME), self::SUPPORTED_SCHEMES)
115
        );
116
    }
117
}
118