Issues (224)

src/RequirementChecker/RequirementsDumper.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\RequirementChecker;
16
17
use Fidry\FileSystem\FS;
18
use KevinGH\Box\Composer\Artifact\ComposerJson;
19
use KevinGH\Box\Composer\Artifact\ComposerLock;
20
use KevinGH\Box\Phar\CompressionAlgorithm;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\Finder\SplFileInfo;
23
use Webmozart\Assert\Assert;
24
use function str_replace;
25
use function var_export;
26
27
/**
28
 * @private
29
 */
30
final class RequirementsDumper
31
{
32
    private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP'
33
        <?php
34
35
        return '__CONFIG__';
36
        PHP;
37
38
    private const REQUIREMENT_CHECKER_PATH = __DIR__.'/../../res/requirement-checker';
39
40
    /**
41
     * @return list<array{string, string}>
0 ignored issues
show
The type KevinGH\Box\RequirementChecker\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...
42
     */
43
    public static function dump(
44
        ?ComposerJson $composerJson,
45
        ?ComposerLock $composerLock,
46
        CompressionAlgorithm $compressionAlgorithm,
47
    ): array {
48
        Assert::directory(self::REQUIREMENT_CHECKER_PATH, 'Expected the requirement checker to have been dumped');
49
50
        $filesWithContents = [
51
            self::dumpRequirementsConfig(
52
                $composerJson ?? new ComposerJson('', []),
53
                $composerLock ?? new ComposerLock('', []),
54
                $compressionAlgorithm,
55
            ),
56
        ];
57
58
        /** @var SplFileInfo[] $requirementCheckerFiles */
59
        $requirementCheckerFiles = Finder::create()
60
            ->files()
61
            ->in(self::REQUIREMENT_CHECKER_PATH);
62
63
        foreach ($requirementCheckerFiles as $file) {
64
            $filesWithContents[] = [
65
                $file->getRelativePathname(),
66
                FS::getFileContents($file->getPathname()),
67
            ];
68
        }
69
70
        return $filesWithContents;
71
    }
72
73
    private static function dumpRequirementsConfig(
74
        ComposerJson $composerJson,
75
        ComposerLock $composerLock,
76
        CompressionAlgorithm $compressionAlgorithm,
77
    ): array {
78
        $requirements = AppRequirementsFactory::create(
79
            $composerJson,
80
            $composerLock,
81
            $compressionAlgorithm,
82
        );
83
84
        return [
85
            '.requirements.php',
86
            str_replace(
87
                '\'__CONFIG__\'',
88
                var_export($requirements->toArray(), true),
89
                self::REQUIREMENTS_CONFIG_TEMPLATE,
90
            ),
91
        ];
92
    }
93
}
94