Completed
Push — 1.x ( 8c45b5...c3bf52 )
by Akihito
15s queued 13s
created

FileUpdate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
c 1
b 0
f 0
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isNotUpdated() 0 3 1
A __construct() 0 8 1
A getLatestUpdateTime() 0 13 2
A filemtime() 0 3 1
A getFiles() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Injector;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use FilesystemIterator;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use RecursiveRegexIterator;
12
use RegexIterator;
13
use SplFileInfo;
14
15
use function array_map;
16
use function array_merge;
17
use function file_exists;
18
use function filemtime;
19
use function glob;
20
use function max;
21
use function preg_quote;
22
use function sprintf;
23
24
final class FileUpdate
25
{
26
    /** @var int */
27
    private $updateTime;
28
29
    /** @var string */
30
    private $srcRegex;
31
32
    /** @var string */
33
    private $varRegex;
34
35
    public function __construct(AbstractAppMeta $meta)
36
    {
37
        $basePath = preg_quote($meta->appDir . '/', '/');
38
        $srcPath = $basePath . 'src\/';
39
        $varPath = $basePath . 'var\/';
40
        $this->srcRegex = sprintf('/^(?!.*(%s)).*?$/', $srcPath . 'Resource');
41
        $this->varRegex = sprintf('/^(?!.*(%s|%s|%s|%s)).*?$/', $varPath . 'tmp', $varPath . 'log', $varPath . 'templates', $varPath . 'phinx');
42
        $this->updateTime = $this->getLatestUpdateTime($meta);
43
    }
44
45
    public function isNotUpdated(AbstractAppMeta $meta): bool
46
    {
47
        return $this->getLatestUpdateTime($meta) === $this->updateTime;
48
    }
49
50
    public function getLatestUpdateTime(AbstractAppMeta $meta): int
51
    {
52
        $srcFiles = $this->getFiles($meta->appDir . '/src', $this->srcRegex);
53
        $varFiles = $this->getFiles($meta->appDir . '/var', $this->varRegex);
54
        $envFiles = (array) glob($meta->appDir . '/.env*');
55
        $scanFiles = array_merge($srcFiles, $varFiles, $envFiles);
56
        $composerLock = $meta->appDir . '/composer.lock';
57
        if (file_exists($composerLock)) {
58
            $scanFiles[] = $composerLock;
59
        }
60
61
        /** @psalm-suppress all -- ignore filemtime could return false */
62
        return (int) max(array_map([$this, 'filemtime'], $scanFiles));
63
    }
64
65
    /**
66
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
67
     */
68
    private function filemtime(string $filename): string
69
    {
70
        return (string) filemtime($filename);
71
    }
72
73
    /**
74
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Injector\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
     */
76
    private function getFiles(string $path, string $regex): array
77
    {
78
        $iterator = new RegexIterator(
79
            new RecursiveIteratorIterator(
80
                new RecursiveDirectoryIterator(
81
                    $path,
82
                    FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
83
                ),
84
                RecursiveIteratorIterator::LEAVES_ONLY
85
            ),
86
            $regex,
87
            RecursiveRegexIterator::MATCH
88
        );
89
90
        $files = [];
91
        /** @var RegexIterator<string, SplFileInfo> $iterator */
92
        foreach ($iterator as $fileName => $fileInfo) {
93
            if ($fileInfo->isFile() && $fileInfo->getFilename()[0] !== '.') {
94
                $files[] = $fileName;
95
            }
96
        }
97
98
        return $files;
99
    }
100
}
101