Skip to content

Commit 0c83a4f

Browse files
committed
Fix weird "If string is a valid class string" tip
1 parent 631ce99 commit 0c83a4f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/Rules/Functions/FactoriesFunctionArgumentTypeRule.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,24 @@ public function processNode(Node $node, Scope $scope): array
8484
)->getParameters()[0];
8585

8686
if ($returnType->isNull()->yes()) {
87-
return [RuleErrorBuilder::message(sprintf(
87+
$addTip = static function (RuleErrorBuilder $ruleErrorBuilder) use ($nameType, $function): RuleErrorBuilder {
88+
if ($nameType->getConstantStrings() === []) {
89+
return $ruleErrorBuilder;
90+
}
91+
92+
return $ruleErrorBuilder->tip(sprintf(
93+
'If %s is a valid class string, you can add its possible namespace(s) in <fg=cyan>codeigniter.additional%sNamespaces</> in your <fg=yellow>%%configurationFile%%</>.',
94+
$nameType->describe(VerbosityLevel::precise()),
95+
ucfirst($function)
96+
));
97+
};
98+
99+
return [$addTip(RuleErrorBuilder::message(sprintf(
88100
'Parameter #1 $%s of function %s expects a valid class string, %s given.',
89101
$firstParameter->getName(),
90102
$function,
91103
$nameType->describe(VerbosityLevel::precise())
92-
))->tip(sprintf(
93-
'If %s is a valid class string, you can add its possible namespace(s) in <fg=cyan>codeigniter.additional%sNamespaces</> in your <fg=yellow>%%configurationFile%%</>.',
94-
$nameType->describe(VerbosityLevel::precise()),
95-
ucfirst($function)
96-
))->identifier(sprintf('codeigniter.%sArgumentType', $function))->build()];
104+
)))->identifier(sprintf('codeigniter.%sArgumentType', $function))->build()];
97105
}
98106

99107
if (! (new ObjectType($this->instanceofMap[$function]))->isSuperTypeOf($returnType)->yes()) {

tests/Fixtures/Type/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ function bar(string $name): void
3131
if (class_exists($name)) {
3232
assertType('object', config($name));
3333
}
34+
35+
assertType('null', config($name));
3436
}

tests/Rules/Functions/FactoriesFunctionArgumentTypeRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function testRule(): void
6262
'Argument #1 $name (class-string) passed to function config does not extend CodeIgniter\\\\Config\\\\BaseConfig.',
6363
32,
6464
],
65+
[
66+
'Parameter #1 $name of function config expects a valid class string, string given.',
67+
35,
68+
],
6569
[
6670
'Parameter #1 $name of function model expects a valid class string, \'foo\' given.',
6771
18,

0 commit comments

Comments
 (0)