Passed
Pull Request — master (#103)
by Sebastian
02:29
created

Check::isDirectoryEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\File\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception\ActionFailed;
17
use CaptainHook\App\Hook\Action;
18
use Exception;
19
use SebastianFeldmann\Git\Repository;
20
21
/**
22
 * Class Check
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 5.4.1
28
 */
29
abstract class Check implements Action
30
{
31
    /**
32
     * Actual action name
33
     *
34
     * @var string
35
     */
36
    protected $actionName;
37
38
    /**
39
     * Executes the action
40
     *
41
     * @param  \CaptainHook\App\Config           $config
42
     * @param  \CaptainHook\App\Console\IO       $io
43
     * @param  \SebastianFeldmann\Git\Repository $repository
44
     * @param  \CaptainHook\App\Config\Action    $action
45
     * @return void
46
     * @throws \Exception
47
     */
48
    abstract public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void;
49
50
    /**
51
     * Extract files list from the action configuration
52
     *
53
     * @param  \CaptainHook\App\Config\Options $options
54
     * @return array
55
     * @throws \Exception
56
     */
57
    protected function getFiles(Config\Options $options): array
58
    {
59
        $files = $options->get('files');
60
        if (!is_array($files)) {
0 ignored issues
show
introduced by
The condition is_array($files) is always false.
Loading history...
61
            throw new Exception('Missing option "files" for ' . $this->actionName . ' action');
62
        }
63
64
        $globs = [];
65
        foreach ($files as $glob) {
66
            $globs[$glob] = glob($glob);
67
        }
68
        return $globs;
69
    }
70
71
    /**
72
     * Check if all files are empty
73
     *
74
     * @param  string[] $files
75
     * @return bool
76
     * @throws \CaptainHook\App\Exception\ActionFailed
77
     */
78
    protected function areAllFilesEmpty(array $files): bool
79
    {
80
        foreach ($files as $file) {
81
            if (!$this->isEmpty($file)) {
82
                return false;
83
            }
84
        }
85
        return true;
86
    }
87
88
    /**
89
     * Check if any file is empty
90
     *
91
     * @param  array $files
92
     * @return bool
93
     * @throws \CaptainHook\App\Exception\ActionFailed
94
     */
95
    protected function isAnyFileEmpty(array $files): bool
96
    {
97
        foreach ($files as $file) {
98
            if ($this->isEmpty($file)) {
99
                return true;
100
            }
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * Returns true when the file has no contents or the directory is empty
107
     *
108
     * @param  string $file
109
     * @return bool
110
     * @throws \CaptainHook\App\Exception\ActionFailed
111
     */
112
    protected function isEmpty(string $file): bool
113
    {
114
        if (is_dir($file)) {
115
            return $this->isDirectoryEmpty($file);
116
        }
117
118
        return filesize($file) === 0;
119
    }
120
121
    /**
122
     * Checks if a directory is empty
123
     *
124
     * @param  string $directory
125
     * @return bool
126
     * @throws \CaptainHook\App\Exception\ActionFailed
127
     */
128
    protected function isDirectoryEmpty(string $directory)
129
    {
130
        // ignore . and .. directories
131
        return empty(array_diff(scandir($directory), ['..', '.']));
0 ignored issues
show
Bug introduced by
It seems like scandir($directory) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

131
        return empty(array_diff(/** @scrutinizer ignore-type */ scandir($directory), ['..', '.']));
Loading history...
132
    }
133
}
134