CallInitializerTest::testBodyStructure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator;
6
7
use Laminas\Code\Generator\PropertyGenerator;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer;
11
use ProxyManager\ProxyGenerator\Util\Properties;
12
use ProxyManagerTestAsset\ClassWithMixedProperties;
13
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
14
use ReflectionClass;
15
16
/**
17
 * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer}
18
 *
19
 * @group Coverage
20
 * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer
21
 */
22
final class CallInitializerTest extends TestCase
23
{
24
    public function testBodyStructure() : void
25
    {
26
        $initializer           = $this->createMock(PropertyGenerator::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $initializer is correct as $this->createMock(\Lamin...opertyGenerator::class) (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
27
        $initializationTracker = $this->createMock(PropertyGenerator::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $initializationTracker is correct as $this->createMock(\Lamin...opertyGenerator::class) (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
29
        $initializer->method('getName')->willReturn('init');
0 ignored issues
show
Bug introduced by
The method method cannot be called on $initializer (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
30
        $initializationTracker->method('getName')->willReturn('track');
0 ignored issues
show
Bug introduced by
The method method cannot be called on $initializationTracker (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
31
32
        $callInitializer = new CallInitializer(
33
            $initializer,
0 ignored issues
show
Documentation introduced by
$initializer is of type null, but the function expects a object<Laminas\Code\Generator\PropertyGenerator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
            $initializationTracker,
0 ignored issues
show
Documentation introduced by
$initializationTracker is of type null, but the function expects a object<Laminas\Code\Generator\PropertyGenerator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
            Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedProperties::class))
36
        );
37
38
        $expectedCode = <<<'PHP'
39
if ($this->track || ! $this->init) {
40
    return;
41
}
42
43
$this->track = true;
44
45
$this->publicProperty0 = 'publicProperty0';
46
$this->publicProperty1 = 'publicProperty1';
47
$this->publicProperty2 = 'publicProperty2';
48
$this->protectedProperty0 = 'protectedProperty0';
49
$this->protectedProperty1 = 'protectedProperty1';
50
$this->protectedProperty2 = 'protectedProperty2';
51
static $cacheProxyManagerTestAsset_ClassWithMixedProperties;
52
53
$cacheProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(static function ($instance) {
54
    $instance->privateProperty0 = 'privateProperty0';
55
    $instance->privateProperty1 = 'privateProperty1';
56
    $instance->privateProperty2 = 'privateProperty2';
57
}, null, 'ProxyManagerTestAsset\\ClassWithMixedProperties');
58
59
$cacheProxyManagerTestAsset_ClassWithMixedProperties($this);
60
61
62
63
64
$properties = [
65
    'publicProperty0' => & $this->publicProperty0,
66
    'publicProperty1' => & $this->publicProperty1,
67
    'publicProperty2' => & $this->publicProperty2,
68
    '' . "\0" . '*' . "\0" . 'protectedProperty0' => & $this->protectedProperty0,
69
    '' . "\0" . '*' . "\0" . 'protectedProperty1' => & $this->protectedProperty1,
70
    '' . "\0" . '*' . "\0" . 'protectedProperty2' => & $this->protectedProperty2,
71
];
72
73
static $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties;
74
75
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(function ($instance, array & $properties) {
76
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty0'] = & $instance->privateProperty0;
77
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty1'] = & $instance->privateProperty1;
78
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty2'] = & $instance->privateProperty2;
79
}, $this, 'ProxyManagerTestAsset\\ClassWithMixedProperties');
80
81
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties($this, $properties);
82
83
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties);
84
$this->track = false;
85
86
return $result;
87
PHP;
88
89
        self::assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<ProxyManagerTest\...or\CallInitializerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            $expectedCode,
91
            $callInitializer->getBody()
92
        );
93
    }
94
95
    public function testBodyStructureWithTypedProperties() : void
96
    {
97
        $initializer           = $this->createMock(PropertyGenerator::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $initializer is correct as $this->createMock(\Lamin...opertyGenerator::class) (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
        $initializationTracker = $this->createMock(PropertyGenerator::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $initializationTracker is correct as $this->createMock(\Lamin...opertyGenerator::class) (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
99
100
        $initializer->method('getName')->willReturn('init');
0 ignored issues
show
Bug introduced by
The method method cannot be called on $initializer (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
101
        $initializationTracker->method('getName')->willReturn('track');
0 ignored issues
show
Bug introduced by
The method method cannot be called on $initializationTracker (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
102
103
        $callInitializer = new CallInitializer(
104
            $initializer,
0 ignored issues
show
Documentation introduced by
$initializer is of type null, but the function expects a object<Laminas\Code\Generator\PropertyGenerator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
            $initializationTracker,
0 ignored issues
show
Documentation introduced by
$initializationTracker is of type null, but the function expects a object<Laminas\Code\Generator\PropertyGenerator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedTypedProperties::class))
107
        );
108
109
        $expectedCode = <<<'PHP'
110
if ($this->track || ! $this->init) {
111
    return;
112
}
113
114
$this->track = true;
115
116
$this->publicUnTypedProperty = 'publicUnTypedProperty';
117
$this->publicUnTypedPropertyWithoutDefaultValue = NULL;
118
$this->publicBoolProperty = true;
119
$this->publicNullableBoolProperty = true;
120
$this->publicIntProperty = 123;
121
$this->publicNullableIntProperty = 123;
122
$this->publicFloatProperty = 123.456;
123
$this->publicNullableFloatProperty = 123.456;
124
$this->publicStringProperty = 'publicStringProperty';
125
$this->publicNullableStringProperty = 'publicStringProperty';
126
$this->publicArrayProperty = array (
127
  0 => 'publicArrayProperty',
128
);
129
$this->publicNullableArrayProperty = array (
130
  0 => 'publicArrayProperty',
131
);
132
$this->publicIterableProperty = array (
133
  0 => 'publicIterableProperty',
134
);
135
$this->publicNullableIterableProperty = array (
136
  0 => 'publicIterableProperty',
137
);
138
$this->protectedUnTypedProperty = 'protectedUnTypedProperty';
139
$this->protectedUnTypedPropertyWithoutDefaultValue = NULL;
140
$this->protectedBoolProperty = true;
141
$this->protectedNullableBoolProperty = true;
142
$this->protectedIntProperty = 123;
143
$this->protectedNullableIntProperty = 123;
144
$this->protectedFloatProperty = 123.456;
145
$this->protectedNullableFloatProperty = 123.456;
146
$this->protectedStringProperty = 'protectedStringProperty';
147
$this->protectedNullableStringProperty = 'protectedStringProperty';
148
$this->protectedArrayProperty = array (
149
  0 => 'protectedArrayProperty',
150
);
151
$this->protectedNullableArrayProperty = array (
152
  0 => 'protectedArrayProperty',
153
);
154
$this->protectedIterableProperty = array (
155
  0 => 'protectedIterableProperty',
156
);
157
$this->protectedNullableIterableProperty = array (
158
  0 => 'protectedIterableProperty',
159
);
160
static $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties;
161
162
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(static function ($instance) {
163
    $instance->privateUnTypedProperty = 'privateUnTypedProperty';
164
    $instance->privateUnTypedPropertyWithoutDefaultValue = NULL;
165
    $instance->privateBoolProperty = true;
166
    $instance->privateNullableBoolProperty = true;
167
    $instance->privateIntProperty = 123;
168
    $instance->privateNullableIntProperty = 123;
169
    $instance->privateFloatProperty = 123.456;
170
    $instance->privateNullableFloatProperty = 123.456;
171
    $instance->privateStringProperty = 'privateStringProperty';
172
    $instance->privateNullableStringProperty = 'privateStringProperty';
173
    $instance->privateArrayProperty = array (
174
  0 => 'privateArrayProperty',
175
);
176
    $instance->privateNullableArrayProperty = array (
177
  0 => 'privateArrayProperty',
178
);
179
    $instance->privateIterableProperty = array (
180
  0 => 'privateIterableProperty',
181
);
182
    $instance->privateNullableIterableProperty = array (
183
  0 => 'privateIterableProperty',
184
);
185
}, null, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties');
186
187
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties($this);
188
189
190
191
192
$properties = [
193
    'publicUnTypedProperty' => & $this->publicUnTypedProperty,
194
    'publicUnTypedPropertyWithoutDefaultValue' => & $this->publicUnTypedPropertyWithoutDefaultValue,
195
    'publicBoolProperty' => & $this->publicBoolProperty,
196
    'publicNullableBoolProperty' => & $this->publicNullableBoolProperty,
197
    'publicIntProperty' => & $this->publicIntProperty,
198
    'publicNullableIntProperty' => & $this->publicNullableIntProperty,
199
    'publicFloatProperty' => & $this->publicFloatProperty,
200
    'publicNullableFloatProperty' => & $this->publicNullableFloatProperty,
201
    'publicStringProperty' => & $this->publicStringProperty,
202
    'publicNullableStringProperty' => & $this->publicNullableStringProperty,
203
    'publicArrayProperty' => & $this->publicArrayProperty,
204
    'publicNullableArrayProperty' => & $this->publicNullableArrayProperty,
205
    'publicIterableProperty' => & $this->publicIterableProperty,
206
    'publicNullableIterableProperty' => & $this->publicNullableIterableProperty,
207
    '' . "\0" . '*' . "\0" . 'protectedUnTypedProperty' => & $this->protectedUnTypedProperty,
208
    '' . "\0" . '*' . "\0" . 'protectedUnTypedPropertyWithoutDefaultValue' => & $this->protectedUnTypedPropertyWithoutDefaultValue,
209
    '' . "\0" . '*' . "\0" . 'protectedBoolProperty' => & $this->protectedBoolProperty,
210
    '' . "\0" . '*' . "\0" . 'protectedNullableBoolProperty' => & $this->protectedNullableBoolProperty,
211
    '' . "\0" . '*' . "\0" . 'protectedIntProperty' => & $this->protectedIntProperty,
212
    '' . "\0" . '*' . "\0" . 'protectedNullableIntProperty' => & $this->protectedNullableIntProperty,
213
    '' . "\0" . '*' . "\0" . 'protectedFloatProperty' => & $this->protectedFloatProperty,
214
    '' . "\0" . '*' . "\0" . 'protectedNullableFloatProperty' => & $this->protectedNullableFloatProperty,
215
    '' . "\0" . '*' . "\0" . 'protectedStringProperty' => & $this->protectedStringProperty,
216
    '' . "\0" . '*' . "\0" . 'protectedNullableStringProperty' => & $this->protectedNullableStringProperty,
217
    '' . "\0" . '*' . "\0" . 'protectedArrayProperty' => & $this->protectedArrayProperty,
218
    '' . "\0" . '*' . "\0" . 'protectedNullableArrayProperty' => & $this->protectedNullableArrayProperty,
219
    '' . "\0" . '*' . "\0" . 'protectedIterableProperty' => & $this->protectedIterableProperty,
220
    '' . "\0" . '*' . "\0" . 'protectedNullableIterableProperty' => & $this->protectedNullableIterableProperty,
221
];
222
223
static $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties;
224
225
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance, array & $properties) {
226
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedProperty'] = & $instance->privateUnTypedProperty;
227
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedPropertyWithoutDefaultValue'] = & $instance->privateUnTypedPropertyWithoutDefaultValue;
228
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateBoolProperty'] = & $instance->privateBoolProperty;
229
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableBoolProperty'] = & $instance->privateNullableBoolProperty;
230
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIntProperty'] = & $instance->privateIntProperty;
231
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIntProperty'] = & $instance->privateNullableIntProperty;
232
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateFloatProperty'] = & $instance->privateFloatProperty;
233
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableFloatProperty'] = & $instance->privateNullableFloatProperty;
234
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateStringProperty'] = & $instance->privateStringProperty;
235
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableStringProperty'] = & $instance->privateNullableStringProperty;
236
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateArrayProperty'] = & $instance->privateArrayProperty;
237
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableArrayProperty'] = & $instance->privateNullableArrayProperty;
238
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIterableProperty'] = & $instance->privateIterableProperty;
239
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIterableProperty'] = & $instance->privateNullableIterableProperty;
240
}, $this, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties');
241
242
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties($this, $properties);
243
244
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties);
245
$this->track = false;
246
247
return $result;
248
PHP;
249
250
        self::assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<ProxyManagerTest\...or\CallInitializerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
251
            $expectedCode,
252
            $callInitializer->getBody()
253
        );
254
    }
255
}
256