|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (c) Florian Krämer (https://florian-kraemer.net) |
| 5 | + * Licensed under The MIT License |
| 6 | + * For full copyright and license information, please see the LICENSE file |
| 7 | + * Redistributions of files must retain the above copyright notice. |
| 8 | + * |
| 9 | + * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
| 10 | + * @author Florian Krämer |
| 11 | + * @link https://github.com/Phauthentic |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + */ |
| 14 | + |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Phauthentic\PHPStanRules\Architecture; |
| 18 | + |
| 19 | +use PhpParser\Node\ComplexType; |
| 20 | +use PhpParser\Node\Identifier; |
| 21 | +use PhpParser\Node\IntersectionType; |
| 22 | +use PhpParser\Node\Name; |
| 23 | +use PhpParser\Node\NullableType; |
| 24 | +use PhpParser\Node\Stmt\Class_; |
| 25 | +use PhpParser\Node\Stmt\Interface_; |
| 26 | +use PhpParser\Node\UnionType; |
| 27 | +use PHPStan\Analyser\Scope; |
| 28 | + |
| 29 | +/** |
| 30 | + * Trait providing common FQCN resolution and type conversion utilities for PHPStan rules. |
| 31 | + */ |
| 32 | +trait ClassNameResolver |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Build FQCN from a Class_ or Interface_ node and scope. |
| 36 | + * |
| 37 | + * @param Class_|Interface_ $node |
| 38 | + * @param Scope $scope |
| 39 | + * @return string|null Returns null if the node has no name (anonymous class) |
| 40 | + */ |
| 41 | + protected function resolveFullClassName(Class_|Interface_ $node, Scope $scope): ?string |
| 42 | + { |
| 43 | + if (!isset($node->name)) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + $className = $node->name->toString(); |
| 48 | + $namespaceName = $scope->getNamespace() ?? ''; |
| 49 | + |
| 50 | + return $namespaceName !== '' ? $namespaceName . '\\' . $className : $className; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Convert a type node to its string representation. |
| 55 | + * |
| 56 | + * Handles Identifier, Name, NullableType, UnionType, and IntersectionType. |
| 57 | + * |
| 58 | + * @param ComplexType|Identifier|Name|null $type |
| 59 | + * @return string|null |
| 60 | + */ |
| 61 | + protected function getTypeAsString(ComplexType|Identifier|Name|null $type): ?string |
| 62 | + { |
| 63 | + return match (true) { |
| 64 | + $type === null => null, |
| 65 | + $type instanceof Identifier => $type->name, |
| 66 | + $type instanceof Name => $type->toString(), |
| 67 | + $type instanceof NullableType => |
| 68 | + ($inner = $this->getTypeAsString($type->type)) !== null |
| 69 | + ? '?' . $inner |
| 70 | + : null, |
| 71 | + $type instanceof UnionType => implode('|', array_filter( |
| 72 | + array_map(fn($t) => $this->getTypeAsString($t), $type->types) |
| 73 | + )), |
| 74 | + $type instanceof IntersectionType => implode('&', array_filter( |
| 75 | + array_map(fn($t) => $this->getTypeAsString($t), $type->types) |
| 76 | + )), |
| 77 | + default => null, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if a subject string matches any of the given regex patterns. |
| 83 | + * |
| 84 | + * @param string $subject The string to test |
| 85 | + * @param array<string> $patterns Array of regex patterns |
| 86 | + * @return bool True if any pattern matches |
| 87 | + */ |
| 88 | + protected function matchesAnyPattern(string $subject, array $patterns): bool |
| 89 | + { |
| 90 | + foreach ($patterns as $pattern) { |
| 91 | + if (preg_match($pattern, $subject) === 1) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Normalize a class name by removing leading backslash. |
| 101 | + * |
| 102 | + * @param string $className |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + protected function normalizeClassName(string $className): string |
| 106 | + { |
| 107 | + return ltrim($className, '\\'); |
| 108 | + } |
| 109 | +} |
0 commit comments