Code Duplication    Length = 46-46 lines in 2 locations

src/Strategy/Sha256Strategy.php 1 location

@@ 19-64 (lines=46) @@
16
use Humbug\SelfUpdate\Exception\HttpRequestException;
17
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
18
19
final class Sha256Strategy extends ShaStrategyAbstract
20
{
21
22
    /**
23
     * Retrieve the current version available remotely.
24
     *
25
     * @param Updater $updater
26
     * @return string|bool
27
     */
28
    public function getCurrentRemoteVersion(Updater $updater)
29
    {
30
        /** Switch remote request errors to HttpRequestExceptions */
31
        set_error_handler(array($updater, 'throwHttpRequestException'));
32
        $version = humbug_get_contents($this->getVersionUrl());
33
        restore_error_handler();
34
        if (false === $version) {
35
            throw new HttpRequestException(sprintf(
36
                'Request to URL failed: %s', $this->getVersionUrl()
37
            ));
38
        }
39
        if (empty($version)) {
40
            throw new HttpRequestException(
41
                'Version request returned empty response.'
42
            );
43
        }
44
        if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) {
45
            throw new HttpRequestException(
46
                'Version request returned incorrectly formatted response.'
47
            );
48
        }
49
50
        return $matches[0];
51
    }
52
53
    /**
54
     * Retrieve the current version of the local phar file.
55
     *
56
     * @param Updater $updater
57
     * @return string
58
     */
59
    public function getCurrentLocalVersion(Updater $updater)
60
    {
61
        return hash_file('sha256', $updater->getLocalPharFile());
62
    }
63
64
}

src/Strategy/ShaStrategy.php 1 location

@@ 22-67 (lines=46) @@
19
/**
20
 * @deprecated 1.0.4 SHA-1 is increasingly susceptible to collision attacks; use SHA-256
21
 */
22
class ShaStrategy extends ShaStrategyAbstract
23
{
24
25
    /**
26
     * Retrieve the current version available remotely.
27
     *
28
     * @param Updater $updater
29
     * @return string|bool
30
     */
31
    public function getCurrentRemoteVersion(Updater $updater)
32
    {
33
        /** Switch remote request errors to HttpRequestExceptions */
34
        set_error_handler(array($updater, 'throwHttpRequestException'));
35
        $version = humbug_get_contents($this->getVersionUrl());
36
        restore_error_handler();
37
        if (false === $version) {
38
            throw new HttpRequestException(sprintf(
39
                'Request to URL failed: %s', $this->getVersionUrl()
40
            ));
41
        }
42
        if (empty($version)) {
43
            throw new HttpRequestException(
44
                'Version request returned empty response.'
45
            );
46
        }
47
        if (!preg_match('%^[a-z0-9]{40}%', $version, $matches)) {
48
            throw new HttpRequestException(
49
                'Version request returned incorrectly formatted response.'
50
            );
51
        }
52
53
        return $matches[0];
54
    }
55
56
    /**
57
     * Retrieve the current version of the local phar file.
58
     *
59
     * @param Updater $updater
60
     * @return string
61
     */
62
    public function getCurrentLocalVersion(Updater $updater)
63
    {
64
        return sha1_file($updater->getLocalPharFile());
65
    }
66
67
}
68