Passed
Push — master ( be994a...680396 )
by Hilari
01:36
created

DataProviderMaintainer::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace HMoragrega\PhpSpec\DataProvider\Maintainer;
4
5
use HMoragrega\PhpSpec\DataProvider\Parser\DataProviderExtractor;
6
use HMoragrega\PhpSpec\DataProvider\Parser\ExampleParser;
7
use PhpSpec\Loader\Node\ExampleNode;
8
use PhpSpec\Runner\CollaboratorManager;
9
use PhpSpec\Runner\Maintainer\Maintainer;
10
use PhpSpec\Runner\MatcherManager;
11
use PhpSpec\Specification;
12
13
class DataProviderMaintainer implements Maintainer
14
{
15
    const EXAMPLE_NUMBER_PATTERN = '/^(\d+)\)/';
16
17
    /**
18
     * @var ExampleParser
19
     */
20
    private $exampleParser;
21
22
    /**
23
     * @var DataProviderExtractor
24
     */
25
    private $dataProviderExtractor;
26
27
    /**
28
     * @var array[]
29
     */
30
    private $providerData = [];
31
32
    /**
33
     * @param ExampleParser         $exampleParser
34
     * @param DataProviderExtractor $dataProviderExtractor
35
     */
36
    public function __construct(ExampleParser $exampleParser, DataProviderExtractor $dataProviderExtractor)
37
    {
38
        $this->exampleParser         = $exampleParser;
39
        $this->dataProviderExtractor = $dataProviderExtractor;
40
    }
41
42
    /**
43
     * @param ExampleNode $example
44
     *
45
     * @return boolean
46
     * @throws \ReflectionException
47
     */
48
    public function supports(ExampleNode $example): bool
49
    {
50
        $dataProviderMethod = $this->exampleParser->getDataProvider($example);
51
52
        if (isset($this->providerData[$dataProviderMethod])) {
53
            return true;
54
        }
55
56
        $providedData = $this->dataProviderExtractor->getProvidedData($example, $dataProviderMethod);
0 ignored issues
show
Bug introduced by
It seems like $dataProviderMethod can also be of type null; however, parameter $dataProviderMethod of HMoragrega\PhpSpec\DataP...ctor::getProvidedData() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $providedData = $this->dataProviderExtractor->getProvidedData($example, /** @scrutinizer ignore-type */ $dataProviderMethod);
Loading history...
57
58
        foreach ($providedData as $dataRow) {
59
            if (!is_array($dataRow)) {
60
                return false;
61
            }
62
        }
63
64
        $this->providerData[$dataProviderMethod] = $providedData;
65
66
        return true;
67
    }
68
69
    /**
70
     * @param ExampleNode         $example
71
     * @param Specification       $context
72
     * @param MatcherManager      $matchers
73
     * @param CollaboratorManager $collaborators
74
     */
75
    public function prepare(ExampleNode $example, Specification $context, MatcherManager $matchers, CollaboratorManager $collaborators): void
76
    {
77
        $exampleNum   = $this->getExampleNumber($example->getTitle());
78
        $providedData = $this->providerData[$this->exampleParser->getDataProvider($example)];
79
80
        if (!array_key_exists($exampleNum, $providedData)) {
81
            return ;
82
        }
83
84
        $data               = $providedData[$exampleNum];
85
        $function           = $example->getFunctionReflection();
86
        $numberOfParameters = $function->getNumberOfParameters();
87
88
        foreach ($function->getParameters() as $position => $parameter) {
89
            if ($numberOfParameters < count($data)) {
90
                $position++;
91
            }
92
93
            if (!isset($data[$position])) {
94
                continue;
95
            }
96
97
            $collaborators->set($parameter->getName(), $data[$position]);
98
        }
99
    }
100
101
    /**
102
     * @param ExampleNode         $example
103
     * @param Specification       $context
104
     * @param MatcherManager      $matchers
105
     * @param CollaboratorManager $collaborators
106
     */
107
    public function teardown(ExampleNode $example, Specification $context, MatcherManager $matchers, CollaboratorManager $collaborators): void
108
    {
109
        unset($this->providerData[$this->exampleParser->getDataProvider($example)]);
110
    }
111
112
    /**
113
     * @return integer
114
     */
115
    public function getPriority(): int
116
    {
117
        return 50;
118
    }
119
120
    /**
121
     * @param string $title
122
     *
123
     * @return int
124
     */
125
    private function getExampleNumber(string $title): int
126
    {
127
        if (!preg_match(self::EXAMPLE_NUMBER_PATTERN, $title, $matches)) {
128
            return 0;
129
        }
130
131
        return (int) $matches[1] - 1;
132
    }
133
}