Skip to content

Commit b12c0bc

Browse files
committed
Fix for unnamed class string
1 parent 79cc971 commit b12c0bc

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/Type/FactoriesReturnTypeHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function check(Type $type, string $function): Type
6363
return $traverse($type);
6464
}
6565

66+
if ($type->isClassStringType()->yes()) {
67+
return $type->getClassStringObjectType();
68+
}
69+
6670
$constantStrings = $type->getConstantStrings();
6771

6872
if ($constantStrings === []) {

tests/Fixtures/Type/config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14+
namespace CodeIgniter\PHPStan\Tests\Fixtures\Type;
15+
1416
use Config\App;
17+
use stdClass;
1518

1619
use function PHPStan\Testing\assertType;
1720

@@ -22,3 +25,10 @@
2225
assertType('Config\App', config('App'));
2326
assertType('Config\App', config(App::class));
2427
assertType('stdClass|null', config($class));
28+
29+
function bar(string $name): void
30+
{
31+
if (class_exists($name)) {
32+
assertType('object', config($name));
33+
}
34+
}

tests/Rules/Functions/FactoriesFunctionArgumentTypeRuleTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ public function testRule(): void
4343
], [
4444
[
4545
'Parameter #1 $name of function config expects a valid class string, \'bar\' given.',
46-
20,
46+
23,
4747
],
4848
[
4949
'Parameter #1 $name of function config expects a valid class string, \'Foo\\\\Bar\' given.',
50-
21,
50+
24,
5151
],
5252
[
5353
'Argument #1 $name (\'Foo\'|\'stdClass\') passed to function config does not extend CodeIgniter\\\\Config\\\\BaseConfig.',
54-
24,
54+
27,
55+
],
56+
[
57+
'Argument #1 $name (class-string) passed to function config does not extend CodeIgniter\\\\Config\\\\BaseConfig.',
58+
32,
5559
],
5660
[
5761
'Parameter #1 $name of function model expects a valid class string, \'foo\' given.',

tests/Type/FactoriesReturnTypeHelperTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
use Config\App;
1818
use PHPStan\Reflection\ReflectionProvider;
1919
use PHPStan\Testing\PHPStanTestCase;
20+
use PHPStan\Type\ClassStringType;
2021
use PHPStan\Type\Constant\ConstantBooleanType;
2122
use PHPStan\Type\Constant\ConstantStringType;
2223
use PHPStan\Type\NullType;
2324
use PHPStan\Type\ObjectType;
25+
use PHPStan\Type\ObjectWithoutClassType;
2426
use PHPStan\Type\Type;
2527
use PHPStan\Type\UnionType;
2628
use PHPStan\Type\VerbosityLevel;
@@ -42,7 +44,9 @@ public static function provideCheckOfReturnTypeCases(): iterable
4244

4345
yield 'non class string returns null type' => [new NullType(), new ConstantStringType('Bar')];
4446

45-
yield 'class string' => [new ObjectType(App::class), new ConstantStringType(App::class, true)];
47+
yield 'constant class string' => [new ObjectType(App::class), new ConstantStringType(App::class, true)];
48+
49+
yield 'class string' => [new ObjectWithoutClassType(), new ClassStringType()];
4650

4751
yield 'union type' => [new UnionType([new NullType(), new ObjectType(App::class)]), new UnionType([new ConstantStringType('Bar'), new ConstantStringType(App::class, true)])];
4852
}

0 commit comments

Comments
 (0)