Completed
Push — master ( 14c836...906b18 )
by Björn
06:09 queued 03:52
created

ExceptionHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer\Helper;
6
7
use BestIt\CodeSniffer\CodeError;
8
use BestIt\CodeSniffer\CodeWarning;
9
use PHP_CodeSniffer\Files\File;
10
11
/**
12
 * Registers the exception as an error or warning on the file.
13
 *
14
 * @author blange <[email protected]>
15
 * @package BestIt\CodeSniffer\Helper
16
 */
17
class ExceptionHelper
18
{
19
    /**
20
     * The sniffed file.
21
     *
22
     * @var File
23
     */
24
    private File $file;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    /**
27
     * ExceptionHelper constructor.
28
     *
29
     * @param File $file The sniffed file.
30
     */
31
    public function __construct(File $file)
32
    {
33
        $this->file = $file;
34
    }
35
36
    /**
37
     * Registers the exception as an error or warning on the file.
38
     *
39
     * @param CodeWarning $exception The error which should be handled.
40
     *
41
     * @return bool Should this error be fixed?
42
     */
43
    public function handleException(CodeWarning $exception): bool
44
    {
45
        $isError = $exception instanceof CodeError;
46
        $isFixable = $exception->isFixable();
47
        $method = 'add';
48
49
        if ($isFixable) {
50
            $method .= 'Fixable';
51
        }
52
53
        $method .= $isError ? 'Error' : 'Warning';
54
55
        return $this->file->$method(
56
            $exception->getMessage(),
57
            $exception->getStackPosition(),
58
            $exception->getCode(),
59
            $exception->getPayload()
60
        );
61
    }
62
}
63