• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / dir / 7028745808

29 Nov 2023 06:13AM UTC coverage: 95.0% (-0.5%) from 95.455%
7028745808

push

github

h4kuna
Dir: Filesystem interface

33 of 35 new or added lines in 4 files covered. (94.29%)

1 existing line in 1 file now uncovered.

57 of 60 relevant lines covered (95.0%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/Dir.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Dir;
4

5
use h4kuna\Dir\Storage\Filesystem;
6
use h4kuna\Dir\Storage\Local;
7
use SplFileInfo;
8
use Stringable;
9
use Throwable;
10

11
/**
12
 * You don't fill last slash in path
13
 *
14
 * @phpstan-consistent-constructor
15
 */
16
class Dir implements Stringable
17
{
18
        private Filesystem $filesystem;
19

20

21
        public function __construct(private string $baseAbsolutePath, ?Filesystem $filesystem = null)
1✔
22
        {
23
                $this->filesystem = $filesystem ?? new Local();
1✔
24
        }
1✔
25

26

27
        public function getDir(): string
28
        {
29
                return $this->baseAbsolutePath;
1✔
30
        }
31

32

33
        /**
34
         * Make absolute path with filename
35
         * @param string $name doesn't start with slash
36
         * @throws Exceptions\IOException
37
         */
38
        public function filename(string $name, string $extension = ''): string
1✔
39
        {
40
                $path = dirname($name);
1✔
41
                if ($path !== '.') {
1✔
42
                        return $this->dir($path)->filename(basename($name), $extension);
1✔
43
                } elseif ($extension !== '') {
1✔
44
                        $name .= ".$extension";
1✔
45
                }
46

47
                return self::slash($this->baseAbsolutePath, $name);
1✔
48
        }
49

50

51
        /**
52
         * @throws Exceptions\IOException
53
         */
54
        public function fileInfo(string $name, string $extension = ''): SplFileInfo
1✔
55
        {
56
                return new SplFileInfo($this->filename($name, $extension));
1✔
57
        }
58

59

60
        /**
61
         * Add relative path from $baseAbsolutePath
62
         * @throws Exceptions\IOException
63
         * @example both is possible 'foo' or 'foo/bar'
64
         */
65
        public function dir(string $path): static
1✔
66
        {
67
                $newDir = self::slash($this->baseAbsolutePath, $path);
1✔
68

69
                return new static(self::createDir($newDir, $this->filesystem), $this->filesystem);
1✔
70
        }
71

72

73
        /**
74
         * @throws Exceptions\IOException
75
         */
76
        public function create(): static
77
        {
78
                self::createDir($this->baseAbsolutePath, $this->filesystem);
1✔
79

80
                return $this;
1✔
81
        }
82

83

84
        /**
85
         * @throws Exceptions\DirIsNotWriteableException
86
         */
87
        public function checkWriteable(): static
88
        {
89
                if ($this->filesystem->isWriteable($this->baseAbsolutePath) === false) {
1✔
90
                        throw new Exceptions\DirIsNotWriteableException($this->baseAbsolutePath);
1✔
91
                }
92

93
                return $this;
1✔
94
        }
95

96

97
        /**
98
         * @throws Exceptions\DirIsNotReadableException
99
         */
100
        public function checkReadable(): static
101
        {
102
                if ($this->filesystem->isReadable($this->baseAbsolutePath) === false) {
1✔
103
                        throw new Exceptions\DirIsNotReadableException($this->baseAbsolutePath);
1✔
104
                }
105

106
                return $this;
1✔
107
        }
108

109

110
        public function __toString(): string
111
        {
112
                return $this->getDir();
1✔
113
        }
114

115

116
        /**
117
         * @throws Exceptions\IOException
118
         */
119
        final protected static function makeHomeDir(string $path, Filesystem $filesystem, string $root = ''): string
1✔
120
        {
121
                if ($filesystem->isAbsolute($path) === false) {
1✔
122
                        if ($root === '') {
1✔
123
                                $root = self::slash(sys_get_temp_dir(), 'h4kuna');
1✔
124
                        }
125
                        $path = self::createDir(self::slash($root, $path), $filesystem); // intentionally here in condition branch
1✔
126
                }
127

128
                return $path;
1✔
129
        }
130

131

132
        final protected static function slash(string $dir1, string $dir2): string
1✔
133
        {
134
                return "$dir1/$dir2";
1✔
135
        }
136

137

138
        /**
139
         * @throws Exceptions\IOException
140
         */
141
        private static function createDir(string $path, Filesystem $filesystem): string
1✔
142
        {
143
                try {
144
                        $filesystem->createDir($path);
1✔
NEW
145
                } catch (Throwable $e) {
×
UNCOV
146
                        throw new Exceptions\IOException($path, 0, $e);
×
147
                }
148

149
                return $path;
1✔
150
        }
151

152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc