Passed
Push — master ( e729e7...b0febc )
by Jakub
01:57
created

SkipChecker   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
eloc 31
c 2
b 0
f 1
dl 0
loc 75
rs 10
ccs 34
cts 34
cp 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addDefaultCheckers() 0 5 1
A addChecker() 0 3 1
A __construct() 0 4 1
A getSkipValue() 0 3 1
A shouldSkip() 0 15 5
A checkPhpSapi() 0 6 2
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 (is_scalar($value)) {
56 1
            return (bool) $value;
57 1
        } elseif (is_iterable($value)) {
58 1
            foreach ($value as $k => $v) {
59 1
                $checker = Arrays::get($this->checkers, $k, null);
60 1
                if ($checker === null) {
61 1
                    return false;
62
                }
63 1
                return $checker($v);
64
            }
65
        }
66 1
        return false;
67
    }
68
69 1
    public function checkPhpVersion(mixed $value): ?string
70
    {
71 1
        if (version_compare(PHP_VERSION, (string) $value, "<")) {
72 1
            return "PHP version is lesser than $value";
73
        }
74 1
        return null;
75
    }
76
77 1
    public function checkLoadedExtension(mixed $value): ?string
78
    {
79 1
        if (!extension_loaded($value)) {
80 1
            return "extension $value is not loaded";
81
        }
82 1
        return null;
83
    }
84
85 1
    public function checkPhpSapi(mixed $value): ?string
86
    {
87 1
        if (PHP_SAPI != $value) {
88 1
            return "the sapi is not $value";
89
        }
90 1
        return null;
91
    }
92
}
93