CopyRequest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 11.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.8%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 12
loc 103
ccs 36
cts 41
cp 0.878
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A makeSuccess() 0 4 1
A __destruct() 0 12 4
A __construct() 12 12 3
A getCurlOptions() 0 17 3
A setDestination() 0 11 2
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 = true;
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
            $useRedirector,
44 5
            $config->get('github-domains') ?: array(),
45 5
            $config->get('gitlab-domains') ?: array()
46
        );
47 5
    }
48
49 5
    public function __destruct()
50
    {
51 5
        if ($this->fp) {
52 5
            fclose($this->fp);
53
        }
54
55 5
        if (!$this->success) {
56 5
            if (file_exists($this->destination)) {
57 5
                unlink($this->destination);
58
            }
59
        }
60 5
    }
61
62 1
    public function makeSuccess()
63
    {
64 1
        $this->success = true;
65 1
    }
66
67
    /**
68
     * @return array
69
     */
70 3
    public function getCurlOptions()
71
    {
72 3
        if ($this->fp) {
73 3
            fclose($this->fp);
74 3
        }
75
        $this->success = false;
76
        $this->fp = fopen($this->destination, 'wb');
77
        if (!$this->fp) {
78
            throw new FetchException(
79
                'The file could not be written to ' . $this->destination
80 6
            );
81
        }
82 6
83 6
        $curlOpts = parent::getCurlOptions();
84 1
        $curlOpts[CURLOPT_FILE] = $this->fp;
85 1
        return $curlOpts;
86
    }
87
88
    /**
89 5
     * @param string
90
     */
91 5
    public function setDestination($destination)
92 5
    {
93
        $this->destination = $destination;
94
        if (is_dir($destination)) {
95
            throw new FetchException(
96
                'The file could not be written to ' . $destination . '. Directory exists.'
97 5
            );
98
        }
99 5
100
        $this->createDir($destination);
101 5
    }
102 5
103
    private function createDir($fileName)
104
    {
105
        $targetdir = dirname($fileName);
106
        if (!file_exists($targetdir)) {
107
            if (!mkdir($targetdir, 0775, true)) {
108
                throw new FetchException(
109 5
                    'The file could not be written to ' . $fileName
110
                );
111
            }
112
        }
113
    }
114
}
115