Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

FileUpdate::isNotUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 glob;
19
use function max;
20
use function preg_quote;
21
use function sprintf;
22
23
final class FileUpdate
24
{
25
    /** @var int */
26
    private $updateTime;
27
28
    /** @var string */
29
    private $srcRegex;
30
31
    /** @var string */
32
    private $varRegex;
33
34
    public function __construct(AbstractAppMeta $meta)
35
    {
36
        $basePath = preg_quote($meta->appDir . '/', '/');
37
        $srcPath = $basePath . 'src\/';
38
        $varPath = $basePath . 'var\/';
39
        $this->srcRegex = sprintf('/^(?!.*(%s)).*?$/', $srcPath . 'Resource');
40
        $this->varRegex = sprintf('/^(?!.*(%s|%s|%s|%s)).*?$/', $varPath . 'tmp', $varPath . 'log', $varPath . 'templates', $varPath . 'phinx');
41
        $this->updateTime = $this->getLatestUpdateTime($meta);
42
    }
43
44
    public function isNotUpdated(AbstractAppMeta $meta): bool
45
    {
46
        return $this->getLatestUpdateTime($meta) === $this->updateTime;
47
    }
48
49
    public function getLatestUpdateTime(AbstractAppMeta $meta): int
50
    {
51
        $srcFiles = $this->getFiles($meta->appDir . '/src', $this->srcRegex);
52
        $varFiles = $this->getFiles($meta->appDir . '/var', $this->varRegex);
53
        $envFiles = (array) glob($meta->appDir . '/.env*');
54
        $scanFiles = array_merge($srcFiles, $varFiles, $envFiles);
55
        $composerLock = $meta->appDir . '/composer.lock';
56
        if (file_exists($composerLock)) {
57
            $scanFiles[] = $composerLock;
58
        }
59
60
        /** @psalm-suppress all -- ignore filemtime could return false */
61
        return (int) max(array_map('filemtime', $scanFiles));
62
    }
63
64
    /**
65
     * @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...
66
     */
67
    private function getFiles(string $path, string $regex): array
68
    {
69
        $iterator = new RegexIterator(
70
            new RecursiveIteratorIterator(
71
                new RecursiveDirectoryIterator(
72
                    $path,
73
                    FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
74
                ),
75
                RecursiveIteratorIterator::LEAVES_ONLY
76
            ),
77
            $regex,
78
            RecursiveRegexIterator::MATCH
79
        );
80
81
        $files = [];
82
        /** @var SplFileInfo $fileInfo */
83
        foreach ($iterator as $fileName => $fileInfo) {
84
            if ($fileInfo->isFile() && $fileInfo->getFilename()[0] !== '.') {
85
                $files[] = (string) $fileName;
86
            }
87
        }
88
89
        return $files;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $files returns the type array|string[] which is incompatible with the documented return type BEAR\Package\Injector\list.
Loading history...
90
    }
91
}
92