Completed
Push — master ( f863d7...e85477 )
by Hiraku
11s
created

CopyRequest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 3
dl 12
loc 99
ccs 42
cts 48
cp 0.875
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 3
A __destruct() 0 12 4
A makeSuccess() 0 4 1
A getCurlOptions() 0 6 1
A setDestination() 0 18 3
A createDir() 0 11 3

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
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\IO;
10
use Composer\Config;
11
12
class CopyRequest extends BaseRequest
13
{
14
    /** @var string */
15
    private $destination;
16
17
    /** @var resource<stream<plainfile>> */
18
    private $fp;
19
20
    private $success = false;
21
22
    protected static $defaultCurlOptions = array(
23
        CURLOPT_HTTPGET => true,
24
        CURLOPT_FOLLOWLOCATION => true,
25
        CURLOPT_MAXREDIRS => 20,
26
        CURLOPT_ENCODING => '',
27
    );
28
29
    /**
30
     * @param string $url
31
     * @param string $destination
32
     * @param bool $useRedirector
33
     * @param IO\IOInterface $io
34
     * @param Config $config
35
     */
36 6 View Code Duplication
    public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config)
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...
37
    {
38 6
        $this->setURL($url);
39 6
        $this->setDestination($destination);
40 5
        $this->setCA($config->get('capath'), $config->get('cafile'));
41 5
        $this->setupAuthentication(
42 5
            $io,
43 5
            $useRedirector,
44 5
            $config->get('github-domains') ?: array(),
45 5
            $config->get('gitlab-domains') ?: array()
46 5
        );
47 5
    }
48
49 5
    public function __destruct()
50
    {
51 5
        if ($this->fp) {
52 5
            fclose($this->fp);
53 5
        }
54
55 5
        if (!$this->success) {
56 5
            if (file_exists($this->destination)) {
57 5
                unlink($this->destination);
58 5
            }
59 5
        }
60 5
    }
61
62 1
    public function makeSuccess()
63
    {
64 1
        $this->success = true;
65 1
    }
66
67
    /**
68
     * @return array
69
     */
70 2
    public function getCurlOptions()
71
    {
72 2
        $curlOpts = parent::getCurlOptions();
73 2
        $curlOpts[CURLOPT_FILE] = $this->fp;
74 2
        return $curlOpts;
75
    }
76
77
    /**
78
     * @param string
79
     */
80 6
    public function setDestination($destination)
81
    {
82 6
        $this->destination = $destination;
83 6
        if (is_dir($destination)) {
84 1
            throw new FetchException(
85 1
                'The file could not be written to ' . $destination . '. Directory exists.'
86 1
            );
87
        }
88
89 5
        $this->createDir($destination);
90
91 5
        $this->fp = fopen($destination, 'wb');
92 5
        if (!$this->fp) {
93
            throw new FetchException(
94
                'The file could not be written to ' . $destination
95
            );
96
        }
97 5
    }
98
99 5
    private function createDir($fileName)
100
    {
101 5
        $targetdir = dirname($fileName);
102 5
        if (!file_exists($targetdir)) {
103
            if (!mkdir($targetdir, 0775, true)) {
104
                throw new FetchException(
105
                    'The file could not be written to ' . $fileName
106
                );
107
            }
108
        }
109 5
    }
110
}
111