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

h4kuna / dir / 7028743349

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

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 Nette;
8
use SplFileInfo;
9
use Stringable;
10
use Throwable;
11

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

21

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

27

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

33

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

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

51

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

60

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

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

73

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

81
                return $this;
1✔
82
        }
83

84

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

94
                return $this;
1✔
95
        }
96

97

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

107
                return $this;
1✔
108
        }
109

110

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

116

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

129
                return $path;
1✔
130
        }
131

132

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

138

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

150
                return $path;
1✔
151
        }
152

153
}
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