Skip to content

Commit ee4afde

Browse files
committed
Better handling of constant strings
1 parent 165226e commit ee4afde

File tree

3 files changed

+51
-55
lines changed

3 files changed

+51
-55
lines changed

src/Rules/Functions/FactoriesFunctionArgumentTypeRule.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,15 @@ public function processNode(Node $node, Scope $scope): array
8989
return $ruleErrorBuilder;
9090
}
9191

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-
));
92+
foreach ($nameType->getConstantStrings() as $constantStringType) {
93+
$ruleErrorBuilder->addTip(sprintf(
94+
'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%%</>.',
95+
$constantStringType->describe(VerbosityLevel::precise()),
96+
ucfirst($function)
97+
));
98+
}
99+
100+
return $ruleErrorBuilder;
97101
};
98102

99103
return [$addTip(RuleErrorBuilder::message(sprintf(

src/Type/FactoriesReturnTypeHelper.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,25 @@ public function check(Type $type, string $function): Type
6767
return $type->getClassStringObjectType();
6868
}
6969

70-
$constantStrings = $type->getConstantStrings();
71-
72-
if ($constantStrings === []) {
73-
return new NullType();
74-
}
75-
76-
$constantStringType = current($constantStrings);
77-
78-
if ($constantStringType->isClassStringType()->yes()) {
79-
return $constantStringType->getClassStringObjectType();
80-
}
70+
foreach ($type->getConstantStrings() as $constantStringType) {
71+
if ($constantStringType->isClassStringType()->yes()) {
72+
return $constantStringType->getClassStringObjectType();
73+
}
8174

82-
$constantString = $constantStringType->getValue();
75+
$constantString = $constantStringType->getValue();
8376

84-
$appName = $this->namespaceMap[$function] . $constantString;
77+
$appName = $this->namespaceMap[$function] . $constantString;
8578

86-
if ($this->reflectionProvider->hasClass($appName)) {
87-
return new ObjectType($appName);
88-
}
79+
if ($this->reflectionProvider->hasClass($appName)) {
80+
return new ObjectType($appName);
81+
}
8982

90-
foreach ($this->additionalNamespacesMap[$function] as $additionalNamespace) {
91-
$moduleClassName = $additionalNamespace . $constantString;
83+
foreach ($this->additionalNamespacesMap[$function] as $additionalNamespace) {
84+
$moduleClassName = $additionalNamespace . $constantString;
9285

93-
if ($this->reflectionProvider->hasClass($moduleClassName)) {
94-
return new ObjectType($moduleClassName);
86+
if ($this->reflectionProvider->hasClass($moduleClassName)) {
87+
return new ObjectType($moduleClassName);
88+
}
9589
}
9690
}
9791

src/Type/ServicesReturnTypeHelper.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ final class ServicesReturnTypeHelper
3131
/**
3232
* @var array<int, string>
3333
*/
34-
private const IMPOSSIBLE_SERVICE_METHOD_NAMES = [
35-
'__callStatic',
36-
'buildServicesCache',
37-
'createRequest',
38-
'discoverServices',
39-
'getSharedInstance',
40-
'injectMock',
34+
public const IMPOSSIBLE_SERVICE_METHOD_NAMES = [
35+
'__callstatic',
36+
'buildservicescache',
37+
'createrequest',
38+
'discoverservices',
39+
'getsharedinstance',
40+
'injectmock',
4141
'reset',
42-
'resetSingle',
43-
'serviceExists',
42+
'resetsingle',
43+
'serviceexists',
4444
];
4545

4646
/**
@@ -84,37 +84,35 @@ public function check(Type $type, Scope $scope): Type
8484
return $traverse($type);
8585
}
8686

87-
$constantStrings = $type->getConstantStrings();
87+
foreach ($type->getConstantStrings() as $constantStringType) {
88+
$constantString = $constantStringType->getValue();
8889

89-
if ($constantStrings === []) {
90-
return new NullType();
91-
}
90+
foreach (self::IMPOSSIBLE_SERVICE_METHOD_NAMES as $impossibleServiceMethodName) {
91+
if (strtolower($constantString) === $impossibleServiceMethodName) {
92+
return new NullType();
93+
}
94+
}
9295

93-
$constantString = current($constantStrings)->getValue();
96+
$methodReflection = null;
9497

95-
foreach (self::IMPOSSIBLE_SERVICE_METHOD_NAMES as $impossibleServiceMethodName) {
96-
if (strtolower($constantString) === strtolower($impossibleServiceMethodName)) {
97-
return new NullType();
98+
foreach (self::$servicesReflection as $servicesReflection) {
99+
if ($servicesReflection->hasMethod($constantString)) {
100+
$methodReflection = $servicesReflection->getMethod($constantString, $scope);
101+
}
98102
}
99-
}
100-
101-
$methodReflection = null;
102103

103-
foreach (self::$servicesReflection as $servicesReflection) {
104-
if ($servicesReflection->hasMethod($constantString)) {
105-
$methodReflection = $servicesReflection->getMethod($constantString, $scope);
104+
if ($methodReflection === null) {
105+
return new NullType();
106106
}
107-
}
108107

109-
if ($methodReflection === null) {
110-
return new NullType();
111-
}
108+
if (! $methodReflection->isStatic() || ! $methodReflection->isPublic()) {
109+
return new NullType();
110+
}
112111

113-
if (! $methodReflection->isStatic() || ! $methodReflection->isPublic()) {
114-
return new NullType();
112+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
115113
}
116114

117-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
115+
return new NullType();
118116
});
119117
}
120118
}

0 commit comments

Comments
 (0)