if ($callingClass === TextMessageQueueProcessor::class) {
return [];
}
$type = $scope->getType($node->var);
foreach ($type->getReferencedClasses() as $targetClass) {
if ($targetClass === TextMessageSender::class) {
return [
RuleErrorBuilder::message(
sprintf(
"Can not call %s from %s",
$targetClass,
$callingClass
)
)->build()
];
}
}
return [];
```
There are no `else` statements.
If the calling class isn't of the required type, it returns immediately with no violations, and we continue, knowing the calling class must be what we need.
If the target class is one where the code shouldn't be called from, it returns immediately with theviolation.
Finally, if no violations were found within the referenced classes, it returns an empty array.
## Here's the thing
The code always returns an array of rule violations but does so as soon as possible at each point.
The code is clean and readable, and I can understand it, knowing once each condition is passed, I don't need to continue thinking about it.
Whilst there are some situations to use `else`, most of the time I've found that I can use an early return instead.