File   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 3.85 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 4
loc 104
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A guessExtension() 0 4 1
A guessMimeType() 0 4 1
A move() 4 13 2
A getTargetFile() 0 14 6
A customChmod() 0 6 2
A getName() 0 8 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
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\File;
6
7
use Pivasic\Bundle\Common\File\Exception\FileException;
8
use Pivasic\Bundle\Common\File\Exception\FileNotFoundException;
9
use Pivasic\Bundle\Common\File\Extension\Extension;
10
use Pivasic\Bundle\Common\File\MimeType\MimeType;
11
12
/**
13
 * Class File
14
 * @package Pivasic\Bundle\Common\File
15
 */
16
class File extends \SplFileInfo
17
{
18
    /**
19
     * @param string $path      The path to the file
20
     * @param bool   $checkPath Whether to check the path or not
21
     * @throws FileNotFoundException
22
     */
23
    public function __construct(string $path, bool $checkPath = true)
24
    {
25
        if ($checkPath && !is_file($path)) {
26
            throw new FileNotFoundException($path);
27
        }
28
29
        parent::__construct($path);
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function guessExtension(): string
36
    {
37
        return Extension::getInstance()->guess($this->guessMimeType());
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function guessMimeType(): string
44
    {
45
        return MimeType::getInstance()->guess($this->getPathname());
46
    }
47
48
    /**
49
     * Moves the file to a new location.
50
     *
51
     * @param string $directory   Destination folder
52
     * @param string $name   New file name
53
     * @return File
54
     * @throws FileException
55
     */
56
    public function move(string $directory, string $name = ''): File
57
    {
58
        $target = $this->getTargetFile($directory, $name);
59
60 View Code Duplication
        if (!@rename($this->getPathname(), $target)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
            $error = error_get_last();
62
            throw new FileException(sprintf('Could not rename the file "%s" (%s)', $this->getPathname(), strip_tags($error['message'])));
63
        }
64
65
        $this->customChmod($target);
66
67
        return $target;
68
    }
69
70
    /**
71
     * @param string $directory
72
     * @param string $name
73
     * @return File
74
     * @throws FileException
75
     */
76
    protected function getTargetFile(string $directory, string $name = ''): File
77
    {
78
        if (!is_dir($directory)) {
79
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
80
                throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
81
            }
82
        } elseif (!is_writable($directory)) {
83
            throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
84
        }
85
86
        $target = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . ('' == $name ? $this->getBasename() : $this->getName($name));
87
88
        return new self($target, false);
89
    }
90
91
    /**
92
     * Chmod function with exception
93
     *
94
     * @param $target
95
     * @param $mode
96
     * @throws FileException
97
     */
98
    protected function customChmod(string $target, $mode = 0666)
99
    {
100
        if (false === @chmod($target, $mode & ~umask())) {
101
            throw new FileException(sprintf('Unable to change mode of the "%s"', $target));
102
        }
103
    }
104
105
    /**
106
     * Returns locale independent base name of the given path.
107
     *
108
     * @param string $name The new file name
109
     * @return string containing
110
     */
111
    protected function getName(string $name): string
112
    {
113
        $originalName = str_replace('\\', '/', $name);
114
        $pos = strrpos($originalName, '/');
115
        $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
116
117
        return $originalName;
118
    }
119
}