ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
LegacyClassUsageRule.php
Go to the documentation of this file.
1 <?php
2 
20 
24 use PhpParser\Node;
27 
28 abstract class LegacyClassUsageRule implements Rule
29 {
30  protected ReflectionProvider $reflectionProvider;
31  protected \PHPStan\Rules\Generics\GenericAncestorsCheck $genericAncestorsCheck;
32  private array $forbidden_classes = [];
33  private array $ancestor_cache = [];
34 
35  public function __construct(
36  ReflectionProvider $reflectionProvider,
37  \PHPStan\Rules\Generics\GenericAncestorsCheck $genericAncestorsCheck
38  ) {
39  $this->reflectionProvider = $reflectionProvider;
40  $this->genericAncestorsCheck = $genericAncestorsCheck;
41 
42  // Determine possible class-names (parents and children) of the forbidden classes
43  $forbidden_classes = [];
44 
45  foreach ($this->getForbiddenClasses() as $forbidden_class) {
46  $ancestors = $this->getClassAncestors($forbidden_class);
47  $this->cacheAncestors($forbidden_class, $ancestors);
48  $forbidden_classes = array_merge(
49  $forbidden_classes,
50  $ancestors
51  );
52  }
53 
54  $this->forbidden_classes = array_unique($forbidden_classes);
55  }
56 
57  private function getClassAncestors(string $class_name): array
58  {
59  if (isset($this->ancestor_cache[$class_name])) {
60  return $this->ancestor_cache[$class_name];
61  }
62 
63  $ancestors[] = $class_name;
64 
65  try {
66  $reflection = $this->reflectionProvider->getClass($class_name);
67  $ancestors = array_merge($ancestors, $reflection->getParentClassesNames());
68  } catch (\PHPStan\Broker\ClassNotFoundException $e) {
69  // Do nothing
70  } finally {
71  unset($reflection);
72  }
73  return array_unique($ancestors);
74  }
75 
76  private function cacheAncestors($class_name, array $ancestor_classes): void
77  {
78  $this->ancestor_cache[$class_name] = $ancestor_classes;
79  }
80 
81  public function getNodeType(): string
82  {
83  return CallLike::class;
84  }
85 
86  protected function findInstanceCreation(): bool
87  {
88  return true;
89  }
90 
91  protected function findMethodUsages(): bool
92  {
93  return false;
94  }
95 
96  abstract protected function getForbiddenClasses(): array;
97 
98  abstract protected function getHumanReadableRuleName(): string;
99 
100  abstract protected function getRelevantILIASVersion(): int;
101 
102  private function instantiatesForbiddenClasses(Node $node): bool
103  {
104  if ($node->class instanceof Node\Name) {
105  $class_name = $node->class->toString();
106  } else {
107  return false;
108  }
109  $class_names_to_test = $this->getClassAncestors($class_name);
110 
111  $array_intersect = array_intersect($class_names_to_test, $this->forbidden_classes);
112  if ($array_intersect !== []) {
113  $this->cacheAncestors($class_name, $class_names_to_test);
114  return true;
115  }
116  return false;
117  }
118 
119  private function usesMethodsOfForbiddenClass(Node $node, Scope $scope): bool
120  {
121  if (!$node->name instanceof Node\Identifier) {
122  return false;
123  }
124  $method_name = $node->name->name;
125  $called_classes = $scope->getType($node->var)->getObjectClassNames();
126  $forbidden_classes = $this->getForbiddenClasses();
127 
128  return array_intersect($called_classes, $forbidden_classes) !== [];
129  }
130 
131  final public function processNode(Node $node, Scope $scope): array
132  {
133  // Find new Instance Creation or Static Method Calls
134  if ($this->findInstanceCreation()
135  && ($node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\New_)
136  && $this->instantiatesForbiddenClasses($node)
137  ) {
138  $class_name = $node->class->toString();
139  return [
140  RuleErrorBuilder::message("Usage of $class_name is forbidden.")
141  ->metadata([
142  'rule' => $this->getHumanReadableRuleName(),
143  'version' => $this->getRelevantILIASVersion(),
144  ])
145  ->build()
146  ];
147  }
148 
149  // Find Method Calls to forbidden classes
150  if ($node instanceof Node\Expr\MethodCall
151  && $this->findMethodUsages()
152  && $this->usesMethodsOfForbiddenClass($node, $scope)
153  ) {
154  $method_name = $node->name->name;
155  $class_name = $scope->getType($node->var)->getObjectClassNames()[0];
156  return [
157  RuleErrorBuilder::message("Usage of $class_name::$method_name is forbidden.")
158  ->metadata([
159  'rule' => $this->getHumanReadableRuleName(),
160  'version' => $this->getRelevantILIASVersion(),
161  ])
162  ->build()
163  ];
164  }
165 
166  return [];
167  }
168 }
$scope
Definition: ltiregstart.php:47
cacheAncestors($class_name, array $ancestor_classes)
__construct(ReflectionProvider $reflectionProvider, \PHPStan\Rules\Generics\GenericAncestorsCheck $genericAncestorsCheck)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
PHPStan Rules Generics GenericAncestorsCheck $genericAncestorsCheck