Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.43% covered (danger)
46.43%
13 / 28
27.27% covered (danger)
27.27%
3 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileWithDependency
46.43% covered (danger)
46.43%
13 / 28
27.27% covered (danger)
27.27%
3 / 11
44.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 getDependency
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addAutoloader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isFilesAutoloaderFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPackageRelativePath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isDoDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addAutoloaderType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAutoloaderTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAutoloaderTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isPsr0
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isAutoloaded
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace BrianHenryIE\Strauss\Files;
4
5use BrianHenryIE\Strauss\Composer\ComposerPackage;
6use BrianHenryIE\Strauss\Helpers\Flysystem\FileSystem;
7
8class FileWithDependency extends File implements HasDependency
9{
10
11    /**
12     * @var string The path to the file relative to the package root.
13     */
14    protected string $vendorRelativePath;
15
16    protected string $packageRelativePath;
17
18    /**
19     * The project dependency that this file belongs to.
20     */
21    protected ComposerPackage $dependency;
22
23    /**
24     * @var string[] The autoloader types that this file is included in.
25     */
26    protected array $autoloaderTypes = [];
27
28    public function __construct(
29        ComposerPackage $dependency,
30        string $vendorRelativePath,
31        string $sourceAbsolutePath,
32        string $targetAbsolutePath
33    ) {
34        parent::__construct($sourceAbsolutePath, $vendorRelativePath, $targetAbsolutePath);
35
36        $this->dependency = $dependency;
37
38        /** @var string $packageAbsolutePath */
39        $packageAbsolutePath = $dependency->getPackageAbsolutePath();
40
41        $this->vendorRelativePath = $vendorRelativePath;
42
43        $this->packageRelativePath = str_replace(
44            FileSystem::normalizeDirSeparator($packageAbsolutePath),
45            '',
46            FileSystem::normalizeDirSeparator($sourceAbsolutePath)
47        );
48
49        // Set this to null so we query the package's `isDelete` setting.
50        $this->doDelete = null;
51
52        $this->dependency->addFile($this);
53    }
54
55    public function getDependency(): ComposerPackage
56    {
57        return $this->dependency;
58    }
59
60    /**
61     * The target path to (maybe) copy the file to, and the target path to perform replacements in (which may be the
62     * original path).
63     */
64
65    /**
66     * Record the autoloader it is found in. Which could be all of them.
67     */
68    public function addAutoloader(string $autoloaderType): void
69    {
70        $this->autoloaderTypes = array_unique(array_merge($this->autoloaderTypes, array($autoloaderType)));
71    }
72
73    public function isFilesAutoloaderFile(): bool
74    {
75        return in_array('files', $this->autoloaderTypes, true);
76    }
77
78    public function getPackageRelativePath(): string
79    {
80        return trim($this->packageRelativePath, '\\/');
81    }
82
83    public function isDoDelete(): bool
84    {
85        return $this->doDelete ?? $this->dependency->isDoDelete();
86    }
87
88    public function addAutoloaderType(string $autoloaderType): void
89    {
90        $this->autoloaderTypes[$autoloaderType] = $autoloaderType;
91    }
92
93    /**
94     * @param string[] $autoloaderTypes
95     */
96    public function setAutoloaderTypes(array $autoloaderTypes): void
97    {
98        $this->autoloaderTypes = $autoloaderTypes;
99    }
100
101    /**
102     * @return string[] The autoloader types that this file is loaded under.
103     */
104    public function getAutoloaderTypes(): array
105    {
106        return $this->autoloaderTypes;
107    }
108    public function isPsr0(): bool
109    {
110        return in_array('psr-0', $this->autoloaderTypes);
111    }
112
113    public function isAutoloaded(): bool
114    {
115        if ($this->dependency->hasPsr0()) {
116            /**
117             * This is checked by {@see ComposerPackage::hasPsr0()}.
118             * @phpstan-ignore offsetAccess.notFound
119             */
120            foreach ($this->getDependency()->getAutoload()['psr-0'] as $autoloadPackageRelativePath) {
121                if (str_starts_with(
122                    trim($this->packageRelativePath, '\\/'),
123                    trim($autoloadPackageRelativePath, '\\/')
124                )) {
125                    return true;
126                }
127            }
128        }
129
130        return !empty($this->autoloaderTypes);
131    }
132}