| Total Complexity | 44 |
| Total Lines | 205 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like InternalClassRule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InternalClassRule, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 33 | #[Package('core')] |
||
| 34 | class InternalClassRule implements Rule |
||
| 35 | { |
||
| 36 | private const TEST_CLASS_EXCEPTIONS = [ |
||
| 37 | StoreApiTestOtherRoute::class, // The test route is used to test the OpenApiGenerator, that class would ignore internal classes |
||
| 38 | ]; |
||
| 39 | |||
| 40 | private const INTERNAL_NAMESPACES = [ |
||
| 41 | '\\DevOps\\StaticAnalyze', |
||
| 42 | ]; |
||
| 43 | private const SUBSCRIBER_EXCEPTIONS = [ |
||
| 44 | RefreshIndexCommand::class, |
||
| 45 | ]; |
||
| 46 | private const MESSAGE_HANDLER_EXCEPTIONS = [ |
||
| 47 | EntityIndexerRegistry::class, |
||
| 48 | ]; |
||
| 49 | private const DEMO_DATA_EXCEPTIONS = [ |
||
| 50 | DemodataContext::class, |
||
| 51 | DemodataGeneratorInterface::class, |
||
| 52 | DemodataRequest::class, |
||
| 53 | DemodataService::class, |
||
| 54 | DemodataCommand::class, |
||
| 55 | DemodataRequestCreatedEvent::class, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | public function getNodeType(): string |
||
| 59 | { |
||
| 60 | return InClassNode::class; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param InClassNode $node |
||
| 65 | * |
||
| 66 | * @return array<array-key, RuleError|string> |
||
| 67 | */ |
||
| 68 | public function processNode(Node $node, Scope $scope): array |
||
| 69 | { |
||
| 70 | if ($this->isInternal($node)) { |
||
| 71 | return []; |
||
| 72 | } |
||
| 73 | |||
| 74 | $class = $node->getClassReflection()->getName(); |
||
| 75 | |||
| 76 | if ($this->isTestClass($node)) { |
||
| 77 | return ['Test classes must be flagged @internal to not be captured by the BC checker']; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->isStorefrontController($node)) { |
||
| 81 | return ['Storefront controllers must be flagged @internal to not be captured by the BC checker. The BC promise is checked over the route annotation.']; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($this->isBundle($node)) { |
||
| 85 | return ['Bundles must be flagged @internal to not be captured by the BC checker.']; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($this->isEventSubscriber($node) && !$this->isFinal($node) && !\in_array($class, self::SUBSCRIBER_EXCEPTIONS, true)) { |
||
| 89 | return ['Event subscribers must be flagged @internal to not be captured by the BC checker.']; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($namespace = $this->isInInternalNamespace($node)) { |
||
| 93 | return ['Classes in `' . $namespace . '` namespace must be flagged @internal to not be captured by the BC checker.']; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($this->isInNamespace($node, '\\Framework\\Demodata') && !\in_array($class, self::DEMO_DATA_EXCEPTIONS, true)) { |
||
| 97 | return ['Classes in `Framework\\Demodata` namespace must be flagged @internal to not be captured by the BC checker.']; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($this->isMigrationStep($node)) { |
||
| 101 | return ['Migrations must be flagged @internal to not be captured by the BC checker.']; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($this->isMessageHandler($node) && !\in_array($class, self::MESSAGE_HANDLER_EXCEPTIONS, true)) { |
||
| 105 | return ['MessageHandlers must be flagged @internal to not be captured by the BC checker.']; |
||
| 106 | } |
||
| 107 | |||
| 108 | return []; |
||
| 109 | } |
||
| 110 | |||
| 111 | private function isTestClass(InClassNode $node): bool |
||
| 112 | { |
||
| 113 | $namespace = $node->getClassReflection()->getName(); |
||
| 114 | |||
| 115 | if (\in_array($namespace, self::TEST_CLASS_EXCEPTIONS, true)) { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (\str_contains($namespace, '\\Test\\')) { |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | if (\str_contains($namespace, '\\Tests\\')) { |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($node->getClassReflection()->getParentClass() === null) { |
||
| 128 | return false; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $node->getClassReflection()->getParentClass()->getName() === TestCase::class; |
||
| 132 | } |
||
| 133 | |||
| 134 | private function isInternal(InClassNode $class): bool |
||
| 135 | { |
||
| 136 | $doc = $class->getDocComment(); |
||
| 137 | |||
| 138 | if ($doc === null) { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | return \str_contains($doc->getText(), '@internal') || \str_contains($doc->getText(), 'reason:becomes-internal'); |
||
| 143 | } |
||
| 144 | |||
| 145 | private function isStorefrontController(InClassNode $node): bool |
||
| 146 | { |
||
| 147 | $class = $node->getClassReflection(); |
||
| 148 | |||
| 149 | if ($class->getParentClass() === null) { |
||
| 150 | return false; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $class->getParentClass()->getName() === StorefrontController::class; |
||
| 154 | } |
||
| 155 | |||
| 156 | private function isBundle(InClassNode $node): bool |
||
| 157 | { |
||
| 158 | $class = $node->getClassReflection(); |
||
| 159 | |||
| 160 | if ($class->getParentClass() === null) { |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($class->isAnonymous()) { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $class->getParentClass()->getName() === Bundle::class && $class->getName() !== Plugin::class; |
||
| 169 | } |
||
| 170 | |||
| 171 | private function isEventSubscriber(InClassNode $node): bool |
||
| 172 | { |
||
| 173 | $class = $node->getClassReflection(); |
||
| 174 | |||
| 175 | foreach ($class->getInterfaces() as $interface) { |
||
| 176 | if ($interface->getName() === EventSubscriberInterface::class) { |
||
| 177 | return true; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | private function isInInternalNamespace(InClassNode $node): ?string |
||
| 185 | { |
||
| 186 | $namespace = $node->getClassReflection()->getName(); |
||
| 187 | |||
| 188 | foreach (self::INTERNAL_NAMESPACES as $internalNamespace) { |
||
| 189 | if (\str_contains($namespace, $internalNamespace)) { |
||
| 190 | return $internalNamespace; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return null; |
||
| 195 | } |
||
| 196 | |||
| 197 | private function isInNamespace(InClassNode $node, string $namespace): bool |
||
| 198 | { |
||
| 199 | return \str_contains($node->getClassReflection()->getName(), $namespace); |
||
| 200 | } |
||
| 201 | |||
| 202 | private function isMigrationStep(InClassNode $node): bool |
||
| 211 | } |
||
| 212 | |||
| 213 | private function isMessageHandler(InClassNode $node): bool |
||
| 214 | { |
||
| 215 | $class = $node->getClassReflection()->getNativeReflection(); |
||
| 216 | |||
| 223 | } |
||
| 224 | |||
| 225 | private function isFinal(InClassNode $class): bool |
||
| 226 | { |
||
| 227 | if ($class->getClassReflection()->isFinal()) { |
||
| 240 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths