Skip to content

Commit 236ad64

Browse files
committed
Adjust and make space for tests for new behaviour of new
1 parent a6f295e commit 236ad64

22 files changed

+251
-127
lines changed

Diff for: tests/PHPStan/Analyser/data/array-destructuring.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
function () {
2+
function (\stdClass $obj) {
33
/** @var mixed $array */
44
$array = getMixed();
55
[$a, $b, [$c]] = $array;
@@ -51,7 +51,6 @@ function () {
5151

5252
[$newArray['newKey']] = [new stdClass(), new stdClass()];
5353

54-
$obj = new stdClass();
5554
[$obj[0]] = ['error', 'error-error'];
5655

5756
$constantAssocArray = [1, 'foo', 'key' => true, 'value' => '123'];

Diff for: tests/PHPStan/Analyser/data/nested-functions.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public function doFoo(): self
1212

1313
}
1414

15-
function () {
16-
$foo = new Foo();
15+
function (Foo $foo) {
1716
$foo->doFoo()
1817
->doFoo()
1918
->doFoo()

Diff for: tests/PHPStan/Analyser/nsrt/bug-6462.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ public function getThis(): self
3737
}
3838
}
3939

40-
$base = new Base();
41-
$child = new Child();
42-
$fixedChild = new FixedChild();
40+
function (Base $base, Child $child, FixedChild $fixedChild): void {
41+
assertType('Bug6462\Base', $base->getThis());
42+
assertType('Bug6462\Child', $child->getThis());
4343

44-
assertType('Bug6462\Base', $base->getThis());
45-
assertType('Bug6462\Child', $child->getThis());
46-
47-
if ($base instanceof \Traversable) {
48-
assertType('Bug6462\Base&Traversable', $base->getThis());
49-
}
44+
if ($base instanceof \Traversable) {
45+
assertType('Bug6462\Base&Traversable', $base->getThis());
46+
}
5047

51-
if ($child instanceof \Traversable) {
52-
assertType('Bug6462\Child&Traversable', $child->getThis());
53-
}
48+
if ($child instanceof \Traversable) {
49+
assertType('Bug6462\Child&Traversable', $child->getThis());
50+
}
5451

55-
if ($fixedChild instanceof \Traversable) {
56-
assertType('Bug6462\FixedChild&Traversable', $fixedChild->getThis());
57-
}
52+
if ($fixedChild instanceof \Traversable) {
53+
assertType('Bug6462\FixedChild&Traversable', $fixedChild->getThis());
54+
}
55+
};

Diff for: tests/PHPStan/Analyser/nsrt/get-debug-type.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class D {}
1515
* @param int|string $intOrString
1616
* @param array|A $arrayOrObject
1717
*/
18-
function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrString, $arrayOrObject) {
18+
function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrString, $arrayOrObject, \stdClass $std) {
1919
$null = null;
2020
$resource = fopen('php://memory', 'r');
2121
$o = new \stdClass();
@@ -34,6 +34,7 @@ function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrStr
3434
assertType("'string'", get_debug_type($s));
3535
assertType("'array'", get_debug_type($a));
3636
assertType("string", get_debug_type($o));
37+
assertType("string", get_debug_type($std));
3738
assertType("'GetDebugType\\\\A'", get_debug_type($A));
3839
assertType("string", get_debug_type($r));
3940
assertType("'bool'|string", get_debug_type($resource));

Diff for: tests/PHPStan/Analyser/nsrt/instanceof.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ abstract class BarParent
1919
class Foo extends BarParent
2020
{
2121

22-
public function someMethod(Expr $foo)
22+
public function someMethod(Expr $foo, Foo $intersected)
2323
{
2424
$bar = $foo;
2525
$baz = doFoo();
26-
$intersected = new Foo();
2726
$parent = doFoo();
2827

2928
if ($baz instanceof Foo) {

Diff for: tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+47-39
Original file line numberDiff line numberDiff line change
@@ -96,169 +96,177 @@ public function testImpossibleCheckTypeFunctionCall(): void
9696
'Call to function method_exists() with CheckTypeFunctionCall\Foo and \'doFoo\' will always evaluate to true.',
9797
179,
9898
],
99+
[
100+
'Call to function method_exists() with CheckTypeFunctionCall\Foo and \'doFoo\' will always evaluate to true.',
101+
189,
102+
],
99103
[
100104
'Call to function method_exists() with $this(CheckTypeFunctionCall\FinalClassWithMethodExists) and \'doFoo\' will always evaluate to true.',
101-
191,
105+
201,
102106
],
103107
[
104108
'Call to function method_exists() with $this(CheckTypeFunctionCall\FinalClassWithMethodExists) and \'doBar\' will always evaluate to false.',
105-
194,
109+
204,
106110
],
107111
[
108112
'Call to function property_exists() with $this(CheckTypeFunctionCall\FinalClassWithPropertyExists) and \'fooProperty\' will always evaluate to true.',
109-
210,
113+
220,
110114
],
111115
[
112116
'Call to function in_array() with arguments int, array{\'foo\', \'bar\'} and true will always evaluate to false.',
113-
236,
117+
246,
114118
],
115119
[
116120
'Call to function in_array() with arguments \'bar\'|\'foo\', array{\'baz\', \'lorem\'} and true will always evaluate to false.',
117-
245,
121+
255,
118122
],
119123
[
120124
'Call to function in_array() with arguments \'foo\', array{\'foo\'} and true will always evaluate to true.',
121-
253,
125+
263,
122126
],
123127
[
124128
'Call to function in_array() with arguments \'foo\', array{\'foo\', \'bar\'} and true will always evaluate to true.',
125-
257,
129+
267,
126130
],
127131
[
128132
'Call to function in_array() with arguments \'bar\', array{}|array{\'foo\'} and true will always evaluate to false.',
129-
321,
133+
331,
130134
],
131135
[
132136
'Call to function in_array() with arguments \'baz\', array{0: \'bar\', 1?: \'foo\'} and true will always evaluate to false.',
133-
337,
137+
347,
134138
],
135139
[
136140
'Call to function in_array() with arguments \'foo\', array{} and true will always evaluate to false.',
137-
344,
141+
354,
138142
],
139143
[
140144
'Call to function array_key_exists() with \'a\' and array{a: 1, b?: 2} will always evaluate to true.',
141-
361,
145+
371,
142146
],
143147
[
144148
'Call to function array_key_exists() with \'c\' and array{a: 1, b?: 2} will always evaluate to false.',
145-
367,
149+
377,
146150
],
147151
[
148152
'Call to function is_string() with mixed will always evaluate to false.',
149-
561,
153+
571,
150154
],
151155
[
152156
'Call to function is_callable() with mixed will always evaluate to false.',
153-
572,
157+
582,
154158
],
155159
[
156160
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExists\' and \'testWithStringFirst…\' will always evaluate to true.',
157-
586,
161+
596,
158162
],
159163
[
160164
'Call to function method_exists() with \'UndefinedClass\' and string will always evaluate to false.',
161-
595,
165+
605,
162166
],
163167
[
164168
'Call to function method_exists() with \'UndefinedClass\' and \'test\' will always evaluate to false.',
165-
598,
169+
608,
170+
],
171+
[
172+
'Call to function method_exists() with CheckTypeFunctionCall\MethodExists and \'testWithNewObjectIn…\' will always evaluate to true.',
173+
620,
166174
],
167175
[
168176
'Call to function method_exists() with CheckTypeFunctionCall\MethodExists and \'testWithNewObjectIn…\' will always evaluate to true.',
169-
610,
177+
635,
170178
],
171179
[
172180
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'method\' will always evaluate to true.',
173-
625,
181+
650,
174182
],
175183
[
176184
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'someAnother\' will always evaluate to true.',
177-
628,
185+
653,
178186
],
179187
[
180188
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'unknown\' will always evaluate to false.',
181-
631,
189+
656,
182190
],
183191
[
184192
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'method\' will always evaluate to true.',
185-
634,
193+
659,
186194
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
187195
],
188196
[
189197
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'someAnother\' will always evaluate to true.',
190-
637,
198+
662,
191199
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
192200
],
193201
[
194202
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'unknown\' will always evaluate to false.',
195-
640,
203+
665,
196204
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
197205
],
198206
[
199207
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'method\' will always evaluate to true.',
200-
643,
208+
668,
201209
],
202210
[
203211
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'someAnother\' will always evaluate to true.',
204-
646,
212+
671,
205213
],
206214
[
207215
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'unknown\' will always evaluate to false.',
208-
649,
216+
674,
209217
],
210218
[
211219
'Call to function is_string() with string will always evaluate to true.',
212-
678,
220+
703,
213221
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
214222
],
215223
[
216224
'Call to function assert() with true will always evaluate to true.',
217-
693,
225+
718,
218226
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
219227
],
220228
[
221229
'Call to function is_numeric() with \'123\' will always evaluate to true.',
222-
693,
230+
718,
223231
],
224232
[
225233
'Call to function assert() with false will always evaluate to false.',
226-
694,
234+
719,
227235
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
228236
],
229237
[
230238
'Call to function is_numeric() with \'blabla\' will always evaluate to false.',
231-
694,
239+
719,
232240
],
233241
[
234242
'Call to function assert() with true will always evaluate to true.',
235-
701,
243+
726,
236244
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
237245
],
238246
[
239247
'Call to function is_numeric() with 123|float will always evaluate to true.',
240-
701,
248+
726,
241249
],
242250
[
243251
'Call to function property_exists() with CheckTypeFunctionCall\Bug2221 and \'foo\' will always evaluate to true.',
244-
784,
252+
809,
245253
],
246254
[
247255
'Call to function property_exists() with CheckTypeFunctionCall\Bug2221 and \'foo\' will always evaluate to true.',
248-
788,
256+
813,
249257
],
250258
[
251259
'Call to function testIsInt() with int will always evaluate to true.',
252-
875,
260+
900,
253261
],
254262
[
255263
'Call to function is_int() with int will always evaluate to true.',
256-
889,
264+
914,
257265
'Remove remaining cases below this one and this error will disappear too.',
258266
],
259267
[
260268
'Call to function in_array() with arguments 1, array<string> and true will always evaluate to false.',
261-
927,
269+
952,
262270
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
263271
],
264272
],

Diff for: tests/PHPStan/Rules/Comparison/data/check-type-function-call.php

+25
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ public function doFoo()
181181
}
182182
}
183183

184+
public function doBar(Foo $foo)
185+
{
186+
if (method_exists($foo, 'test')) {
187+
188+
}
189+
if (method_exists($foo, 'doFoo')) {
190+
191+
}
192+
}
193+
184194
}
185195

