AbstractAppMeta   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 8
eloc 28
c 10
b 0
f 1
dl 0
loc 80
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourceListGenerator() 0 14 3
A camel2kebab() 0 5 1
A getGenerator() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\AppMeta;
6
7
use BEAR\Resource\ResourceObject;
8
use Generator;
9
use Koriym\Psr4List\Psr4List;
10
11
use function array_slice;
12
use function array_walk;
13
use function assert;
14
use function explode;
15
use function implode;
16
use function is_a;
17
use function ltrim;
18
use function preg_replace;
19
use function sprintf;
20
use function strtolower;
21
22
use const DIRECTORY_SEPARATOR;
23
24
/**
25
 * @psalm-import-type AppName from Types
26
 * @psalm-import-type AppDir from Types
27
 * @psalm-import-type TmpDir from Types
28
 * @psalm-import-type LogDir from Types
29
 * @psalm-import-type UriPath from Types
30
 * @psalm-import-type FilePath from Types
31
 * @psalm-import-type Scheme from Types
32
 */
33
abstract class AbstractAppMeta
34
{
35
    /**
36
     * Application name "{Vendor}\{Project}"
37
     *
38
     * @var AppName
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\AppName 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...
39
     */
40
    public string $name;
41
42
    /** @var AppDir */
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\AppDir 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...
43
    public string $appDir;
44
45
    /** @var TmpDir */
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\TmpDir 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...
46
    public string $tmpDir;
47
48
    /** @var LogDir */
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\LogDir 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...
49
    public string $logDir;
50
51
    /** @return Generator<array{0: class-string<ResourceObject>, 1: FilePath}> */
52
    public function getResourceListGenerator(): Generator
53
    {
54
        $list = new Psr4List();
55
        $resourceNamespace = $this->name . '\Resource';
56
        $resourceDir = $this->appDir . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Resource';
57
58
        foreach ($list($resourceNamespace, $resourceDir) as [$class, $file]) {
59
            if (! is_a($class, ResourceObject::class, true)) {
60
                continue;
61
            }
62
63
            assert($file !== '');
64
65
            yield [$class, $file];
66
        }
67
    }
68
69
    /**
70
     * @param Scheme $scheme 'app' | 'page' | '*'
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\Scheme 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...
71
     *
72
     * @return Generator<ResMeta>
73
     */
74
    public function getGenerator(string $scheme = '*'): Generator
75
    {
76
        foreach ($this->getResourceListGenerator() as [$class, $file]) {
77
            /** @var array<non-empty-string> $paths */
78
            $paths = explode('\\', $class);
79
            $path = array_slice($paths, 3);
80
            array_walk($path, [$this, 'camel2kebab']);
81
            if ($scheme === '*') {
82
                /** @var non-empty-string $schemeValue */
83
                $schemeValue = $path[0];
84
                /** @var array<non-empty-string> $slice */
85
                $slice = array_slice($path, 1);
86
                $uri = sprintf('%s://self/%s', $schemeValue, implode('/', $slice));
87
88
                yield new ResMeta($uri, $class, $file);
89
            }
90
91
            if ($scheme === $path[0]) {
92
                /** @var array<non-empty-string> $sliceSchema */
93
                $sliceSchema = array_slice($path, 1);
94
                $uri = sprintf('/%s', implode('/', $sliceSchema));
95
96
                yield new ResMeta($uri, $class, $file);
97
            }
98
        }
99
    }
100
101
    /**
102
     * False positive: used in array_walk
103
     *
104
     * @param non-empty-string $str
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
105
     *
106
     * @param-out non-empty-string $str
107
     */
108
    private function camel2kebab(string &$str): void  // phpcs:ignore
109
    {
110
        $result = ltrim(strtolower((string) preg_replace('/[A-Z]/', '-\0', $str)), '-');
111
        assert($result !== '');
112
        $str = $result;
113
    }
114
}
115