Directory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A recursiveSearchByExtension() 0 17 5
A isSubpath() 0 6 3
1
<?php
2
3
/**
4
 * Utility class for some useful directory operations
5
 *
6
 * @author matias
7
 */
8
namespace Columnis\Utils;
9
10
use RecursiveDirectoryIterator;
11
use RecursiveIteratorIterator;
12
13
class Directory
14
{
15
    /**
16
     * Scans a dir recursively for files that match the given extension
17
     *
18
     * @param string $extension
19
     * @return array|Traversable collections of files
20
     * @throws \Exception
21
     */
22 5
    public static function recursiveSearchByExtension($path, $extension, Array $excludes = null)
23
    {
24 5
        $files = array();
25 5
        if (!is_dir($path)) {
26 1
            throw new \Exception('Path given is not an existant directory.');
27
        }
28
29 4
        $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
30 4
        $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::LEAVES_ONLY);
31
32 4
        foreach ($iterator as $fileinfo) {
33 4
            if ($fileinfo->getExtension() == $extension && !Regex::matchesAny($fileinfo->getPathname(),$excludes)) {
34 4
                $files[] = realpath($fileinfo->getPathname());
35 4
            }
36 4
        }
37 4
        return $files;
38
    }
39
40
    /**
41
     * Checks if a path is inside another
42
     *
43
     * @param string $path
44
     * @param string $subpath
45
     * @return boolean
46
     */
47 5
    public static function isSubpath($path, $subpath)
48
    {
49 5
        $rpath = realpath($path);
50 5
        $rsubpath = realpath($subpath);
51 5
        return $rpath != false && $rsubpath != false && (strpos($rsubpath, $rpath) === 0);
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $rpath of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $rsubpath of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
52
    }
53
}
54