Passed
Pull Request — master (#98)
by Sebastian
02:15
created

DoesNotContainRegex::execute()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 31
rs 9.2888
cc 5
nc 7
nop 4
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\Config\Options;
16
use CaptainHook\App\Console\IO;
17
use CaptainHook\App\Exception\ActionFailed;
18
use CaptainHook\App\Hook\Action;
19
use Exception;
20
use SebastianFeldmann\Git\Repository;
21
22
/**
23
 * Class NotContainsRegex
24
 *
25
 * @package CaptainHook
26
 * @author  Felix Edelmann <[email protected]>
27
 * @link    https://github.com/captainhookphp/captainhook
28
 * @since   TODO
29
 */
30
class DoesNotContainRegex implements Action
31
{
32
    /**
33
     * Executes the action
34
     *
35
     * @param  \CaptainHook\App\Config           $config
36
     * @param  \CaptainHook\App\Console\IO       $io
37
     * @param  \SebastianFeldmann\Git\Repository $repository
38
     * @param  \CaptainHook\App\Config\Action    $action
39
     * @return void
40
     * @throws \Exception
41
     */
42
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
43
    {
44
        $options = $action->getOptions();
45
        $regex = $options->get('regex');
46
        if ($regex === null) {
47
            throw new Exception('Missing option "regex" for DoesNotContainRegex action');
48
        }
49
50
        $files = $this->getFiles($options, $repository);
51
52
        $failedFiles = 0;
53
        $totalMatchesCount = 0;
54
        foreach ($files as $file) {
55
            $fileContent = file_get_contents($file);
56
            $matchCount = preg_match_all($regex, $fileContent, $matches);
57
58
            if ($matchCount > 0) {
59
                $io->write('- <error>FAIL</error> ' . $file . ' - ' . $matchCount . ' matches', true);
60
                $failedFiles++;
61
                $totalMatchesCount += $matchCount;
62
            } else {
63
                $io->write('- <info>OK</info> ' . $file, true, IO::VERBOSE);
64
            }
65
        }
66
67
        if ($failedFiles > 0) {
68
            $regexName = $options->get('regexName', $regex);
69
            throw new ActionFailed('<error>Regex \'' . $regexName . '\' failed:</error> ' . $totalMatchesCount . ' matches in ' . $failedFiles . ' files');
70
        }
71
72
        $io->write('<info>No regex matches found</info>');
73
    }
74
75
    /**
76
     * Returns the files that need to be checked.
77
     * 
78
     * @param  \CaptainHook\App\Config\Options   $options
79
     * @param  \SebastianFeldmann\Git\Repository $repository
80
     * @return array
81
     */
82
    protected function getFiles(Options $options, Repository $repository): array
83
    {
84
        $index = $repository->getIndexOperator();
85
86
        $fileExtensions = $options->get('fileExtensions');
87
        if ($fileExtensions !== null) {
88
            $files = [];
89
            foreach ($fileExtensions as $ext) {
0 ignored issues
show
Bug introduced by
The expression $fileExtensions of type string is not traversable.
Loading history...
90
                $files = array_merge($files, $index->getStagedFilesOfType($ext));
91
            }
92
            return $files;
93
        }
94
95
        return $index->getStagedFiles();
96
    }
97
}
98