Passed
Pull Request — master (#1)
by Nikolay
01:42
created

FileSystemHash::hashPathName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Intersvyaz\AssetManager;
3
4
use RecursiveDirectoryIterator;
5
use RecursiveIteratorIterator;
6
use SplFileInfo;
7
use Yii;
8
9
class FileSystemHash implements FileSystemHashInterface
10
{
11
    /**
12
     * @inheritdoc
13
     */
14
    public function hashPath($path)
15
    {
16
        $path = realpath($path);
17
        $hashes = [];
18
19
        if (is_dir($path)) {
20
            $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
21
22
            /** @var SplFileInfo $file */
23
            foreach ($it as $file) {
24
                if ($file->isFile()) {
25
                    $hashes[] = md5_file($file);
26
                }
27
            }
28
29
            if (empty($hashes)) {
30
                $hashes[] = $this->hashPathName($path);
31
            } else {
32
                sort($hashes);
33
            }
34
        } else {
35
            $hashes[] = md5_file($path);
36
        }
37
38
        return md5(implode($hashes, '|'));
0 ignored issues
show
Bug introduced by
'|' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        return md5(implode($hashes, /** @scrutinizer ignore-type */ '|'));
Loading history...
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function hashPathName($path)
45
    {
46
        $hashPath = str_replace(Yii::getAlias('@app'), '', $path);
47
48
        if (DIRECTORY_SEPARATOR === '\\') {
49
            $hashPath = str_replace(DIRECTORY_SEPARATOR, '/', $hashPath);
50
        }
51
52
        return md5($hashPath);
53
    }
54
}
55