Passed
Pull Request — master (#15)
by Takashi
02:58
created

AccessController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
class AccessController
8
{
9
    public function __construct(
10
        private array $publicCategories,
11
        private array $publicTags,
12
        private array $privateCategories,
13
        private array $privateTags,
14
    ) {
15
    }
16
17
    public function isPublic(?string $category, array $tags): bool
18
    {
19
        if ($this->matchesPrivateConditions($category, $tags)) {
20
            return false;
21
        }
22
23
        return empty($this->publicCategories) || $this->matchesPublicConditions($category, $tags);
0 ignored issues
show
Bug introduced by
It seems like $category can also be of type null; however, parameter $category of App\Service\AccessContro...tchesPublicConditions() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

23
        return empty($this->publicCategories) || $this->matchesPublicConditions(/** @scrutinizer ignore-type */ $category, $tags);
Loading history...
24
    }
25
26
    private function matchesPublicConditions(string $category, array $tags): bool
27
    {
28
        return $this->categoryIsUnderOneOf($category, $this->publicCategories) || $this->atLeastOneTagIsIn($tags, $this->publicTags);
29
    }
30
31
    private function matchesPrivateConditions(?string $category, array $tags): bool
32
    {
33
        return $this->categoryIsUnderOneOf($category, $this->privateCategories) || $this->atLeastOneTagIsIn($tags, $this->privateTags);
34
    }
35
36
    private function categoryIsUnderOneOf(?string $needle, array $haystacks): bool
37
    {
38
        foreach ($haystacks as $haystack) {
39
            if (preg_match(sprintf('#^%s#', $haystack), $needle)) {
0 ignored issues
show
Bug introduced by
It seems like $needle can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

39
            if (preg_match(sprintf('#^%s#', $haystack), /** @scrutinizer ignore-type */ $needle)) {
Loading history...
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
47
    private function atLeastOneTagIsIn(array $needles, array $haystacks): bool
48
    {
49
        foreach ($haystacks as $haystack) {
50
            foreach ($needles as $needle) {
51
                if ($needle === $haystack) {
52
                    return true;
53
                }
54
            }
55
        }
56
57
        return false;
58
    }
59
}
60