-
Couldn't load subscription status.
- Fork 6
Fix up TypeFactory return type. #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Type/TypeFactoryBuildDynamicReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright 2025, Cake Development Corporation (https://www.cakedc.com) | ||
| * | ||
| * Licensed under The MIT License | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright 2020, Cake Development Corporation (https://www.cakedc.com) | ||
| * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
| */ | ||
|
|
||
| namespace CakeDC\PHPStan\Type; | ||
|
|
||
| use Cake\Database\TypeFactory; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\Type; | ||
| use ReflectionClass; | ||
| use ReflectionException; | ||
|
|
||
| /** | ||
| * Provides return type for TypeFactory::build() based on the type name argument. | ||
| * | ||
| * This allows PHPStan to understand that TypeFactory::build('datetime') returns | ||
| * a DateTimeType instance with its specific methods like setUserTimezone(). | ||
| */ | ||
| class TypeFactoryBuildDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | ||
| { | ||
| /** | ||
| * @var array<string, class-string>|null | ||
| */ | ||
| private ?array $typeMap = null; | ||
|
|
||
| /** | ||
| * @return class-string | ||
| */ | ||
| public function getClass(): string | ||
| { | ||
| return TypeFactory::class; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the method is supported. | ||
| * | ||
| * @param \PHPStan\Reflection\MethodReflection $methodReflection Method reflection | ||
| * @return bool | ||
| */ | ||
| public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->getName() === 'build'; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the type from the static method call. | ||
| * | ||
| * @param \PHPStan\Reflection\MethodReflection $methodReflection Method reflection | ||
| * @param \PhpParser\Node\Expr\StaticCall $methodCall Static method call | ||
| * @param \PHPStan\Analyser\Scope $scope Scope | ||
| * @return \PHPStan\Type\Type|null | ||
| */ | ||
| public function getTypeFromStaticMethodCall( | ||
| MethodReflection $methodReflection, | ||
| StaticCall $methodCall, | ||
| Scope $scope, | ||
| ): ?Type { | ||
| $args = $methodCall->getArgs(); | ||
| if (count($args) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| $argType = $scope->getType($args[0]->value); | ||
| $constantStrings = $argType->getConstantStrings(); | ||
| if (count($constantStrings) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $typeName = $constantStrings[0]->getValue(); | ||
| $typeMap = $this->getTypeMap(); | ||
|
|
||
| if (!isset($typeMap[$typeName])) { | ||
| return null; | ||
| } | ||
|
|
||
| return new ObjectType($typeMap[$typeName]); | ||
| } | ||
|
|
||
| /** | ||
| * Get the type map by reading TypeFactory's static property via reflection. | ||
| * This is cached after the first call. | ||
| * | ||
| * @return array<string, class-string> | ||
| */ | ||
| private function getTypeMap(): array | ||
| { | ||
| if ($this->typeMap !== null) { | ||
| return $this->typeMap; | ||
| } | ||
|
|
||
| try { | ||
| $reflection = new ReflectionClass(TypeFactory::class); | ||
| $property = $reflection->getProperty('_types'); | ||
| $property->setAccessible(true); | ||
|
|
||
| /** @var array<string, class-string> $defaultValue */ | ||
| $defaultValue = $property->getDefaultValue(); | ||
| $this->typeMap = $defaultValue; | ||
|
|
||
| return $this->typeMap; | ||
| } catch (ReflectionException $e) { | ||
| return []; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace CakeDC\PHPStan\Test\TestCase\Type\Fake; | ||
|
|
||
| use Cake\Database\TypeFactory; | ||
|
|
||
| class TypeFactoryCorrectUsage | ||
| { | ||
| public function testDateTimeTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('datetime'); | ||
| $type->setUserTimezone('America/New_York'); | ||
| } | ||
|
|
||
| public function testTimestampTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('timestamp'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testDateTimeFractionalTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('datetimefractional'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testDateTimeTimezoneTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('timestamptimezone'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testDateTypeWithSetLocaleFormat(): void | ||
| { | ||
| $type = TypeFactory::build('date'); | ||
| $type->setLocaleFormat('yyyy-MM-dd'); | ||
| } | ||
|
|
||
| public function testTimeTypeWithSetLocaleFormat(): void | ||
| { | ||
| $type = TypeFactory::build('time'); | ||
| $type->setLocaleFormat('HH:mm:ss'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace CakeDC\PHPStan\Test\TestCase\Type\Fake; | ||
|
|
||
| use Cake\Database\TypeFactory; | ||
|
|
||
| class TypeFactoryIncorrectUsage | ||
| { | ||
| public function testIntegerTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('integer'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testStringTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('string'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testBoolTypeWithSetUserTimezone(): void | ||
| { | ||
| $type = TypeFactory::build('boolean'); | ||
| $type->setUserTimezone('UTC'); | ||
| } | ||
|
|
||
| public function testJsonTypeWithNonExistentMethod(): void | ||
| { | ||
| $type = TypeFactory::build('json'); | ||
| $type->nonExistentMethod(); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
tests/TestCase/Type/TypeFactoryBuildDynamicReturnTypeExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright 2025, Cake Development Corporation (https://www.cakedc.com) | ||
| * | ||
| * Licensed under The MIT License | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright 2025, Cake Development Corporation (https://www.cakedc.com) | ||
| * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
| */ | ||
|
|
||
| namespace CakeDC\PHPStan\Test\TestCase\Type; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class TypeFactoryBuildDynamicReturnTypeExtensionTest extends TestCase | ||
| { | ||
| /** | ||
| * Test that TypeFactory::build() returns correct types and allows valid method calls. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testTypeFactoryBuildReturnsCorrectTypes(): void | ||
| { | ||
| $output = $this->runPhpStan(__DIR__ . '/Fake/TypeFactoryCorrectUsage.php'); | ||
| $this->assertStringContainsString('[OK] No errors', $output); | ||
| } | ||
|
|
||
| /** | ||
| * Test that TypeFactory::build() catches invalid method calls. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testTypeFactoryBuildCatchesInvalidMethodCalls(): void | ||
| { | ||
| $output = $this->runPhpStan(__DIR__ . '/Fake/TypeFactoryIncorrectUsage.php'); | ||
|
|
||
| $this->assertStringContainsString('IntegerType::setUserTimezone()', $output); | ||
| $this->assertStringContainsString('StringType::setUserTimezone()', $output); | ||
| $this->assertStringContainsString('BoolType::setUserTimezone()', $output); | ||
| $this->assertStringContainsString('JsonType::nonExistentMethod()', $output); | ||
| $this->assertStringContainsString('Found 4 errors', $output); | ||
| } | ||
|
|
||
| /** | ||
| * Run PHPStan on a file and return the output. | ||
| * | ||
| * @param string $file File to analyze | ||
| * @return string | ||
| */ | ||
| private function runPhpStan(string $file): string | ||
| { | ||
| $configFile = dirname(__DIR__, 3) . '/extension.neon'; | ||
| $command = sprintf( | ||
| 'cd %s && vendor/bin/phpstan analyze %s --level=max --configuration=%s --no-progress 2>&1', | ||
| escapeshellarg(dirname(__DIR__, 3)), | ||
| escapeshellarg($file), | ||
| escapeshellarg($configFile), | ||
| ); | ||
|
|
||
| exec($command, $output, $exitCode); | ||
|
|
||
| return implode("\n", $output); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here could use ReflectionProvider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy if someone wants to finish this up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay