Skip to content

Commit 1ff7c75

Browse files
committed
Refactored ModelBehaviorsExtension
1 parent 4333868 commit 1ff7c75

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

phpinsights.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@
8282
*/
8383

8484
'requirements' => [
85-
'min-quality' => 75,
86-
'min-complexity' => 75,
87-
'min-architecture' => 75,
88-
'min-style' => 75,
85+
'min-quality' => 90,
86+
'min-complexity' => 90,
87+
'min-architecture' => 90,
88+
'min-style' => 90,
8989
'disable-security-check' => false,
9090
],
9191

src/ModelBehaviorsExtension.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ public function __construct(ReflectionProvider $reflectionProvider, array $behav
4444
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
4545
{
4646
return $classReflection->is('Model')
47-
&& count(
48-
array_filter(
49-
$this->getBehaviorMethods(),
50-
static function (MethodReflection $methodReflection) use ($methodName) {
51-
return $methodReflection->getName() === $methodName;
52-
}
53-
)
54-
) > 0;
47+
&& in_array($methodName, array_map([$this, 'getMethodReflectionName'], $this->getBehaviorMethods()));
5548
}
5649

5750
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
@@ -104,29 +97,48 @@ private function getBehaviorMethods(): array
10497
*/
10598
private function getModelBehaviorMethods(ClassReflection $classReflection): array
10699
{
107-
$methodNames = array_map(static function (ReflectionMethod $methodReflection) {
108-
return $methodReflection->getName();
109-
}, $classReflection->getNativeReflection()->getMethods());
100+
$methodNames = array_map(
101+
[$this, 'getMethodReflectionName'],
102+
$classReflection->getNativeReflection()->getMethods()
103+
);
110104
/** @var array<ExtendedMethodReflection> $methodReflections */
111105
$methodReflections = array_filter(
112106
array_map([$classReflection, 'getNativeMethod'], $methodNames),
113-
static function (ExtendedMethodReflection $methodReflection) {
114-
return $methodReflection->isPublic()
115-
&& ! $methodReflection->isStatic()
116-
&& array_filter(
117-
$methodReflection->getVariants(),
118-
static function (ParametersAcceptor $parametersAcceptor) {
119-
$parameters = $parametersAcceptor->getParameters();
120-
/** @var ParameterReflection|null $firstParameter */
121-
$firstParameter = array_shift($parameters);
122-
return $firstParameter
123-
&& ! $firstParameter->getType()->isSuperTypeOf(new ObjectType('Model'))->no();
124-
}
125-
);
126-
}
107+
[$this, 'filterBehaviorMethods']
127108
);
128-
return array_map(static function (ExtendedMethodReflection $methodReflection) {
129-
return new ModelBehaviorMethodWrapper($methodReflection);
130-
}, $methodReflections);
109+
return array_map([$this, 'wrapBehaviorMethod'], $methodReflections);
110+
}
111+
112+
private function filterBehaviorMethods(ExtendedMethodReflection $methodReflection): bool
113+
{
114+
return $methodReflection->isPublic()
115+
&& ! $methodReflection->isStatic()
116+
&& array_filter(
117+
$methodReflection->getVariants(),
118+
[$this, 'filterBehaviorMethodVariants']
119+
);
120+
}
121+
122+
private function filterBehaviorMethodVariants(ParametersAcceptor $parametersAcceptor): bool
123+
{
124+
$parameters = $parametersAcceptor->getParameters();
125+
/** @var ParameterReflection|null $firstParameter */
126+
$firstParameter = array_shift($parameters);
127+
return $firstParameter
128+
&& ! $firstParameter->getType()->isSuperTypeOf(new ObjectType('Model'))->no();
129+
}
130+
131+
private function wrapBehaviorMethod(MethodReflection $methodReflection): MethodReflection
132+
{
133+
return new ModelBehaviorMethodWrapper($methodReflection);
134+
}
135+
136+
/**
137+
* @param MethodReflection|ReflectionMethod $methodReflection
138+
* @return string
139+
*/
140+
private function getMethodReflectionName($methodReflection): string
141+
{
142+
return $methodReflection->getName();
131143
}
132144
}

0 commit comments

Comments
 (0)