Linting::execute()   A
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 3
b 0
f 0
nc 18
nop 4
dl 0
loc 20
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
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\PHP\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Console\IOUtil;
17
use CaptainHook\App\Exception\ActionFailed;
18
use CaptainHook\App\Hook\Action;
19
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
20
use SebastianFeldmann\Git\Repository;
21
22
/**
23
 * Class Linter
24
 *
25
 * @package CaptainHook
26
 * @author  Sebastian Feldmann <[email protected]>
27
 * @link    https://github.com/captainhook-git/captainhook
28
 * @since   Class available since Release 1.0.5
29
 */
30
class Linting implements Action
31
{
32
    /**
33
     * Path to php executable, default 'php'
34
     *
35
     * @var string
36
     */
37
    private $php;
38
39
    /**
40
     * Executes the action
41
     *
42
     * @param  \CaptainHook\App\Config           $config
43
     * @param  \CaptainHook\App\Console\IO       $io
44
     * @param  \SebastianFeldmann\Git\Repository $repository
45
     * @param  \CaptainHook\App\Config\Action    $action
46
     * @return void
47
     * @throws \Exception
48
     */
49 2
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
50
    {
51
        // we have to provide a custom filter because we do not want to check any deleted files
52 2
        $changedPHPFiles  = $repository->getIndexOperator()->getStagedFilesOfType('php', ['A', 'C', 'M']);
53 2
        $this->php        = !empty($config->getPhpPath()) ? $config->getPhpPath() : 'php';
54 2
        $failedFilesCount = 0;
55
56 2
        foreach ($changedPHPFiles as $file) {
57 2
            $prefix = IOUtil::PREFIX_OK;
58 2
            if ($this->hasSyntaxErrors($file)) {
59 1
                $failedFilesCount++;
60 1
                $io->write('  ' . IOUtil::PREFIX_FAIL . ' ' . $file, true, IO::NORMAL);
61
            }
62 2
            $io->write('  ' . $prefix . ' ' . $file, true, IO::VERBOSE);
63
        }
64
65 2
        if ($failedFilesCount > 0) {
66 1
            $s = $failedFilesCount > 1 ? 's' : '';
67 1
            throw new ActionFailed(
68 1
                'Linting failed: PHP syntax errors in ' . $failedFilesCount . ' file' . $s
69 1
            );
70
        }
71
    }
72
73
    /**
74
     * Lint a php file
75
     *
76
     * @param  string $file
77
     * @return bool
78
     */
79 2
    protected function hasSyntaxErrors(string $file): bool
80
    {
81 2
        $process = new Processor();
82 2
        $result  = $process->run($this->php . ' -l ' . escapeshellarg($file));
83
84 2
        return !$result->isSuccessful();
85
    }
86
}
87