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

Option::parseLabel()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 4
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use SimpleXMLElement;
8
9
use function explode;
10
use function is_string;
11
use function property_exists;
12
13
/** @psalm-immutable */
14
final class Option
15
{
16
    /** @var bool */
17
    public $watch;
18
19
    /** @var list<string> */
0 ignored issues
show
Bug introduced by
The type Koriym\AppStateDiagram\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...
20
    public $and;
21
22
    /** @var list<string> */
23
    public $or;
24
25
    /** @var string */
26
    public $color;
27
28
    /** @var string */
29
    public $mode;
30
31
    /** @param array<string, string|bool> $options */
32
    public function __construct(array $options, ?SimpleXMLElement $filter)
33
    {
34
        $this->watch = isset($options['w']) || isset($options['watch']);
35
        $this->and = $this->parseAndTag($options, $filter);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseAndTag($options, $filter) of type array or array or string[] is incompatible with the declared type Koriym\AppStateDiagram\list of property $and.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->or = $this->parseOrTag($options, $filter);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseOrTag($options, $filter) of type array or array or string[] is incompatible with the declared type Koriym\AppStateDiagram\list of property $or.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->color = $this->parseColor($options, $filter);
38
        $this->mode = $this->getMode($options);
39
    }
40
41
    /**
42
     * @param array<string, string|bool> $options
43
     *
44
     * @return list<string>
45
     */
46
    private function parseAndTag(array $options, ?SimpleXMLElement $filter): array
47
    {
48
        if (isset($options['and-tag']) && is_string($options['and-tag'])) {
49
            return explode(',', $options['and-tag']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode(',', $options['and-tag']) returns the type string[] which is incompatible with the documented return type Koriym\AppStateDiagram\list.
Loading history...
50
        }
51
52
        /** @var array<string> */ // phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat
53
54
        return $filter instanceof SimpleXMLElement && property_exists($filter, 'and') ? (array) $filter->and : [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $filter instanceo...)$filter->and : array() returns the type array which is incompatible with the documented return type Koriym\AppStateDiagram\list.
Loading history...
55
    }
56
57
    /**
58
     * @param array<string, string|bool> $options
59
     *
60
     * @return array<string>
61
     */
62
    private function parseOrTag(array $options, ?SimpleXMLElement $filter): array
63
    {
64
        if (isset($options['or-tag']) && is_string($options['or-tag'])) {
65
            return explode(',', $options['or-tag']);
66
        }
67
68
        /** @var array<string> */ // phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat
69
70
        return $filter instanceof SimpleXMLElement && property_exists($filter, 'or') ? (array) $filter->or : [];
71
    }
72
73
    /** @param array<string, string|bool> $options */
74
    private function parseColor(array $options, ?SimpleXMLElement $filter): string
75
    {
76
        if (isset($options['color']) && is_string($options['color'])) {
77
            return $options['color'];
78
        }
79
80
        return $filter instanceof SimpleXMLElement && property_exists($filter, 'color') ? (string) $filter->color : '';
81
    }
82
83
    /** @param array<string, string|bool> $options */
84
    private function getMode(array $options): string
85
    {
86
        $isMarkdown = isset($options['mode']) && $options['mode'] === DumpDocs::MODE_MARKDOWN;
87
88
        return $isMarkdown ? DumpDocs::MODE_MARKDOWN : DumpDocs::MODE_HTML;
89
    }
90
}
91