Passed
Push — master ( bace86...bb4cad )
by Dmitriy
05:30 queued 02:57
created

testSkipCollectOnMatchIgnoreReferences()   A

Complexity

Conditions 2
Paths 12

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 12
nop 8
dl 0
loc 31
rs 9.7333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Unit\Collector;
6
7
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
8
use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector;
9
use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase;
10
11
final class HttpStreamCollectorTest extends AbstractCollectorTestCase
12
{
13
    /**
14
     * @param HttpStreamCollector $collector
15
     */
16
    protected function collectTestData(CollectorInterface $collector): void
17
    {
18
        $collector->collect(
0 ignored issues
show
Bug introduced by
The method collect() does not exist on Yiisoft\Yii\Debug\Collector\CollectorInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Yii\Debug\Collec...mmaryCollectorInterface or Yiisoft\Yii\Debug\Tests\...\Support\DummyCollector. Are you sure you never get one of those? ( Ignorable by Annotation )

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

18
        $collector->/** @scrutinizer ignore-call */ 
19
                    collect(
Loading history...
19
            operation: 'read',
20
            path: __FILE__,
21
            args: ['arg1' => 'v1', 'arg2' => 'v2'],
22
        );
23
        $collector->collect(
24
            operation: 'read',
25
            path: __FILE__,
26
            args: ['arg3' => 'v3', 'arg4' => 'v4'],
27
        );
28
    }
29
30
    /**
31
     * @dataProvider dataSkipCollectOnMatchIgnoreReferences
32
     */
33
    public function testSkipCollectOnMatchIgnoreReferences(
34
        string $url,
35
        callable $before,
36
        array $ignoredPathPatterns,
37
        array $ignoredClasses,
38
        array $ignoredUrls,
39
        callable $operation,
40
        callable $after,
41
        array|callable $assertResult,
42
    ): void {
43
        $before($url);
44
45
        try {
46
            $collector = new HttpStreamCollector(
47
                ignoredPathPatterns: $ignoredPathPatterns,
48
                ignoredClasses: $ignoredClasses,
49
                ignoredUrls: $ignoredUrls,
50
            );
51
            $collector->startup();
52
53
            $operation($url);
54
55
            $collected = $collector->getCollected();
56
            $collector->shutdown();
57
        } finally {
58
            $after($url);
59
        }
60
        if (is_array($assertResult)) {
0 ignored issues
show
introduced by
The condition is_array($assertResult) is always true.
Loading history...
61
            $this->assertSame($assertResult, $collected);
62
        } else {
63
            $assertResult($url, $collected);
64
        }
65
    }
66
67
    public function dataSkipCollectOnMatchIgnoreReferences(): iterable
68
    {
69
        $httpStreamBefore = function (string $url) {
0 ignored issues
show
Unused Code introduced by
The parameter $url 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

69
        $httpStreamBefore = function (/** @scrutinizer ignore-unused */ string $url) {

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...
70
        };
71
        $httpStreamOperation = function (string $url) {
72
            $stream = fopen($url, 'r');
73
            fread($stream, 4);
74
            ftell($stream);
75
            feof($stream);
76
            fstat($stream);
77
            fclose($stream);
78
        };
79
        $httpStreamAfter = $httpStreamBefore;
80
81
        yield 'file stream matched' => [
82
            $url = 'http://example.com',
83
            $httpStreamBefore,
84
            [],
85
            [],
86
            [],
87
            $httpStreamOperation,
88
            $httpStreamAfter,
89
            function (string $url, array $collected) {
90
                $this->assertArrayHasKey('read', $collected);
91
                $this->assertIsArray($collected['read']);
92
                $this->assertCount(1, $collected['read']);
93
94
                $readItem = $collected['read'][0];
95
                $this->assertSame($url, $readItem['uri']);
96
                $this->assertArrayHasKey('args', $readItem);
97
98
                $readItemArgs = $readItem['args'];
99
                $this->assertCount(3, $readItemArgs);
100
101
                $this->assertSame('GET', $readItemArgs['method']);
102
                $this->assertIsArray($readItemArgs['response_headers']);
103
                $this->assertNotEmpty($readItemArgs['response_headers']);
104
                $this->assertIsArray($readItemArgs['request_headers']);
105
                $this->assertEmpty($readItemArgs['request_headers']);
106
            },
107
        ];
108
        yield 'file stream ignored by path' => [
109
            $url,
110
            $httpStreamBefore,
111
            ['/' . basename(__FILE__, '.php') . '/'],
112
            [],
113
            [],
114
            $httpStreamOperation,
115
            $httpStreamAfter,
116
            [],
117
        ];
118
        yield 'file stream ignored by class' => [
119
            $url,
120
            $httpStreamBefore,
121
            [],
122
            [self::class],
123
            [],
124
            $httpStreamOperation,
125
            $httpStreamAfter,
126
            [],
127
        ];
128
        yield 'file stream ignored by url' => [
129
            $url,
130
            $httpStreamBefore,
131
            [],
132
            [],
133
            ['/example/'],
134
            $httpStreamOperation,
135
            $httpStreamAfter,
136
            [],
137
        ];
138
    }
139
140
    protected function getCollector(): CollectorInterface
141
    {
142
        return new HttpStreamCollector();
143
    }
144
145
    protected function checkCollectedData(array $data): void
146
    {
147
        parent::checkCollectedData($data);
148
        $collected = $data;
149
        $this->assertCount(1, $collected);
150
151
        $this->assertCount(2, $collected['read']);
152
        $this->assertEquals([
153
            ['uri' => __FILE__, 'args' => ['arg1' => 'v1', 'arg2' => 'v2']],
154
            ['uri' => __FILE__, 'args' => ['arg3' => 'v3', 'arg4' => 'v4']],
155
        ], $collected['read']);
156
    }
157
158
    protected function checkSummaryData(array $data): void
159
    {
160
        parent::checkSummaryData($data);
161
        $this->assertArrayHasKey('http_stream', $data);
162
        $this->assertEquals(['read' => 2], $data['http_stream']);
163
    }
164
}
165