DBBackupFile   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 29.03 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 18
loc 62
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A exists() 0 4 1
A name() 0 4 1
A basename() 0 4 1
A dirname() 0 4 1
A extension() 0 4 1
A path() 0 4 1
A move() 9 9 2
A copy() 9 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
namespace LilyLabs\DBBackup;
4
5
use LilyLabs\DBBackup\Contracts\BackupFile;
6
use DateTime;
7
use Exception;
8
9
/**
10
 * Represents a database backup file generated
11
 *
12
 * @package LilyLabs\DBBackup
13
 * @author Abraham Chávez
14
 **/
15
class DBBackupFile implements BackupFile
16
{
17
    private $path;
18
19
    public function __construct(string $path)
20
    {
21
        $this->path = $path;
22
        if (!$this->exists()) {
23
            throw new Exception("The specified file don't exists [{$this->path}]");
24
        }
25
    }
26
27
    public function exists() : bool
28
    {
29
        return file_exists($this->path);
30
    }
31
32
    public function name() : string
33
    {
34
        return pathinfo($this->path, PATHINFO_FILENAME);
35
    }
36
37
    public function basename() : string
38
    {
39
        return pathinfo($this->path, PATHINFO_BASENAME);
40
    }
41
42
    public function dirname() : string
43
    {
44
        return pathinfo($this->path, PATHINFO_DIRNAME);
45
    }
46
47
    public function extension() : string
48
    {
49
        return pathinfo($this->path, PATHINFO_EXTENSION);
50
    }
51
52
    public function path() : string
53
    {
54
        return $this->path;
55
    }
56
57 View Code Duplication
    public function move(string $destination) : BackupFile
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...
58
    {
59
        if (!@rename($this->path, $destination)) {
60
            $error = error_get_last();
61
            $message = strip_tags($error['message']);
62
            throw new Exception("The backup file cannot be moved [{$message}]");
63
        }
64
        return new DBBackupFile($destination);
65
    }
66
67 View Code Duplication
    public function copy(string $destination) : BackupFile
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...
68
    {
69
        if (!@copy($this->path, $destination)) {
70
            $error = error_get_last();
71
            $message = strip_tags($error['message']);
72
            throw new Exception("The backup file cannot be copied [{$message}]");
73
        } 
74
        return new DBBackupFile($destination);
75
    }
76
} // END class BasicBackupFileNameGenerator