Passed
Pull Request — master (#168)
by Akihito
01:45
created

ConfigFactory::fromFile()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 18
rs 9.2222
cc 6
nc 32
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use Koriym\DataFile\XmlConfigLoad;
8
use SimpleXMLElement;
9
10
use function assert;
11
use function dirname;
12
use function is_dir;
13
use function is_file;
14
use function property_exists;
15
use function realpath;
16
use function sprintf;
17
18
final class ConfigFactory
19
{
20
    /**
21
     * @param array<string>              $argv
22
     * @param array<string, string|bool> $options
23
     */
24
    public static function fromFile(string $configFile, int $argc = 1, array $argv = [''], array $options = []): Config
25
    {
26
        $xml = (new XmlConfigLoad('asd.xml'))($configFile, dirname(__DIR__) . '/docs/asd.xsd');
27
        assert(property_exists($xml, 'watch'));
28
        $dir = is_dir($configFile) ? $configFile : dirname($configFile);
29
30
        $maybePath = (string) realpath($argv[$argc - 1]);
31
        $profile = is_file($maybePath) && $configFile !== $maybePath ? $maybePath : sprintf('%s/%s', $dir, (string) $xml->alpsFile);
32
        /** @var ?SimpleXMLElement $filter */
33
        $filter = property_exists($xml, 'filter') ? $xml->filter : null;
34
        $mode = property_exists($xml, 'mode') ? (string) $xml->mode : DumpDocs::MODE_HTML;
35
        $option = new Option($options, $filter);
36
37
        return new Config(
38
            $profile,
39
            $option->watch,
40
            new ConfigFilter($option->and, $option->or, $option->color),
0 ignored issues
show
Bug introduced by
$option->and of type Koriym\AppStateDiagram\list is incompatible with the type array expected by parameter $and of Koriym\AppStateDiagram\ConfigFilter::__construct(). ( Ignorable by Annotation )

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

40
            new ConfigFilter(/** @scrutinizer ignore-type */ $option->and, $option->or, $option->color),
Loading history...
Bug introduced by
$option->or of type Koriym\AppStateDiagram\list is incompatible with the type array expected by parameter $or of Koriym\AppStateDiagram\ConfigFilter::__construct(). ( Ignorable by Annotation )

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

40
            new ConfigFilter($option->and, /** @scrutinizer ignore-type */ $option->or, $option->color),
Loading history...
41
            $mode
42
        );
43
    }
44
45
    /**
46
     * @param array<string>              $argv
47
     * @param array<string, string|bool> $options
48
     */
49
    public static function fromCommandLine(int $argc, array $argv, array $options): Config
50
    {
51
        $option = new Option($options, null);
52
53
        return new Config(
54
            (string) realpath($argv[$argc - 1]),
55
            $option->watch,
56
            new ConfigFilter($option->and, $option->or, $option->color),
0 ignored issues
show
Bug introduced by
$option->and of type Koriym\AppStateDiagram\list is incompatible with the type array expected by parameter $and of Koriym\AppStateDiagram\ConfigFilter::__construct(). ( Ignorable by Annotation )

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

56
            new ConfigFilter(/** @scrutinizer ignore-type */ $option->and, $option->or, $option->color),
Loading history...
Bug introduced by
$option->or of type Koriym\AppStateDiagram\list is incompatible with the type array expected by parameter $or of Koriym\AppStateDiagram\ConfigFilter::__construct(). ( Ignorable by Annotation )

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

56
            new ConfigFilter($option->and, /** @scrutinizer ignore-type */ $option->or, $option->color),
Loading history...
57
            $option->mode
58
        );
59
    }
60
}
61