Locator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 6.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 8
loc 124
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B locate() 0 38 7
A getNormalizer() 0 4 1
A getSourcesFromFinder() 0 25 3
A removeLastSlash() 8 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
namespace JK\Sam\File;
4
5
use Exception;
6
use SplFileInfo;
7
use Symfony\Component\Finder\Finder;
8
9
class Locator
10
{
11
    /**
12
     * The normalizer associated to the file locator.
13
     *
14
     * @var Normalizer
15
     */
16
    protected $normalizer;
17
    
18
    /**
19
     * Locator constructor.
20
     *
21
     * @param Normalizer $normalizer
22
     */
23 5
    public function __construct(Normalizer $normalizer)
24
    {
25 5
        $this->normalizer = $normalizer;
26 5
    }
27
    
28
    /**
29
     * Locate a source either if it a file or a directory and normalize it. Return an array of SplFileInfo.
30
     *
31
     * @param mixed $source
32
     * @return SplFileInfo[]
33
     * @throws Exception
34
     */
35 5
    public function locate($source)
36
    {
37 5
        $sources = [];
38 5
        $normalizedSources = [];
39
        
40
        // if the wildcard is at the end of the string, it means that we look for a directory
41 5
        $endingWithWilCard = substr($source, strlen($source) - 2) === '/*';
42
        
43 5
        if ($endingWithWilCard) {
44 1
            $source = substr($source, 0, strlen($source) - 2);
45
        }
46
        
47 5
        if (is_dir($source) || $endingWithWilCard) {
48 2
            $finder = new Finder();
49
            $finder
50 2
                ->files()
51 2
                ->in($this->removeLastSlash($source))
52
            ;
53
            
54 2
            foreach ($finder as $file) {
55 2
                $sources[] = $file;
56
            }
57 3
        } else if (strstr($source, '*') !== false) {
58
            // if the source contains a wildcard, we use it with the finder component
59 2
            $sources = $this->getSourcesFromFinder($source);
60
        } else {
61 2
            $sources[] = $source;
62
        }
63
        
64
        // each found sources should be normalized
65 5
        foreach ($sources as $source) {
66 5
            $normalizedSources[] = $this
67 5
                ->normalizer
68 5
                ->normalize($source);
69
        }
70
        
71 5
        return $normalizedSources;
72
    }
73
    
74
    /**
75
     * Return the normalizer associated to the file locator.
76
     *
77
     * @return Normalizer
78
     */
79 2
    public function getNormalizer()
80
    {
81 2
        return $this->normalizer;
82
    }
83
    
84
    /**
85
     * Return files sources using the finder to allow wild wards.
86
     *
87
     * @param $source
88
     *
89
     * @return SplFileInfo[]
90
     */
91 2
    protected function getSourcesFromFinder($source)
92
    {
93 2
        $array = explode(DIRECTORY_SEPARATOR, $source);
94 2
        $filename = array_pop($array);
95 2
        $directory = $source;
96 2
        $pattern = '*';
97
        
98
        // if a dot is present, the last part is the filename pattern
99 2
        if (strstr($filename, '.') !== false) {
100 2
            $pattern = $filename;
101 2
            $directory = implode('/', $array);
102
        }
103 2
        $finder = new Finder();
104
        $finder
105 2
            ->name($pattern)
106 2
            ->in($directory);
107
        
108 2
        $sources = [];
109
        
110 2
        foreach ($finder as $source) {
111 2
            $sources[] = $source;
112
        }
113
        
114 2
        return $sources;
115
    }
116
    
117
    /**
118
     * Remove the last slash of the string.
119
     *
120
     * @param string $string
121
     *
122
     * @return string
123
     */
124 2 View Code Duplication
    private function removeLastSlash($string)
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...
125
    {
126 2
        if ('/' === substr($string, strlen($string) - 1)) {
127
            $string = substr($string, 0, strlen($string) - 1);
128
        }
129
        
130 2
        return $string;
131
    }
132
}
133