Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
24 / 27
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProjectComposerPackage
88.89% covered (warning)
88.89%
24 / 27
60.00% covered (warning)
60.00%
3 / 5
9.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
5.09
 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;
11use RecursiveArrayIterator;
12use RecursiveIteratorIterator;
13use Symfony\Component\Console\Input\InputInterface;
14
15class ProjectComposerPackage extends ComposerPackage
16{
17    protected string $author;
18
19    protected string $vendorDirectory;
20
21    /**
22     * @param string $absolutePath
23     * @param ?array{files?:array<string>,classmap?:array<string>,"psr-4"?:array<string,string|array<string>>} $overrideAutoload
24     */
25    public function __construct(string $absolutePath, ?array $overrideAutoload = null)
26    {
27        $absolutePathFile = is_dir($absolutePath)
28            ? $absolutePath . DIRECTORY_SEPARATOR . 'composer.json'
29            : $absolutePath;
30        unset($absolutePath);
31
32        $composer = Factory::create(new NullIO(), $absolutePathFile, true);
33
34        parent::__construct($composer, $overrideAutoload);
35
36        $authors = $this->composer->getPackage()->getAuthors();
37        if (empty($authors) || !isset($authors[0]['name'])) {
38            $this->author = explode("/", $this->packageName, 2)[0];
39        } else {
40            $this->author = $authors[0]['name'];
41        }
42
43        // In rare cases, the vendor directory will not be a single level of directory. File an issue.
44        $this->vendorDirectory = is_string($this->composer->getConfig()->get('vendor-dir'))
45            ? basename($this->composer->getConfig()->get('vendor-dir'))
46            :  'vendor';
47    }
48
49    /**
50     * @return StraussConfig
51     * @throws \Exception
52     */
53    public function getStraussConfig(): StraussConfig
54    {
55        $config = new StraussConfig($this->composer);
56        $config->setVendorDirectory($this->getVendorDirectory());
57        return $config;
58    }
59
60
61    public function getAuthor(): string
62    {
63        return $this->author;
64    }
65
66    /**
67     * Relative vendor directory with trailing slash.
68     */
69    public function getVendorDirectory(): string
70    {
71        return rtrim($this->vendorDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
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}