RiskyToFailedListener::getErrorReason()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 37
ccs 22
cts 22
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 8
nop 1
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\PhpUnitExtended\Listener;
3
4
use PHPUnit\Framework\AssertionFailedError;
5
use PHPUnit\Framework\CoveredCodeNotExecutedException;
6
use PHPUnit\Framework\InvalidCoversTargetException;
7
use PHPUnit\Framework\MissingCoversAnnotationException;
8
use PHPUnit\Framework\OutputError;
9
use PHPUnit\Framework\Test;
10
use PHPUnit\Framework\TestCase;
11
use PHPUnit\Framework\TestListener;
12
use PHPUnit\Framework\TestListenerDefaultImplementation;
13
use PHPUnit\Framework\TestResult;
14
use PHPUnit\Framework\UnintentionallyCoveredCodeError;
15
use PHPUnit\Framework\Warning;
16
17
/**
18
 * @see doc/listener/StrictCoverageListener.md
19
 */
20
class RiskyToFailedListener implements TestListener
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated. ( Ignorable by Annotation )

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

20
class RiskyToFailedListener implements /** @scrutinizer ignore-deprecated */ TestListener

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
21
{
22
    use TestListenerDefaultImplementation;
0 ignored issues
show
Deprecated Code introduced by
The trait PHPUnit\Framework\TestLi...erDefaultImplementation has been deprecated: The `TestListener` interface is deprecated ( Ignorable by Annotation )

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

22
    use /** @scrutinizer ignore-deprecated */ TestListenerDefaultImplementation;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function addWarning(Test $test, Warning $e, float $time) : void
28
    {
29 1
        $this->addErrorIfNeeded($test, $e, $time);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 11
    public function addRiskyTest(Test $test, \Throwable $exception, float $time) : void
36
    {
37 11
        $this->addErrorIfNeeded($test, $exception, $time);
38
    }
39
40 12
    protected function addErrorIfNeeded(Test $test, \Throwable $exception, $time)
41
    {
42
        /* Must be TestCase instance to have access to "getTestResultObject" method */
43 12
        if ($test instanceof TestCase) {
44 12
            if (!$test->getTestResultObject()) {
45 11
                $test->setTestResultObject(new TestResult());
46
            }
47 12
            $test->getTestResultObject()->addFailure(
48 12
                $test,
49 12
                new AssertionFailedError(
50 12
                    sprintf(
51 12
                        "Strict mode - %s :\n%s",
52 12
                        $this->getErrorReason($exception),
53 12
                        $exception->getMessage()
54 12
                    )
55 12
                ),
56 12
                $time
57 12
            );
58
        }
59
    }
60
61 12
    protected function getErrorReason(\Throwable $exception): string
62
    {
63 12
        if ($exception instanceof OutputError) {
64
            /* beStrictAboutOutputDuringTests="true" */
65 1
            return 'No output during test';
66 11
        } elseif ($exception instanceof UnintentionallyCoveredCodeError
67 11
            || $exception instanceof InvalidCoversTargetException
68
        ) {
69
            /* checkForUnintentionallyCoveredCode="true" */
70 2
            return 'Executed code must be defined with @covers and @uses annotations';
71 9
        } elseif (str_contains($exception->getMessage(), '--- Global variables before the test')) {
72
            /* beStrictAboutChangesToGlobalState="true" (no specific exception) for globals */
73 1
            return 'No global variable manipulation during test';
74 8
        } elseif (str_contains($exception->getMessage(), '--- Static attributes before the test')) {
75
            /* beStrictAboutChangesToGlobalState="true" (no specific exception) for static var */
76
            /* Only when beStrictAboutChangesToGlobalState="true" */
77 1
            return 'No static attribute manipulation during test';
78 7
        } elseif (str_contains($exception->getMessage(), 'This test did not perform any assertions')) {
79
            /* beStrictAboutTestsThatDoNotTestAnything="true" (no specific exception) */
80 1
            return 'No test that do not test anything';
81 6
        } elseif ($exception instanceof CoveredCodeNotExecutedException
82 6
            || preg_match('#"@covers [^"]+" is invalid#', $exception->getMessage())
83
        ) {
84
            /* forceCoversAnnotation="true" (no specific exception) */
85 4
            return 'Only executed code must be defined with @covers and @uses annotations';
86 2
        } elseif ($exception instanceof MissingCoversAnnotationException
87 2
            || str_contains(
88 2
                $exception->getMessage(),
89 2
                'This test does not have a @covers annotation but is expected to have one'
90 2
            )
91
        ) {
92
            /* forceCoversAnnotation="true" (no specific exception) */
93 1
            return 'Missing @covers or @coversNothing annotation';
94
        }
95
96
        // Always return an error even if it's not a known/managed error
97 1
        return $exception->getMessage();
98
    }
99
}
100