|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace mglaman\PHPStanDrupal\Rules\Drupal; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Broker\Broker; |
| 8 | +use PHPStan\Reflection\Php\PhpFunctionReflection; |
| 9 | +use PHPStan\Reflection\ReflectionProvider; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use PHPStan\TrinaryLogic; |
| 13 | +use PHPStan\Type\ClosureType; |
| 14 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 15 | +use PHPStan\Type\Constant\ConstantArrayTypeAndMethod; |
| 16 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 17 | +use PHPStan\Type\Constant\ConstantStringType; |
| 18 | +use PHPStan\Type\ObjectType; |
| 19 | +use PHPStan\Type\VerbosityLevel; |
| 20 | + |
| 21 | +final class RenderCallbackRule implements Rule |
| 22 | +{ |
| 23 | + |
| 24 | + protected ReflectionProvider $reflectionProvider; |
| 25 | + |
| 26 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 27 | + { |
| 28 | + $this->reflectionProvider = $reflectionProvider; |
| 29 | + } |
| 30 | + |
| 31 | + public function getNodeType(): string |
| 32 | + { |
| 33 | + return Node\Expr\ArrayItem::class; |
| 34 | + } |
| 35 | + |
| 36 | + public function processNode(Node $node, Scope $scope): array |
| 37 | + { |
| 38 | + assert($node instanceof Node\Expr\ArrayItem); |
| 39 | + $key = $node->key; |
| 40 | + if (!$key instanceof Node\Scalar\String_) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + // @see https://www.drupal.org/node/2966725 |
| 44 | + $keysToCheck = ['#pre_render', '#post_render', '#lazy_builder', '#access_callback']; |
| 45 | + $keySearch = array_search($key->value, $keysToCheck, true); |
| 46 | + if ($keySearch === false) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + $keyChecked = $keysToCheck[$keySearch]; |
| 50 | + |
| 51 | + $value = $node->value; |
| 52 | + if (!$value instanceof Node\Expr\Array_) { |
| 53 | + return [ |
| 54 | + RuleErrorBuilder::message(sprintf('The "%s" render array value expects an array of callbacks.', $keyChecked)) |
| 55 | + ->line($node->getLine())->build() |
| 56 | + ]; |
| 57 | + } |
| 58 | + if (count($value->items) === 0) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $trustedCallbackType = new ObjectType('Drupal\Core\Security\TrustedCallbackInterface'); |
| 63 | + $errors = []; |
| 64 | + foreach ($value->items as $pos => $item) { |
| 65 | + if (!$item instanceof Node\Expr\ArrayItem) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + $errorLine = $item->value->getLine(); |
| 69 | + $type = $scope->getType($item->value); |
| 70 | + |
| 71 | + if ($type instanceof ConstantStringType) { |
| 72 | + if (!$type->isCallable()->yes()) { |
| 73 | + $errors[] = RuleErrorBuilder::message( |
| 74 | + sprintf("%s callback %s at key '%s' is not callable.", $keyChecked, $type->describe(VerbosityLevel::value()), $pos) |
| 75 | + )->line($errorLine)->build(); |
| 76 | + continue; |
| 77 | + } |
| 78 | + // We can determine if the callback is callable through the type system. However, we cannot determine |
| 79 | + // if it is just a function or a static class call (MyClass::staticFunc). |
| 80 | + if ($this->reflectionProvider->hasFunction(new \PhpParser\Node\Name($type->getValue()), null)) { |
| 81 | + $errors[] = RuleErrorBuilder::message( |
| 82 | + sprintf("%s callback %s at key '%s' is not trusted.", $keyChecked, $type->describe(VerbosityLevel::value()), $pos) |
| 83 | + )->line($errorLine) |
| 84 | + ->tip('Change record: https://www.drupal.org/node/2966725.') |
| 85 | + ->build(); |
| 86 | + continue; |
| 87 | + } |
| 88 | + // @see \PHPStan\Type\Constant\ConstantStringType::isCallable |
| 89 | + preg_match('#^([a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*)::([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\z#', $type->getValue(), $matches); |
| 90 | + if ($matches === null) { |
| 91 | + throw new \PHPStan\ShouldNotHappenException('Unable to get class name from ConstantStringType value: ' . $type->describe(VerbosityLevel::value())); |
| 92 | + } |
| 93 | + if (!$trustedCallbackType->isSuperTypeOf(new ObjectType($matches[1]))->yes()) { |
| 94 | + $errors[] = RuleErrorBuilder::message( |
| 95 | + sprintf("%s callback class '%s' at key '%s' does not implement Drupal\Core\Security\TrustedCallbackInterface.", $keyChecked, (new ObjectType($matches[1]))->describe(VerbosityLevel::value()), $pos) |
| 96 | + )->line($errorLine)->tip('Change record: https://www.drupal.org/node/2966725.')->build(); |
| 97 | + } |
| 98 | + } elseif ($type instanceof ConstantArrayType) { |
| 99 | + if (!$type->isCallable()->yes()) { |
| 100 | + $errors[] = RuleErrorBuilder::message( |
| 101 | + sprintf("%s callback %s at key '%s' is not callable.", $keyChecked, $type->describe(VerbosityLevel::value()), $pos) |
| 102 | + )->line($errorLine)->build(); |
| 103 | + continue; |
| 104 | + } |
| 105 | + $typeAndMethodName = $type->findTypeAndMethodName(); |
| 106 | + if ($typeAndMethodName === null) { |
| 107 | + throw new \PHPStan\ShouldNotHappenException(); |
| 108 | + } |
| 109 | + |
| 110 | + if (!$trustedCallbackType->isSuperTypeOf($typeAndMethodName->getType())->yes()) { |
| 111 | + $errors[] = RuleErrorBuilder::message( |
| 112 | + sprintf("%s callback class '%s' at key '%s' does not implement Drupal\Core\Security\TrustedCallbackInterface.", $keyChecked, $typeAndMethodName->getType()->describe(VerbosityLevel::value()), $pos) |
| 113 | + )->line($errorLine)->tip('Change record: https://www.drupal.org/node/2966725.')->build(); |
| 114 | + } |
| 115 | + } elseif ($type instanceof ClosureType) { |
| 116 | + if ($scope->isInClass()) { |
| 117 | + $classReflection = $scope->getClassReflection(); |
| 118 | + if ($classReflection === null) { |
| 119 | + throw new \PHPStan\ShouldNotHappenException(); |
| 120 | + } |
| 121 | + $classType = new ObjectType($classReflection->getName()); |
| 122 | + $formType = new ObjectType('\Drupal\Core\Form\FormInterface'); |
| 123 | + if ($formType->isSuperTypeOf($classType)->yes()) { |
| 124 | + $errors[] = RuleErrorBuilder::message( |
| 125 | + sprintf("%s may not contain a closure at key '%s' as forms may be serialized and serialization of closures is not allowed.", $keyChecked, $pos) |
| 126 | + )->line($errorLine)->build(); |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + $errors[] = RuleErrorBuilder::message( |
| 131 | + sprintf("%s value '%s' at key '%s' is invalid.", $keyChecked, $type->describe(VerbosityLevel::value()), $pos) |
| 132 | + )->line($errorLine)->build(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return $errors; |
| 137 | + } |
| 138 | +} |
0 commit comments