1515
1616use CodeIgniter \Config \BaseConfig ;
1717use CodeIgniter \Model ;
18- use CodeIgniter \PHPStan \Type \FactoriesReturnTypeHelper ;
18+ use CodeIgniter \PHPStan \Helpers \FactoriesReturnTypeHelper ;
1919use PhpParser \Node ;
2020use PHPStan \Analyser \Scope ;
2121use PHPStan \Reflection \ParametersAcceptorSelector ;
@@ -41,7 +41,7 @@ final class FactoriesFunctionArgumentTypeRule implements Rule
4141 /**
4242 * @var array<string, bool>
4343 */
44- private array $ argumentTypeCheck = [] ;
44+ private array $ argumentTypeCheck ;
4545
4646 public function __construct (
4747 private readonly ReflectionProvider $ reflectionProvider ,
@@ -85,47 +85,48 @@ public function processNode(Node $node, Scope $scope): array
8585 return []; // caught elsewhere
8686 }
8787
88- $ returnType = $ this ->factoriesReturnTypeHelper ->check ($ nameType , $ function );
89-
88+ $ returnType = $ this ->factoriesReturnTypeHelper ->check ($ nameType , $ function );
9089 $ firstParameter = ParametersAcceptorSelector::selectFromArgs (
9190 $ scope ,
92- $ node -> getArgs () ,
91+ $ args ,
9392 $ this ->reflectionProvider ->getFunction ($ nameNode , $ scope )->getVariants (),
9493 )->getParameters ()[0 ];
9594
9695 if ($ returnType ->isNull ()->yes ()) {
97- $ addTip = static function (RuleErrorBuilder $ ruleErrorBuilder ) use ($ nameType , $ function ): RuleErrorBuilder {
98- foreach ($ nameType ->getConstantStrings () as $ constantStringType ) {
99- $ ruleErrorBuilder ->addTip (sprintf (
100- '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%%</>. ' ,
101- $ constantStringType ->describe (VerbosityLevel::precise ()),
102- ucfirst ($ function ),
103- ));
104- }
105-
106- return $ ruleErrorBuilder ;
107- };
108-
109- return [$ addTip (RuleErrorBuilder::message (sprintf (
96+ $ ruleErrorBuilder = RuleErrorBuilder::message (sprintf (
11097 'Parameter #1 $%s of function %s expects a valid class string, %s given. ' ,
11198 $ firstParameter ->getName (),
11299 $ function ,
113100 $ nameType ->describe (VerbosityLevel::precise ()),
114- )))->identifier (sprintf ('codeigniter.%sArgumentType ' , $ function ))->build ()];
101+ ))->identifier (sprintf ('codeigniter.%sArgumentType ' , $ function ));
102+
103+ foreach ($ nameType ->getConstantStrings () as $ constantStringType ) {
104+ $ ruleErrorBuilder ->addTip (sprintf (
105+ '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%%</>. ' ,
106+ $ constantStringType ->describe (VerbosityLevel::precise ()),
107+ ucfirst ($ function ),
108+ ));
109+ }
110+
111+ return [$ ruleErrorBuilder ->build ()];
115112 }
116113
117114 if (! (new ObjectType ($ this ->instanceofMap [$ function ]))->isSuperTypeOf ($ returnType )->yes ()) {
118115 if (! $ this ->argumentTypeCheck [$ function ]) {
119116 return [];
120117 }
121118
122- return [RuleErrorBuilder::message (sprintf (
123- 'Argument #1 $%s (%s) passed to function %s does not extend %s. ' ,
124- $ firstParameter ->getName (),
125- $ nameType ->describe (VerbosityLevel::precise ()),
126- $ function ,
127- addcslashes ($ this ->instanceofMap [$ function ], '\\' ),
128- ))->identifier (sprintf ('codeigniter.%sArgumentInstanceof ' , $ function ))->build ()];
119+ return [
120+ RuleErrorBuilder::message (sprintf (
121+ 'Argument #1 $%s (%s) passed to function %s does not extend %s. ' ,
122+ $ firstParameter ->getName (),
123+ $ nameType ->describe (VerbosityLevel::precise ()),
124+ $ function ,
125+ $ this ->instanceofMap [$ function ],
126+ ))
127+ ->identifier (sprintf ('codeigniter.%sArgumentInstanceof ' , $ function ))
128+ ->build (),
129+ ];
129130 }
130131
131132 return [];
0 commit comments