CompilePreload   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 4
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A loadResources() 0 8 2
A __invoke() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use ArrayObject;
8
use BEAR\AppMeta\AbstractAppMeta;
9
use BEAR\AppMeta\Meta;
10
use BEAR\Package\Injector;
11
use BEAR\Package\Types;
12
13
use function assert;
14
use function realpath;
15
use function sprintf;
16
17
/**
18
 * @psalm-import-type ClassList from Types
19
 * @psalm-import-type Context from Types
20
 * @psalm-import-type AppName from Types
21
 * @psalm-import-type AppDir from Types
22
 */
23
final class CompilePreload
24
{
25
    /**
26
     * @param ClassList $classes
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Compiler\ClassList 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...
27
     * @param Context   $context
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Compiler\Context 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...
28
     */
29
    public function __construct(
30
        private FakeRun $fakeRun,
31
        private CompileAutoload $dumpAutoload,
32
        private FilePutContents $filePutContents,
33
        private ArrayObject $classes,
34
        private string $context,
35
    ) {
36
        $this->fakeRun = $fakeRun;
37
    }
38
39
    /** @param Context $context */
40
    public function __invoke(AbstractAppMeta $appMeta, string $context): string
41
    {
42
        ($this->fakeRun)();
43
        $this->loadResources($appMeta->name, $context, $appMeta->appDir);
44
        /** @var list<string> $classes */
45
        $classes = (array) $this->classes;
46
        $paths = $this->dumpAutoload->getPaths($classes);
0 ignored issues
show
Bug introduced by
$classes of type BEAR\Package\Compiler\list is incompatible with the type array expected by parameter $classes of BEAR\Package\Compiler\CompileAutoload::getPaths(). ( Ignorable by Annotation )

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

46
        $paths = $this->dumpAutoload->getPaths(/** @scrutinizer ignore-type */ $classes);
Loading history...
47
        $requiredOnceFile = '';
48
        foreach ($paths as $path) {
49
            $requiredOnceFile .= sprintf(
50
                "require %s;\n",
51
                $path,
52
            );
53
        }
54
55
        $preloadFile = sprintf("<?php
56
57
// %s preload
58
require __DIR__ . '/vendor/autoload.php';
59
60
%s", $this->context, $requiredOnceFile);
61
        $appDirRealpath = realpath($appMeta->appDir);
62
        assert($appDirRealpath !== false);
63
        $fileName = $appDirRealpath . '/preload.php';
64
        ($this->filePutContents)($fileName, $preloadFile);
65
66
        return $fileName;
67
    }
68
69
    /**
70
     * @param AppName $appName
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Compiler\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...
71
     * @param Context $context
72
     * @param AppDir  $appDir
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Compiler\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...
73
     */
74
    public function loadResources(string $appName, string $context, string $appDir): void
75
    {
76
        $meta = new Meta($appName, $context, $appDir);
77
        $injector = Injector::getInstance($appName, $context, $appDir);
78
79
        $resMetas = $meta->getGenerator('*');
80
        foreach ($resMetas as $resMeta) {
81
            $injector->getInstance($resMeta->class);
82
        }
83
    }
84
}
85