Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
18.75% |
3 / 16 |
|
16.67% |
2 / 12 |
CRAP | |
0.00% |
0 / 1 |
| DiscoveredFiles | |
18.75% |
3 / 16 |
|
16.67% |
2 / 12 |
119.13 | |
0.00% |
0 / 1 |
| __construct | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| add | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sort | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetGet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetSet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetUnset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPsr0 | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BrianHenryIE\Strauss\Files; |
| 4 | |
| 5 | use ArrayAccess; |
| 6 | use ArrayIterator; |
| 7 | use BadMethodCallException; |
| 8 | use Countable; |
| 9 | use IteratorAggregate; |
| 10 | use Traversable; |
| 11 | |
| 12 | /** |
| 13 | * @implements IteratorAggregate<string, FileBase> |
| 14 | * @implements ArrayAccess<string, FileBase> |
| 15 | */ |
| 16 | class DiscoveredFiles implements IteratorAggregate, ArrayAccess, Countable |
| 17 | { |
| 18 | /** @var array<string,FileBase|File|FileWithDependency> */ |
| 19 | protected array $files = []; |
| 20 | |
| 21 | /** |
| 22 | * @param FileBase[] $files |
| 23 | */ |
| 24 | public function __construct(array $files = []) |
| 25 | { |
| 26 | foreach ($files as $file) { |
| 27 | $this->files[ $file->getSourcePath()] = $file; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public function add(FileBase $file): void |
| 32 | { |
| 33 | $this->files[$file->getSourcePath()] = $file; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return array<string,FileBase|File|FileWithDependency> |
| 38 | */ |
| 39 | public function getFiles(): array |
| 40 | { |
| 41 | return $this->files; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Fetch/check if a file exists in the discovered files. |
| 46 | * |
| 47 | * @param string $sourceAbsolutePath Full path to the file. |
| 48 | */ |
| 49 | public function getFile(string $sourceAbsolutePath): ?FileBase |
| 50 | { |
| 51 | return $this->files[$sourceAbsolutePath] ?? null; |
| 52 | } |
| 53 | |
| 54 | public function sort(): void |
| 55 | { |
| 56 | ksort($this->files); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return Traversable<FileBase> |
| 61 | */ |
| 62 | #[\ReturnTypeWillChange] |
| 63 | public function getIterator() |
| 64 | { |
| 65 | return new ArrayIterator($this->files); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param string $offset Absolute path. |
| 70 | * |
| 71 | * @return bool |
| 72 | */ |
| 73 | #[\ReturnTypeWillChange] |
| 74 | public function offsetExists($offset) |
| 75 | { |
| 76 | return array_key_exists($offset, $this->files); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $offset Absolute path. |
| 81 | * |
| 82 | * @return ?FileBase |
| 83 | */ |
| 84 | #[\ReturnTypeWillChange] |
| 85 | public function offsetGet($offset) |
| 86 | { |
| 87 | return $this->files[$offset] ?? null; |
| 88 | } |
| 89 | |
| 90 | #[\ReturnTypeWillChange] |
| 91 | public function offsetSet($offset, $value) |
| 92 | { |
| 93 | throw new BadMethodCallException(); |
| 94 | } |
| 95 | |
| 96 | #[\ReturnTypeWillChange] |
| 97 | public function offsetUnset($offset) |
| 98 | { |
| 99 | throw new BadMethodCallException(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return int |
| 104 | */ |
| 105 | #[\ReturnTypeWillChange] |
| 106 | public function count() |
| 107 | { |
| 108 | return count($this->files); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return FileWithDependency[] |
| 113 | */ |
| 114 | public function getPsr0(): array |
| 115 | { |
| 116 | return array_filter( |
| 117 | $this->files, |
| 118 | fn(FileBase $file) => $file instanceof FileWithDependency && $file->isPsr0() |
| 119 | ); |
| 120 | } |
| 121 | } |