Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProjectComposerPackage
87.50% covered (warning)
87.50%
21 / 24
60.00% covered (warning)
60.00%
3 / 5
9.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
5.20
 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 Composer\Factory;
10use Composer\IO\NullIO;
11
12class ProjectComposerPackage extends ComposerPackage
13{
14    protected string $author;
15
16    protected string $vendorDirectory;
17
18    /**
19     * @param string $absolutePathFile
20     * @param ?array{files?:array<string>,classmap?:array<string>,"psr-4"?:array<string,string|array<string>>} $overrideAutoload
21     */
22    public function __construct(string $absolutePathFile, ?array $overrideAutoload = null)
23    {
24        $composer = Factory::create(new NullIO(), $absolutePathFile, true);
25
26        parent::__construct($composer, $overrideAutoload);
27
28        $authors = $this->composer->getPackage()->getAuthors();
29        if (empty($authors) || !isset($authors[0]['name'])) {
30            $this->author = explode("/", $this->packageName, 2)[0];
31        } else {
32            $this->author = $authors[0]['name'];
33        }
34
35        /** @var string $projectVendorAbsoluteDirectoryPath */
36        $projectVendorAbsoluteDirectoryPath = $this->composer->getConfig()->get('vendor-dir');
37        $this->vendorDirectory = is_string($projectVendorAbsoluteDirectoryPath) && !empty($projectVendorAbsoluteDirectoryPath)
38            ? ltrim(str_replace(dirname($absolutePathFile), '', $projectVendorAbsoluteDirectoryPath), '\\/')
39            :  'vendor';
40    }
41
42    /**
43     * @return StraussConfig
44     * @throws \Exception
45     */
46    public function getStraussConfig(): StraussConfig
47    {
48        $config = new StraussConfig($this->composer);
49        $config->setVendorDirectory($this->getVendorDirectory());
50        return $config;
51    }
52
53
54    public function getAuthor(): string
55    {
56        return $this->author;
57    }
58
59    /**
60     * Relative vendor directory with trailing slash.
61     */
62    public function getVendorDirectory(): string
63    {
64        return rtrim($this->vendorDirectory, '\\/') . '/';
65    }
66
67    /**
68     * Get all values in the autoload key as a flattened array.
69     *
70     * @return string[]
71     */
72    public function getFlatAutoloadKey(): array
73    {
74        $autoload = $this->getAutoload();
75        $values = [];
76        array_walk_recursive(
77            $autoload,
78            function ($value, $key) use (&$values) {
79                $values[] = $value;
80            }
81        );
82        return $values;
83    }
84}