Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
20 / 23
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProjectComposerPackage
86.96% covered (warning)
86.96%
20 / 23
60.00% covered (warning)
60.00%
3 / 5
8.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
4.18
 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        $this->vendorDirectory = is_string($this->composer->getConfig()->get('vendor-dir'))
36            ? ltrim(str_replace(dirname($absolutePathFile), '', $this->composer->getConfig()->get('vendor-dir')), '\\/')
37            :  'vendor';
38    }
39
40    /**
41     * @return StraussConfig
42     * @throws \Exception
43     */
44    public function getStraussConfig(): StraussConfig
45    {
46        $config = new StraussConfig($this->composer);
47        $config->setVendorDirectory($this->getVendorDirectory());
48        return $config;
49    }
50
51
52    public function getAuthor(): string
53    {
54        return $this->author;
55    }
56
57    /**
58     * Relative vendor directory with trailing slash.
59     */
60    public function getVendorDirectory(): string
61    {
62        return rtrim($this->vendorDirectory, '\\/') . '/';
63    }
64
65    /**
66     * Get all values in the autoload key as a flattened array.
67     *
68     * @return string[]
69     */
70    public function getFlatAutoloadKey(): array
71    {
72        $autoload = $this->getAutoload();
73        $values = [];
74        array_walk_recursive(
75            $autoload,
76            function ($value, $key) use (&$values) {
77                $values[] = $value;
78            }
79        );
80        return $values;
81    }
82}