FileParameter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 38
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFileFormats() 0 3 1
A isCompatibleWithFiles() 0 16 5
A getType() 0 3 1
1
<?php
2
3
namespace Startwind\Forrest\Command\Parameters;
4
5
/**
6
 * This parameter configuration class handles parameters that are file names.
7
 */
8
class FileParameter extends Parameter
9
{
10
    public const TYPE = 'file';
11
12
    public const DIRECTORY = 'directory';
13
14
    private array $fileFormats = [];
15
16
    public function setFileFormats(array $fileFormats): void
17
    {
18
        $this->fileFormats = $fileFormats;
19
    }
20
21
    /**
22
     * Return true if the given filename is compatible with this parameters
23
     * file formats.
24
     */
25
    public function isCompatibleWithFiles(array $compatibleFilenames): bool
26
    {
27
        $normalizedCompatibleFilenames = [];
28
29
        foreach ($compatibleFilenames as $compatibleFilename) {
30
            $normalizedCompatibleFilenames[] = strtolower($compatibleFilename);
31
        }
32
33
        foreach ($this->fileFormats as $fileFormat) {
34
            foreach ($normalizedCompatibleFilenames as $normalizedCompatibleFilename) {
35
                if (str_contains($normalizedCompatibleFilename, $fileFormat)) {
36
                    return true;
37
                }
38
            }
39
        }
40
        return false;
41
    }
42
43
    public function getType(): string
44
    {
45
        return self::TYPE;
46
    }
47
}
48