Completed
Push — master ( 02da02...be0985 )
by De Cramer
17s queued 12s
created

AbstractScopeResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 24
c 2
b 0
f 0
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getScope() 0 7 2
A validateScope() 0 5 1
A inherits() 0 11 2
A getScopes() 0 7 2
A getScopeTree() 0 13 4
1
<?php
2
3
4
namespace oliverde8\ComfyBundle\Resolver;
5
6
7
use oliverde8\AssociativeArraySimplified\AssociativeArray;
0 ignored issues
show
Bug introduced by
The type oliverde8\AssociativeArr...lified\AssociativeArray 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...
8
9
abstract class AbstractScopeResolver implements ScopeResolverInterface
10
{
11
    /** @var array */
12
    private ?array $scopes = null;
13
14
    /**
15
     * @inheritDoc
16
     */
17
    public function validateScope(string $scope = null): bool
18
    {
19
        $scopes = $this->getScopes();
20
        $scope = $this->getScope($scope);
21
        return isset($scopes[$scope]);
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function getScope(string $scope = null): string
28
    {
29
        if (is_null($scope)) {
30
            $scope = $this->getCurrentScope();
31
        }
32
33
        return $scope;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function inherits(string $scope = null): ?string
40
    {
41
        $scope = $this->getScope($scope);
42
43
        $parts = explode('/', $scope);
44
        if (count($parts) <= 1) {
45
            return null;
46
        }
47
48
        array_pop($parts);
49
        return implode('/', $parts);
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getScopeTree() : array
56
    {
57
        $data = [];
58
        foreach ($this->scopes as $scopeKey => $scopeName) {
59
            $parentScopeKey = $this->inherits($scopeKey);
60
            if ($parentScopeKey && !AssociativeArray::checkKeyExist($data, $parentScopeKey . "/~name")) {
61
                AssociativeArray::setFromKey($data, $parentScopeKey . "/~name", $parentScopeKey);
62
            }
63
64
            AssociativeArray::setFromKey($data, $scopeKey . "/~name", $scopeName);
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    protected function getScopes(): array
74
    {
75
        if (is_null($this->scopes)) {
76
            $this->scopes = $this->initScopes();
77
        }
78
79
        return $this->scopes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->scopes could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82
    abstract protected function initScopes(): array;
83
}