Passed
Pull Request — master (#102)
by
unknown
02:39
created

NotEmpty   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 23 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CaptainHook\App\Hook\File\Action;
6
7
use CaptainHook\App\Config;
8
use CaptainHook\App\Console\IO;
9
use CaptainHook\App\Hook\Action;
10
use Exception;
11
use SebastianFeldmann\Git\Repository;
12
13
class NotEmpty implements Action
14
{
15
    /**
16
     * Execute the action.
17
     *
18
     * @param Config        $config
19
     * @param IO            $io
20
     * @param Repository    $repository
21
     * @param Config\Action $action
22
     *
23
     * @return void
24
     *
25
     * @throws Exception
26
     */
27
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
28
    {
29
        $pathToFolder = $action->getOptions()->get('pathSrc');
30
        $searchedFolder = $action->getOptions()->get('folderName');
31
        $path     = glob((getcwd() . $pathToFolder));
32
        $errors   = [];
33
        foreach ($path as $folder) {
34
            $folderName = $folder . $searchedFolder;
35
            if (!is_dir($folderName)) {
36
                $errors[] = '<error>Missing ' . $searchedFolder . ' folder in ' . basename($folder) . ' </error>';
37
            }
38
            if (is_dir($folderName)) {
39
                $filesInDir = array_diff(scandir($folderName), ['.', '..']);
0 ignored issues
show
Bug introduced by
It seems like scandir($folderName) 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

39
                $filesInDir = array_diff(/** @scrutinizer ignore-type */ scandir($folderName), ['.', '..']);
Loading history...
40
                if (count($filesInDir) === 0) {
41
                    $errors[] = '<error>Empty   ' . $searchedFolder . ' folder in ' . basename($folder) . ' </error>';
42
                }
43
            }
44
        }
45
        if (empty($errors)) {
46
            $io->write('<info>No errors found while checking for ' . $searchedFolder . ' files</info>');
47
        } else {
48
            $io->writeError($errors);
49
            throw new Exception('Please check if ' . $searchedFolder . ' folder is not missing and is not empty');
50
        }
51
    }
52
}
53