Sha256Strategy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getCurrentRemoteVersion() 24 24 4
A getCurrentLocalVersion() 4 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
 * Humbug
4
 *
5
 * @category   Humbug
6
 * @package    Humbug
7
 * @copyright  Copyright (c) 2017 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\Updater;
17
use function Humbug\get_contents;
18
19 View Code Duplication
final class Sha256Strategy extends ShaStrategyAbstract
0 ignored issues
show
Duplication introduced by
This class 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...
20
{
21
    /**
22
     * Retrieve the current version available remotely.
23
     *
24
     * @param Updater $updater
25
     * @return string|bool
26
     */
27
    public function getCurrentRemoteVersion(Updater $updater)
28
    {
29
        /** Switch remote request errors to HttpRequestExceptions */
30
        set_error_handler(array($updater, 'throwHttpRequestException'));
31
        $version = get_contents($this->getVersionUrl());
32
        restore_error_handler();
33
        if (false === $version) {
34
            throw new HttpRequestException(sprintf(
35
                'Request to URL failed: %s', $this->getVersionUrl()
36
            ));
37
        }
38
        if (empty($version)) {
39
            throw new HttpRequestException(
40
                'Version request returned empty response.'
41
            );
42
        }
43
        if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) {
44
            throw new HttpRequestException(
45
                'Version request returned incorrectly formatted response.'
46
            );
47
        }
48
49
        return $matches[0];
50
    }
51
52
    /**
53
     * Retrieve the current version of the local phar file.
54
     *
55
     * @param Updater $updater
56
     * @return string
57
     */
58
    public function getCurrentLocalVersion(Updater $updater)
59
    {
60
        return hash_file('sha256', $updater->getLocalPharFile());
61
    }
62
}
63