Passed
Pull Request — master (#412)
by
unknown
17:58 queued 15:20
created

MainBench   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRules() 0 4 2
A cloneRule() 0 4 2
A provider() 0 29 1
A setUp() 0 3 1
A benchValidate() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Benchmark;
6
7
use Generator;
8
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
9
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
10
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
11
use PhpBench\Benchmark\Metadata\Annotations\Revs;
12
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
13
use Yiisoft\Validator\Rule\Boolean;
14
use Yiisoft\Validator\Rule\Number;
15
use Yiisoft\Validator\Tests\Support\ValidatorFactory;
16
use Yiisoft\Validator\ValidatorInterface;
17
18
/**
19
 * @BeforeMethods("setUp")
20
 */
21
final class MainBench
22
{
23
    private ValidatorInterface $validator;
24
25
    public function setUp(array $params): void
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

25
    public function setUp(/** @scrutinizer ignore-unused */ array $params): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $this->validator = ValidatorFactory::make();
28
    }
29
30
    /**
31
     * @Revs(1000)
32
     * @Iterations(10)
33
     * @ParamProviders("provider")
34
     * @Warmup(1)
35
     */
36
    public function benchValidate(array $params): void
37
    {
38
        $rules = $this->generateRules($params['rules'], $params['count']);
39
        $this->validator->validate($params['data'], $rules);
40
    }
41
42
    public function provider(): Generator
43
    {
44
        $data = [
45
            'bool' => true,
46
            'int' => 555,
47
        ];
48
        yield 'simple 1' => [
49
            'data' => $data,
50
            'rules' => [
51
                'bool' => new Boolean(),
52
                'int' => new Number(asInteger: true),
53
            ],
54
            'count' => 1,
55
        ];
56
        yield 'simple 10' => [
57
            'data' => $data,
58
            'rules' => [
59
                'bool' => new Boolean(),
60
                'int' => new Number(asInteger: true),
61
            ],
62
            'count' => 10,
63
        ];
64
        yield 'simple 100' => [
65
            'data' => $data,
66
            'rules' => [
67
                'bool' => new Boolean(),
68
                'int' => new Number(asInteger: true),
69
            ],
70
            'count' => 100,
71
        ];
72
    }
73
74
    public function generateRules(array $rules, int $count): iterable
75
    {
76
        foreach ($rules as $attribute => $rule) {
77
            yield $attribute => $this->cloneRule($rule, $count);
78
        }
79
    }
80
81
    private function cloneRule(mixed $rule, int $count): Generator
82
    {
83
        for ($i = 0; $i < $count; $i++) {
84
            yield clone $rule;
85
        }
86
    }
87
}
88