Passed
Push — master ( 11230f...5f5cdb )
by Jakub
12:32
created

SkipChecker   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addDefaultCheckers() 0 5 1
A addChecker() 0 3 1
A __construct() 0 4 1
A shouldSkip() 0 17 6
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
    }
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
    }
38
39 1
    public function addChecker(string $name, callable $callback): void
40
    {
41 1
        $this->checkers[$name] = $callback;
42
    }
43
44 1
    public function getSkipValue(string $class, string $method): mixed
45
    {
46 1
        return $this->annotationsReader->getAnnotation(static::ANNOTATION_NAME, $class, $method);
47
    }
48
49
    /**
50
     * Check whether to skip a test method
51
     */
52 1
    public function shouldSkip(string $class, string $method): bool|string
53
    {
54 1
        $value = $this->getSkipValue($class, $method);
55 1
        if ($value === null) {
56 1
            return false;
57 1
        } elseif (is_scalar($value)) {
58 1
            return (bool) $value;
59 1
        } elseif (is_iterable($value)) {
60 1
            foreach ($value as $k => $v) {
61 1
                $checker = Arrays::get($this->checkers, $k, null);
62 1
                if ($checker === null) {
63
                    return false;
64
                }
65 1
                return $checker($v);
66
            }
67
        }
68
        return false;
69
    }
70
71 1
    public function checkPhpVersion(mixed $value): ?string
72
    {
73 1
        if (version_compare(PHP_VERSION, (string) $value, "<")) {
74 1
            return "PHP version is lesser than $value";
75
        }
76 1
        return null;
77
    }
78
79 1
    public function checkLoadedExtension(mixed $value): ?string
80
    {
81 1
        if (!extension_loaded($value)) {
82 1
            return "extension $value is not loaded";
83
        }
84 1
        return null;
85
    }
86
87 1
    public function checkPhpSapi(mixed $value): ?string
88
    {
89 1
        if (PHP_SAPI != $value) {
90 1
            return "the sapi is not $value";
91
        }
92
        return null;
93
    }
94
}
95