186196
final class FinalClassWithMethodExists
@@ -616,6 +626,21 @@ public function testWithNewObjectInFirstArgument(): void
616626
if (method_exists((new MethodExists()), $string)) {
617627
}
618628
}
629+
630+
public function testWithTypehintedObject(MethodExists $methodExists): void
631+
{
632+
/** @var string $string */
633+
$string = doFoo();
634+
635+
if (method_exists($methodExists, 'testWithNewObjectInFirstArgument')) {
636+
}
637+
638+
if (method_exists($methodExists, 'undefinedMethod')) {
639+
}
640+
641+
if (method_exists($methodExists, $string)) {
642+
}
643+
}
619644
}
620645

621646
trait MethodExistsTrait

Diff for: tests/PHPStan/Rules/DeadCode/CallToMethodStatementWithoutImpurePointsRuleTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ public function testRule(): void
4848
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.',
4949
41,
5050
],
51+
[
52+
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.',
53+
62,
54+
],
5155
[
5256
'Call to method CallToMethodWithoutImpurePoints\AbstractFoo::myFunc() on a separate line has no effect.',
53-
119,
57+
140,
5458
],
5559
[
5660
'Call to method CallToMethodWithoutImpurePoints\CallsPrivateMethodWithoutImpurePoints::doBar() on a separate line has no effect.',
57-
127,
61+
148,
5862
],
5963
]);
6064
}

Diff for: tests/PHPStan/Rules/DeadCode/data/call-to-method-without-impure-points.php

+21
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ function (): void {
4141
$subSubY->myFinalBaseFunc();
4242
};
4343

44+
function (y $xy, finalX $finalX): void {
45+
$xy = new y();
46+
if (rand(0,1)) {
47+
$xy = $finalX;
48+
}
49+
$xy->myFunc();
50+
};
51+
52+
function (Y $xy, finalX $finalX): void {
53+
// case-insensitive class name
54+
if (rand(0,1)) {
55+
$xy = $finalX;
56+
}
57+
$xy->myFunc();
58+
};
59+
60+
function (subY $subY): void {
61+
$subY->myFunc();
62+
$subY->myFinalBaseFunc();
63+
};
64+
4465
class y
4566
{
4667
function myFunc()

0 commit comments

Comments
 (0)