|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Reflection; |
| 15 | + |
| 16 | +use CodeIgniter\Entity\Entity; |
| 17 | +use CodeIgniter\PHPStan\Database\Schema\CastTypeResolver; |
| 18 | +use PHPStan\Reflection\ClassReflection; |
| 19 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 20 | +use PHPStan\Reflection\PropertiesClassReflectionExtension; |
| 21 | +use PHPStan\Reflection\PropertyReflection; |
| 22 | +use PHPStan\Reflection\ReflectionProvider; |
| 23 | +use PHPStan\Type\MixedType; |
| 24 | +use PHPStan\Type\Type; |
| 25 | +use PHPStan\Type\TypeCombinator; |
| 26 | + |
| 27 | +/** |
| 28 | + * Types virtual properties on `CodeIgniter\Entity\Entity` subclasses from their `$casts` entries, |
| 29 | + * reflecting custom `$castHandlers` for casts the framework does not define. |
| 30 | + */ |
| 31 | +final class EntityPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly ReflectionProvider $reflectionProvider, |
| 35 | + private readonly CastTypeResolver $castTypeResolver, |
| 36 | + ) {} |
| 37 | + |
| 38 | + public function hasProperty(ClassReflection $classReflection, string $propertyName): bool |
| 39 | + { |
| 40 | + if (! $classReflection->is(Entity::class)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $casts = $this->readStringMap($classReflection, 'casts'); |
| 45 | + $column = $this->mapColumn($classReflection, $propertyName); |
| 46 | + |
| 47 | + return isset($casts[$column]); |
| 48 | + } |
| 49 | + |
| 50 | + public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection |
| 51 | + { |
| 52 | + $casts = $this->readStringMap($classReflection, 'casts'); |
| 53 | + $column = $this->mapColumn($classReflection, $propertyName); |
| 54 | + $cast = $casts[$column] ?? ''; |
| 55 | + |
| 56 | + return new EntityPropertyReflection($classReflection, $this->resolveCastType($classReflection, $cast)); |
| 57 | + } |
| 58 | + |
| 59 | + private function resolveCastType(ClassReflection $classReflection, string $cast): Type |
| 60 | + { |
| 61 | + return $this->castTypeResolver->resolve($cast) ?? $this->resolveCustomHandlerType($classReflection, $cast); |
| 62 | + } |
| 63 | + |
| 64 | + private function resolveCustomHandlerType(ClassReflection $classReflection, string $cast): Type |
| 65 | + { |
| 66 | + $nullable = str_starts_with($cast, '?'); |
| 67 | + |
| 68 | + if ($nullable) { |
| 69 | + $cast = substr($cast, 1); |
| 70 | + } |
| 71 | + |
| 72 | + $handler = $this->readStringMap($classReflection, 'castHandlers')[$this->castName($cast)] ?? null; |
| 73 | + |
| 74 | + if ($handler === null || ! $this->reflectionProvider->hasClass($handler)) { |
| 75 | + return new MixedType(); |
| 76 | + } |
| 77 | + |
| 78 | + $handlerReflection = $this->reflectionProvider->getClass($handler); |
| 79 | + |
| 80 | + if (! $handlerReflection->hasNativeMethod('get')) { |
| 81 | + return new MixedType(); |
| 82 | + } |
| 83 | + |
| 84 | + $type = ParametersAcceptorSelector::combineAcceptors($handlerReflection->getNativeMethod('get')->getVariants())->getReturnType(); |
| 85 | + |
| 86 | + return $nullable ? TypeCombinator::addNull($type) : $type; |
| 87 | + } |
| 88 | + |
| 89 | + private function castName(string $cast): string |
| 90 | + { |
| 91 | + if (preg_match('/\A(.+)\[.+\]\z/', $cast, $matches) === 1) { |
| 92 | + return $matches[1]; |
| 93 | + } |
| 94 | + |
| 95 | + return $cast; |
| 96 | + } |
| 97 | + |
| 98 | + private function mapColumn(ClassReflection $classReflection, string $propertyName): string |
| 99 | + { |
| 100 | + $mapped = $this->readStringMap($classReflection, 'datamap')[$propertyName] ?? ''; |
| 101 | + |
| 102 | + return $mapped !== '' ? $mapped : $propertyName; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, string> |
| 107 | + */ |
| 108 | + private function readStringMap(ClassReflection $classReflection, string $property): array |
| 109 | + { |
| 110 | + $value = $classReflection->getNativeReflection()->getDefaultProperties()[$property] ?? []; |
| 111 | + |
| 112 | + if (! is_array($value)) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + |
| 116 | + $map = []; |
| 117 | + |
| 118 | + foreach ($value as $key => $cast) { |
| 119 | + if (is_string($key) && is_string($cast)) { |
| 120 | + $map[$key] = $cast; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return $map; |
| 125 | + } |
| 126 | +} |
0 commit comments