Directory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 58
ccs 22
cts 22
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDirectory() 0 21 3
B removeDir() 0 23 7
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Helpers;
15
16
use Exception;
17
18
/**
19
 * Class Directory.
20
 *
21
 * @package Wszetko\Sitemap\Helpers
22
 */
23
class Directory
24
{
25
    /**
26
     * @param string $directory
27
     *
28
     * @return string
29
     *
30
     * @throws \Exception
31
     */
32 18
    public static function checkDirectory(string $directory): string
33
    {
34 18
        $dir = realpath($directory);
35
36 18
        if (false === $dir) {
37 16
            @mkdir(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

37
            /** @scrutinizer ignore-unhandled */ @mkdir(

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
38 16
                $directory,
39 16
                0777,
40 16
                true
41
            );
42
43 16
            $dir = realpath($directory);
44
        }
45
46 18
        if (false === $dir) {
47
            // @codeCoverageIgnoreStart
48
            throw new Exception("Can't get directory $directory.");
49
            // @codeCoverageIgnoreEnd
50
        }
51
52 18
        return $dir;
53
    }
54
55
    /**
56
     * @param string $dir
57
     */
58 6
    public static function removeDir(string $dir): void
59
    {
60 6
        if (!is_dir($dir)) {
61 2
            return;
62
        }
63
64 4
        $objects = scandir($dir);
65
66 4
        if (false !== $objects) {
67 4
            foreach ($objects as $object) {
68 4
                if ('.' === $object || '..' === $object) {
69 4
                    continue;
70
                }
71
72 4
                if (is_dir($dir . '/' . $object)) {
73 2
                    self::removeDir($dir . '/' . $object);
74
                } else {
75 2
                    unlink($dir . '/' . $object);
76
                }
77
            }
78
        }
79
80 4
        rmdir($dir);
81 4
    }
82
}
83