Files
michaelschiemer/src/Framework/Quality/PHPStan/Rules/UseFrameworkAbstractionsRule.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

65 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Quality\PHPStan\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
/**
* @implements Rule<New_>
*/
final class UseFrameworkAbstractionsRule implements Rule
{
/** @var array<string, string> */
private array $forbiddenClasses = [
'DateTime' => 'Use App\\Framework\\DateTime\\DateTime instead',
'DateTimeImmutable' => 'Use App\\Framework\\DateTime\\DateTime instead',
'PDO' => 'Use App\\Framework\\Database\\ConnectionInterface instead',
'mysqli' => 'Use App\\Framework\\Database\\ConnectionInterface instead',
'Redis' => 'Use App\\Framework\\Cache\\CacheDriver instead',
'GuzzleHttp\\Client' => 'Use App\\Framework\\HttpClient\\HttpClient instead',
'Predis\\Client' => 'Use App\\Framework\\Cache\\CacheDriver instead',
];
public function getNodeType(): string
{
return New_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (! $node instanceof New_) {
return [];
}
if (! $node->class instanceof Node\Name) {
return [];
}
$className = $node->class->toString();
// Skip if we're already in Framework layer (allow direct usage there)
$currentClass = $scope->getClassReflection()?->getName() ?? '';
if (str_starts_with($currentClass, 'App\\Framework\\')) {
return [];
}
if (! isset($this->forbiddenClasses[$className])) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Direct instantiation of %s is forbidden: %s',
$className,
$this->forbiddenClasses[$className]
))->tip('Use dependency injection to get the framework abstraction')->build(),
];
}
}