Completed
Push — master ( c8c0c1...47af09 )
by Jakub
01:44
created

SkipChecker   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
eloc 33
c 2
b 0
f 1
dl 0
loc 91
rs 10
ccs 36
cts 39
cp 0.9231

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addDefaultCheckers() 0 5 1
A shouldSkip() 0 17 6
A addChecker() 0 3 1
A __construct() 0 4 1
A checkPhpSapi() 0 6 2
A getSkipValue() 0 3 1
A checkPhpVersion() 0 6 2
A checkLoadedExtension() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester;
6
7
use MyTester\Annotations\Reader;
8
use Nette\Utils\Arrays;
9
10
/**
11
 * SkipChecker
12
 *
13
 * @author Jakub Konečný
14
 * @internal
15
 */
16
final class SkipChecker
17
{
18
    use \Nette\SmartObject;
19
20
    public const ANNOTATION_NAME = "skip";
21
22
    private Reader $annotationsReader;
23
    /** @var callable[] */
24
    private array $checkers = [];
25
26 1
    public function __construct(Reader $annotationsReader)
27
    {
28 1
        $this->annotationsReader = $annotationsReader;
29 1
        $this->addDefaultCheckers();
30 1
    }
31
32 1
    private function addDefaultCheckers(): void
33
    {
34 1
        $this->addChecker("php", [$this, "checkPhpVersion"]);
35 1
        $this->addChecker("extension", [$this, "checkLoadedExtension"]);
36 1
        $this->addChecker("sapi", [$this, "checkPhpSapi"]);
37 1
    }
38
39 1
    public function addChecker(string $name, callable $callback): void
40
    {
41 1
        $this->checkers[$name] = $callback;
42 1
    }
43
44
    /**
45
     * @return mixed
46
     */
47 1
    public function getSkipValue(string $class, string $method)
48
    {
49 1
        return $this->annotationsReader->getAnnotation(static::ANNOTATION_NAME, $class, $method);
50
    }
51
52
    /**
53
     * Check whether to skip a test method
54
     *
55
     * @return bool|string
56
     */
57 1
    public function shouldSkip(string $class, string $method)
58
    {
59 1
        $value = $this->getSkipValue($class, $method);
60 1
        if ($value === null) {
61 1
            return false;
62 1
        } elseif (is_scalar($value)) {
63 1
            return (bool) $value;
64 1
        } elseif (is_iterable($value)) {
65 1
            foreach ($value as $k => $v) {
66 1
                $checker = Arrays::get($this->checkers, $k, null);
67 1
                if ($checker === null) {
68
                    return false;
69
                }
70 1
                return $checker($v);
71
            }
72
        }
73
        return false;
74
    }
75
76
    /**
77
     * @param mixed $value
78
     */
79 1
    public function checkPhpVersion($value): ?string
80
    {
81 1
        if (version_compare(PHP_VERSION, (string) $value, "<")) {
82 1
            return "PHP version is lesser than $value";
83
        }
84 1
        return null;
85
    }
86
87
    /**
88
     * @param mixed $value
89
     */
90 1
    public function checkLoadedExtension($value): ?string
91
    {
92 1
        if (!extension_loaded($value)) {
93 1
            return "extension $value is not loaded";
94
        }
95 1
        return null;
96
    }
97
98
    /**
99
     * @param mixed $value
100
     */
101 1
    public function checkPhpSapi($value): ?string
102
    {
103 1
        if (PHP_SAPI != $value) {
104 1
            return "the sapi is not $value";
105
        }
106
        return null;
107
    }
108
}
109