Passed
Push — php81 ( 165b0c...5beee4 )
by Jakub
02:44
created

DataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
eloc 22
c 2
b 0
f 1
dl 0
loc 40
ccs 15
cts 19
cp 0.7895
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getData() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester;
6
7
use MyTester\Annotations\Reader;
8
use ReflectionMethod;
9
10
/**
11
 * DataProvider
12
 *
13
 * @author Jakub Konečný
14
 * @internal
15
 */
16
final class DataProvider
17
{
18
    use \Nette\SmartObject;
19
20
    public const ANNOTATION_NAME = "dataProvider";
21
22
    private Reader $annotationsReader;
23
24 1
    public function __construct(Reader $annotationsReader)
25
    {
26 1
        $this->annotationsReader = $annotationsReader;
27
    }
28
29
    /**
30
     * @throws InvalidDataProviderException
31
     */
32 1
    public function getData(object $class, string $method): array
33
    {
34 1
        $reflection = new ReflectionMethod($class, $method);
35 1
        if ($reflection->getNumberOfParameters() < 1) {
36 1
            return [];
37
        }
38 1
        $dataProvider = $this->annotationsReader->getAnnotation(static::ANNOTATION_NAME, $class, $method);
39 1
        if (is_string($dataProvider)) {
40 1
            $className = $reflection->getDeclaringClass()->getName();
41
            try {
42 1
                $reflection = new ReflectionMethod($class, $dataProvider);
43 1
                if (!$reflection->isPublic()) {
44
                    throw new InvalidDataProviderException("Method $className::$dataProvider is not public.");
45
                }
46 1
                $result = call_user_func([$class, $dataProvider]);
47 1
                if (!is_array($result)) {
48
                    throw new InvalidDataProviderException("Method $className::$dataProvider has to return an array.");
49
                }
50 1
                return $result;
51
            } catch (\ReflectionException $e) {
52
                throw new InvalidDataProviderException("Method $className::$dataProvider does not exist.", 0, $e);
53
            }
54
        }
55 1
        return [];
56
    }
57
}
58