IncrementsPath   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIncrementedPath() 0 12 2
A fileExists() 0 7 1
A incrementPath() 0 8 2
A pathHasExtension() 0 4 1
1
<?php
2
3
namespace Druc\Flysystem\IncrementalNaming;
4
5
trait IncrementsPath
6
{
7
    /**
8
     * @param $filePath
9
     * @return string
10
     */
11 30
    private function getIncrementedPath($filePath)
12
    {
13 30
        $originalFilePath = $filePath;
14 30
        $increment = 1;
15
16 30
        while ($this->fileExists($filePath)) {
17 30
            $filePath = $this->incrementPath($originalFilePath, $increment);
18 30
            $increment++;
19 10
        }
20
21 30
        return $filePath;
22
    }
23
24
    /**
25
     * @param $filePath
26
     * @return bool
27
     */
28 30
    private function fileExists($filePath)
29
    {
30 30
        $directory = pathinfo($filePath, PATHINFO_DIRNAME);
31 30
        $existingPaths = array_column($this->filesystem->listContents($directory), 'path');
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        
33 30
        return in_array($filePath, $existingPaths);
34
    }
35
36
    /**
37
     * @param $originalFilePath
38
     * @param $increment
39
     * @return null|string|string[]
40
     */
41 30
    private function incrementPath($originalFilePath, $increment)
42
    {
43 30
        if ($this->pathHasExtension($originalFilePath)) {
44 12
            return preg_replace('#^(.+)\.([\w]+)$#i', '$1_' . $increment . '.$2', $originalFilePath);
45
        }
46
47 18
        return $originalFilePath . '_' . $increment;
48
    }
49
50
    /**
51
     * @param $filePath
52
     * @return bool|int
53
     */
54 30
    private function pathHasExtension($filePath)
55
    {
56 30
        return (bool)strpos($filePath, '.');
57
    }
58
}
59