|  | 
|  | 1 | +<?php | 
|  | 2 | +declare(strict_types=1); | 
|  | 3 | + | 
|  | 4 | +/** | 
|  | 5 | + * Copyright 2025, Cake Development Corporation (https://www.cakedc.com) | 
|  | 6 | + * | 
|  | 7 | + * Licensed under The MIT License | 
|  | 8 | + * Redistributions of files must retain the above copyright notice. | 
|  | 9 | + * | 
|  | 10 | + * @copyright Copyright 2020, Cake Development Corporation (https://www.cakedc.com) | 
|  | 11 | + * @license   MIT License (http://www.opensource.org/licenses/mit-license.php) | 
|  | 12 | + */ | 
|  | 13 | + | 
|  | 14 | +namespace CakeDC\PHPStan\Type; | 
|  | 15 | + | 
|  | 16 | +use Cake\Database\TypeFactory; | 
|  | 17 | +use PhpParser\Node\Expr\StaticCall; | 
|  | 18 | +use PHPStan\Analyser\Scope; | 
|  | 19 | +use PHPStan\Reflection\MethodReflection; | 
|  | 20 | +use PHPStan\Reflection\MissingPropertyFromReflectionException; | 
|  | 21 | +use PHPStan\Reflection\ReflectionProvider; | 
|  | 22 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | 
|  | 23 | +use PHPStan\Type\ObjectType; | 
|  | 24 | +use PHPStan\Type\Type; | 
|  | 25 | +use ReflectionException; | 
|  | 26 | + | 
|  | 27 | +/** | 
|  | 28 | + * Provides return type for TypeFactory::build() based on the type name argument. | 
|  | 29 | + * | 
|  | 30 | + * This allows PHPStan to understand that TypeFactory::build('datetime') returns | 
|  | 31 | + * a DateTimeType instance with its specific methods like setUserTimezone(). | 
|  | 32 | + */ | 
|  | 33 | +class TypeFactoryBuildDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | 
|  | 34 | +{ | 
|  | 35 | +    /** | 
|  | 36 | +     * @var array<string, class-string>|null | 
|  | 37 | +     */ | 
|  | 38 | +    private ?array $typeMap = null; | 
|  | 39 | + | 
|  | 40 | +    /** | 
|  | 41 | +     * @var \PHPStan\Reflection\ReflectionProvider | 
|  | 42 | +     */ | 
|  | 43 | +    protected ReflectionProvider $reflectionProvider; | 
|  | 44 | + | 
|  | 45 | +    /** | 
|  | 46 | +     * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider | 
|  | 47 | +     */ | 
|  | 48 | +    public function __construct(ReflectionProvider $reflectionProvider) | 
|  | 49 | +    { | 
|  | 50 | +        $this->reflectionProvider = $reflectionProvider; | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    /** | 
|  | 54 | +     * @return class-string | 
|  | 55 | +     */ | 
|  | 56 | +    public function getClass(): string | 
|  | 57 | +    { | 
|  | 58 | +        return TypeFactory::class; | 
|  | 59 | +    } | 
|  | 60 | + | 
|  | 61 | +    /** | 
|  | 62 | +     * Checks if the method is supported. | 
|  | 63 | +     * | 
|  | 64 | +     * @param \PHPStan\Reflection\MethodReflection $methodReflection Method reflection | 
|  | 65 | +     * @return bool | 
|  | 66 | +     */ | 
|  | 67 | +    public function isStaticMethodSupported(MethodReflection $methodReflection): bool | 
|  | 68 | +    { | 
|  | 69 | +        return $methodReflection->getName() === 'build'; | 
|  | 70 | +    } | 
|  | 71 | + | 
|  | 72 | +    /** | 
|  | 73 | +     * Returns the type from the static method call. | 
|  | 74 | +     * | 
|  | 75 | +     * @param \PHPStan\Reflection\MethodReflection $methodReflection Method reflection | 
|  | 76 | +     * @param \PhpParser\Node\Expr\StaticCall $methodCall Static method call | 
|  | 77 | +     * @param \PHPStan\Analyser\Scope $scope Scope | 
|  | 78 | +     * @return \PHPStan\Type\Type|null | 
|  | 79 | +     */ | 
|  | 80 | +    public function getTypeFromStaticMethodCall( | 
|  | 81 | +        MethodReflection $methodReflection, | 
|  | 82 | +        StaticCall $methodCall, | 
|  | 83 | +        Scope $scope, | 
|  | 84 | +    ): ?Type { | 
|  | 85 | +        $args = $methodCall->getArgs(); | 
|  | 86 | +        if (count($args) === 0) { | 
|  | 87 | +            return null; | 
|  | 88 | +        } | 
|  | 89 | + | 
|  | 90 | +        $argType = $scope->getType($args[0]->value); | 
|  | 91 | +        $constantStrings = $argType->getConstantStrings(); | 
|  | 92 | +        if (count($constantStrings) !== 1) { | 
|  | 93 | +            return null; | 
|  | 94 | +        } | 
|  | 95 | + | 
|  | 96 | +        $typeName = $constantStrings[0]->getValue(); | 
|  | 97 | +        $typeMap = $this->getTypeMap(); | 
|  | 98 | + | 
|  | 99 | +        if (!isset($typeMap[$typeName])) { | 
|  | 100 | +            return null; | 
|  | 101 | +        } | 
|  | 102 | + | 
|  | 103 | +        return new ObjectType($typeMap[$typeName]); | 
|  | 104 | +    } | 
|  | 105 | + | 
|  | 106 | +    /** | 
|  | 107 | +     * Get the type map by reading TypeFactory's static property via reflection. | 
|  | 108 | +     * This is cached after the first call. | 
|  | 109 | +     * | 
|  | 110 | +     * @return array<string, class-string> | 
|  | 111 | +     */ | 
|  | 112 | +    private function getTypeMap(): array | 
|  | 113 | +    { | 
|  | 114 | +        if ($this->typeMap !== null) { | 
|  | 115 | +            return $this->typeMap; | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        try { | 
|  | 119 | +            $reflection = $this->reflectionProvider->getClass(TypeFactory::class); | 
|  | 120 | +            /** | 
|  | 121 | +             * @var \PHPStan\Reflection\Php\PhpPropertyReflection $property | 
|  | 122 | +             */ | 
|  | 123 | +            $property = $reflection->getStaticProperty('_types'); | 
|  | 124 | +            /** | 
|  | 125 | +             * @var array<string, class-string> $defaultValue | 
|  | 126 | +             */ | 
|  | 127 | +            $defaultValue = $property->getNativeReflection()->getValue(); | 
|  | 128 | +            $this->typeMap = $defaultValue; | 
|  | 129 | + | 
|  | 130 | +            return $this->typeMap; | 
|  | 131 | +        } catch (MissingPropertyFromReflectionException | ReflectionException) { | 
|  | 132 | +            return []; | 
|  | 133 | +        } | 
|  | 134 | +    } | 
|  | 135 | +} | 
0 commit comments