ViewDiffService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewFileHashIndex() 0 11 2
A checksumMatchesAny() 0 3 1
A getChecksums() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Hyde;
8
9
use function Hyde\unixsum_file;
10
use function str_replace;
11
use function in_array;
12
use function Hyde\unslash;
13
use function glob;
14
15
/**
16
 * @internal This class may be refactored to better suit its intended purpose.
17
 *
18
 * Helper methods to interact with the virtual filecache that is used to compare
19
 * published Blade views with the original Blade views in the Hyde Framework
20
 * so the user can be warned before overwriting their customizations.
21
 *
22
 * Since we currently never use this class more than one in the request cycle,
23
 * there is no reason to cache the results of the file index in the instance.
24
 */
25
class ViewDiffService
26
{
27
    /** @return array<string, array{unixsum: string}> */
28
    public static function getViewFileHashIndex(): array
29
    {
30
        $filecache = [];
31
32
        foreach (glob(Hyde::vendorPath('resources/views/**/*.blade.php')) as $file) {
33
            $filecache[unslash(str_replace(Hyde::vendorPath(), '', (string) $file))] = [
0 ignored issues
show
Bug introduced by
The method vendorPath() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

33
            $filecache[unslash(str_replace(Hyde::/** @scrutinizer ignore-call */ vendorPath(), '', (string) $file))] = [
Loading history...
34
                'unixsum' => unixsum_file($file),
35
            ];
36
        }
37
38
        return $filecache;
39
    }
40
41
    /** @return array<string> */
42
    public static function getChecksums(): array
43
    {
44
        $checksums = [];
45
46
        foreach (static::getViewFileHashIndex() as $file) {
47
            $checksums[] = $file['unixsum'];
48
        }
49
50
        return $checksums;
51
    }
52
53
    public static function checksumMatchesAny(string $checksum): bool
54
    {
55
        return in_array($checksum, static::getChecksums());
56
    }
57
}
58