|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace mglaman\PHPStanDrupal\Rules\Drupal; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormStateInterface; |
| 6 | +use Drupal\Core\Hook\Attribute\Hook; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use PHPStan\Type\ObjectType; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | +use function class_exists; |
| 15 | +use function count; |
| 16 | +use function preg_match; |
| 17 | + |
| 18 | +/** |
| 19 | + * Rule to validate form alter hook implementations using the Hook attribute. |
| 20 | + * |
| 21 | + * Validates that OOP hooks implementing form_alter, form_FORM_ID_alter, and |
| 22 | + * form_BASE_FORM_ID_alter have the correct method signatures. |
| 23 | + * |
| 24 | + * @implements Rule<ClassMethod> |
| 25 | + */ |
| 26 | +class HookFormAlterRule implements Rule |
| 27 | +{ |
| 28 | + |
| 29 | + /** |
| 30 | + * Pattern matching form alter hook names. |
| 31 | + */ |
| 32 | + private const FORM_ALTER_PATTERNS = [ |
| 33 | + '/^form_alter$/', |
| 34 | + '/^form_[a-zA-Z0-9_]+_alter$/', |
| 35 | + ]; |
| 36 | + |
| 37 | + |
| 38 | + public function getNodeType(): string |
| 39 | + { |
| 40 | + return ClassMethod::class; |
| 41 | + } |
| 42 | + |
| 43 | + public function processNode(Node $node, Scope $scope): array |
| 44 | + { |
| 45 | + // Skip if Hook attribute class doesn't exist (older Drupal versions) |
| 46 | + if (!class_exists(Hook::class)) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + return $this->processMethod($node, $scope); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Process a method with Hook attribute. |
| 55 | + * |
| 56 | + * @return list<\PHPStan\Rules\IdentifierRuleError> |
| 57 | + */ |
| 58 | + private function processMethod(ClassMethod $method, Scope $scope): array |
| 59 | + { |
| 60 | + if (!$scope->isInClass()) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + $classReflection = $scope->getClassReflection(); |
| 65 | + $nativeClass = $classReflection->getNativeReflection(); |
| 66 | + |
| 67 | + if (!$nativeClass->hasMethod($method->name->toString())) { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + $nativeMethod = $nativeClass->getMethod($method->name->toString()); |
| 72 | + $hookAttributes = $nativeMethod->getAttributes(Hook::class); |
| 73 | + |
| 74 | + if (count($hookAttributes) === 0) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + $errors = []; |
| 79 | + foreach ($hookAttributes as $hookAttribute) { |
| 80 | + /** @var Hook $hookInstance */ |
| 81 | + $hookInstance = $hookAttribute->newInstance(); |
| 82 | + if ($this->isFormAlterHook($hookInstance->hook)) { |
| 83 | + $methodErrors = $this->validateMethodSignature($method, $scope, $hookInstance->hook); |
| 84 | + $errors[] = $methodErrors; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return array_merge(...$errors); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Check if a hook name matches form alter patterns. |
| 94 | + */ |
| 95 | + private function isFormAlterHook(string $hookName): bool |
| 96 | + { |
| 97 | + foreach (self::FORM_ALTER_PATTERNS as $pattern) { |
| 98 | + if (preg_match($pattern, $hookName) === 1) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Validate method signature for method-targeted hooks. |
| 107 | + * |
| 108 | + * @return list<\PHPStan\Rules\IdentifierRuleError> |
| 109 | + */ |
| 110 | + private function validateMethodSignature(ClassMethod $method, Scope $scope, string $hookName): array |
| 111 | + { |
| 112 | + $expectedSignature = 'Expected signature: method(&$form, \Drupal\Core\Form\FormStateInterface $form_state[, $form_id])'; |
| 113 | + $paramCount = count($method->params); |
| 114 | + |
| 115 | + if ($paramCount < 2 || $paramCount > 3) { |
| 116 | + return [ |
| 117 | + RuleErrorBuilder::message( |
| 118 | + sprintf('Form alter hook "%s" implementation must have 2 or 3 parameters. %s', $hookName, $expectedSignature) |
| 119 | + ) |
| 120 | + ->line($method->getStartLine()) |
| 121 | + ->identifier('hookFormAlter.invalidParameterCount') |
| 122 | + ->build() |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + $errors = []; |
| 127 | + |
| 128 | + // Validate first parameter (form array by reference) |
| 129 | + $formParam = $method->params[0]; |
| 130 | + if (!$formParam->byRef) { |
| 131 | + $errors[] = RuleErrorBuilder::message( |
| 132 | + sprintf('Form alter hook "%s" first parameter must be passed by reference (&$form). %s', $hookName, $expectedSignature) |
| 133 | + ) |
| 134 | + ->line($method->getStartLine()) |
| 135 | + ->identifier('hookFormAlter.formParameterNotByRef') |
| 136 | + ->build(); |
| 137 | + } |
| 138 | + |
| 139 | + // Validate parameter types if type hints are present |
| 140 | + if ($formParam->type !== null) { |
| 141 | + $formParamType = $scope->getFunctionType($formParam->type, false, false); |
| 142 | + if (!$formParamType->isArray()->yes()) { |
| 143 | + $errors[] = RuleErrorBuilder::message( |
| 144 | + sprintf('Form alter hook "%s" first parameter should be an array. %s', $hookName, $expectedSignature) |
| 145 | + ) |
| 146 | + ->line($method->getStartLine()) |
| 147 | + ->identifier('hookFormAlter.invalidFormParameterType') |
| 148 | + ->build(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Validate second parameter (FormStateInterface) |
| 153 | + $formStateParam = $method->params[1]; |
| 154 | + if ($formStateParam->type !== null) { |
| 155 | + $formStateType = $scope->getFunctionType($formStateParam->type, false, false); |
| 156 | + $expectedFormStateType = new ObjectType(FormStateInterface::class); |
| 157 | + if (!$expectedFormStateType->isSuperTypeOf($formStateType)->yes()) { |
| 158 | + $errors[] = RuleErrorBuilder::message( |
| 159 | + sprintf( |
| 160 | + 'Form alter hook "%s" second parameter should be \Drupal\Core\Form\FormStateInterface, %s given. %s', |
| 161 | + $hookName, |
| 162 | + $formStateType->describe(VerbosityLevel::typeOnly()), |
| 163 | + $expectedSignature |
| 164 | + ) |
| 165 | + ) |
| 166 | + ->line($method->getStartLine()) |
| 167 | + ->identifier('hookFormAlter.invalidFormStateParameterType') |
| 168 | + ->build(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Validate third parameter (string form_id) - only if present |
| 173 | + if ($paramCount === 3) { |
| 174 | + $formIdParam = $method->params[2]; |
| 175 | + if ($formIdParam->type !== null) { |
| 176 | + $formIdType = $scope->getFunctionType($formIdParam->type, false, false); |
| 177 | + if (!$formIdType->isString()->yes()) { |
| 178 | + $errors[] = RuleErrorBuilder::message( |
| 179 | + sprintf( |
| 180 | + 'Form alter hook "%s" third parameter should be string, %s given. %s', |
| 181 | + $hookName, |
| 182 | + $formIdType->describe(VerbosityLevel::typeOnly()), |
| 183 | + $expectedSignature |
| 184 | + ) |
| 185 | + ) |
| 186 | + ->line($method->getStartLine()) |
| 187 | + ->identifier('hookFormAlter.invalidFormIdParameterType') |
| 188 | + ->build(); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return $errors; |
| 194 | + } |
| 195 | +} |
0 commit comments