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
 getVendorDirectory
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 $vendorDirectory;
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->vendorDirectory = 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->setVendorDirectory($this->getVendorDirectory());
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    public function getVendorDirectory(): string
68    {
69        return rtrim($this->vendorDirectory, '\\/') . '/';
70    }
71
72    /**
73     * Get all values in the autoload key as a flattened array.
74     *
75     * @return string[]
76     */
77    public function getFlatAutoloadKey(): array
78    {
79        $autoload = $this->getAutoload();
80        $values = [];
81        array_walk_recursive(
82            $autoload,
83            function ($value, $key) use (&$values) {
84                $values[] = $value;
85            }
86        );
87        return $values;
88    }
89}