Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Path | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| inside | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Util; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | |
| 9 | class Path |
| 10 | { |
| 11 | public static function inside(string $parent, string $child, bool $checkIsFile = false): string |
| 12 | { |
| 13 | $parent = realpath($parent); |
| 14 | |
| 15 | if (!$parent) { |
| 16 | throw new RuntimeException('Parent directory does not exist.'); |
| 17 | } |
| 18 | |
| 19 | $path = realpath(rtrim($parent, '\\/') . DIRECTORY_SEPARATOR . ltrim($child, '\\/')); |
| 20 | |
| 21 | if (!$path || strncmp($path, $parent, strlen($parent)) !== 0) { |
| 22 | throw new RuntimeException( |
| 23 | 'File or directory does not exist or is not in the expected location.', |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | if ($checkIsFile && !is_file($path)) { |
| 28 | throw new RuntimeException('Path is not a file: ' . $path); |
| 29 | } |
| 30 | |
| 31 | return $path; |
| 32 | } |
| 33 | } |