Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.62% covered (success)
90.62%
29 / 32
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProjectComposerPackage
90.62% covered (success)
90.62%
29 / 32
60.00% covered (warning)
60.00%
3 / 5
9.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
 getStraussConfig
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAuthor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRelativeVendorDirectory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFlatAutoloadKey
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Extends ComposerPackage to return the typed Strauss config.
4 */
5
6namespace BrianHenryIE\Strauss\Composer;
7
8use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
9use BrianHenryIE\Strauss\Helpers\Flysystem\FileSystem;
10use Composer\Factory;
11use Composer\IO\NullIO;
12
13class ProjectComposerPackage extends ComposerPackage
14{
15    protected string $author;
16
17    protected string $relativeVendorDirectory;
18
19    /**
20     * @param string $absolutePathFile
21     * @param ?array{files?:array<string>,classmap?:array<string>,"psr-4"?:array<string,string|array<string>>} $overrideAutoload
22     */
23    public function __construct(string $absolutePathFile, ?array $overrideAutoload = null)
24    {
25        $composer = (new Factory())->createComposer(
26            new NullIO(),
27            $absolutePathFile,
28            true
29        );
30
31        parent::__construct($composer, $overrideAutoload);
32
33        $authors = $this->composer->getPackage()->getAuthors();
34        if (empty($authors) || !isset($authors[0]['name'])) {
35            $this->author = explode("/", $this->packageName, 2)[0];
36        } else {
37            $this->author = $authors[0]['name'];
38        }
39
40        /** @var ?string $projectVendorAbsoluteDirectoryPath */
41        $projectVendorAbsoluteDirectoryPath = $this->composer->getConfig()->get('vendor-dir');
42        $this->relativeVendorDirectory = is_string($projectVendorAbsoluteDirectoryPath) && !empty($projectVendorAbsoluteDirectoryPath)
43            ? ltrim(str_replace(
44                FileSystem::normalizeDirSeparator(dirname($absolutePathFile)),
45                '',
46                FileSystem::normalizeDirSeparator($projectVendorAbsoluteDirectoryPath)
47            ), '\\/')
48            :  'vendor';
49    }
50
51    /**
52     * @return StraussConfig
53     * @throws \Exception
54     */
55    public function getStraussConfig(): StraussConfig
56    {
57        $config = new StraussConfig($this->composer);
58        $config->setRelativeVendorDirectory($this->getRelativeVendorDirectory());
59        return $config;
60    }
61
62
63    public function getAuthor(): string
64    {
65        return $this->author;
66    }
67
68    /**
69     * Relative vendor directory with trailing slash.
70     *
71     * No leading or trailing slash
72     */
73    public function getRelativeVendorDirectory(): string
74    {
75        return trim($this->relativeVendorDirectory, '\\/');
76    }
77
78    /**
79     * Get all values in the autoload key as a flattened array.
80     *
81     * @return string[]
82     */
83    public function getFlatAutoloadKey(): array
84    {
85        $autoload = $this->getAutoload();
86        $values = [];
87        array_walk_recursive(
88            $autoload,
89            function ($value, $key) use (&$values) {
90                $values[] = $value;
91            }
92        );
93        return $values;
94    }
95}