Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProjectComposerPackage
89.29% covered (warning)
89.29%
25 / 28
60.00% covered (warning)
60.00%
3 / 5
9.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
85.71% covered (warning)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
5.07
 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\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 = Factory::create(new NullIO(), $absolutePathFile, true);
26
27        parent::__construct($composer, $overrideAutoload);
28
29        $authors = $this->composer->getPackage()->getAuthors();
30        if (empty($authors) || !isset($authors[0]['name'])) {
31            $this->author = explode("/", $this->packageName, 2)[0];
32        } else {
33            $this->author = $authors[0]['name'];
34        }
35
36        /** @var ?string $projectVendorAbsoluteDirectoryPath */
37        $projectVendorAbsoluteDirectoryPath = $this->composer->getConfig()->get('vendor-dir');
38        $this->relativeVendorDirectory = is_string($projectVendorAbsoluteDirectoryPath) && !empty($projectVendorAbsoluteDirectoryPath)
39            ? ltrim(str_replace(
40                FileSystem::normalizeDirSeparator(dirname($absolutePathFile)),
41                '',
42                FileSystem::normalizeDirSeparator($projectVendorAbsoluteDirectoryPath)
43            ), '\\/')
44            :  'vendor';
45    }
46
47    /**
48     * @return StraussConfig
49     * @throws \Exception
50     */
51    public function getStraussConfig(): StraussConfig
52    {
53        $config = new StraussConfig($this->composer);
54        $config->setRelativeVendorDirectory($this->getRelativeVendorDirectory());
55        return $config;
56    }
57
58
59    public function getAuthor(): string
60    {
61        return $this->author;
62    }
63
64    /**
65     * Relative vendor directory with trailing slash.
66     *
67     * No leading or trailing slash
68     */
69    public function getRelativeVendorDirectory(): string
70    {
71        return trim($this->relativeVendorDirectory, '\\/');
72    }
73
74    /**
75     * Get all values in the autoload key as a flattened array.
76     *
77     * @return string[]
78     */
79    public function getFlatAutoloadKey(): array
80    {
81        $autoload = $this->getAutoload();
82        $values = [];
83        array_walk_recursive(
84            $autoload,
85            function ($value, $key) use (&$values) {
86                $values[] = $value;
87            }
88        );
89        return $values;
90    }
91}