-
Notifications
You must be signed in to change notification settings - Fork 105
Add typed Version object
#821
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| final class Version | ||
| { | ||
| /** | ||
| * @param non-empty-string $commitSha | ||
| * @param non-empty-string $pkgVersion | ||
| */ | ||
| public function __construct( | ||
| private readonly string $commitSha, | ||
| private readonly \DateTimeImmutable $commitDate, | ||
| private readonly string $pkgVersion, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return non-empty-string | ||
| */ | ||
| public function getCommitSha(): string | ||
| { | ||
| return $this->commitSha; | ||
| } | ||
|
|
||
| public function getCommitDate(): \DateTimeImmutable | ||
| { | ||
| return $this->commitDate; | ||
| } | ||
|
|
||
| public function getPkgVersion(): string | ||
| { | ||
| return $this->pkgVersion; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{ | ||
| * commitSha: non-empty-string, | ||
| * commitDate: non-empty-string, | ||
| * pkgVersion: non-empty-string | ||
| * } $data | ||
| */ | ||
| public static function fromArray(array $data): Version | ||
| { | ||
| return new self( | ||
| $data['commitSha'], | ||
| new \DateTimeImmutable($data['commitDate']), | ||
| $data['pkgVersion'], | ||
| ); | ||
| } | ||
| } | ||
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
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,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Contracts; | ||
|
|
||
| use Meilisearch\Contracts\Version; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class VersionTest extends TestCase | ||
| { | ||
| public function testConstruct(): void | ||
| { | ||
| $version = new Version( | ||
| commitSha: 'ea70a7d1c90b4d87c1c3319e9bf280dc790f7f5e', | ||
| commitDate: $date = new \DateTimeImmutable('2025-11-15 10:03:15.000000'), | ||
| pkgVersion: '1.26.0', | ||
| ); | ||
|
|
||
| self::assertSame('ea70a7d1c90b4d87c1c3319e9bf280dc790f7f5e', $version->getCommitSha()); | ||
| self::assertSame($date, $version->getCommitDate()); | ||
| self::assertSame('1.26.0', $version->getPkgVersion()); | ||
| } | ||
|
|
||
| public function testFromArray(): void | ||
| { | ||
| $version = Version::fromArray([ | ||
| 'commitSha' => 'ea70a7d1c90b4d87c1c3319e9bf280dc790f7f5e', | ||
| 'commitDate' => '2025-11-15T10:03:15.000000000Z', | ||
| 'pkgVersion' => '1.26.0', | ||
| ]); | ||
|
|
||
| self::assertSame('ea70a7d1c90b4d87c1c3319e9bf280dc790f7f5e', $version->getCommitSha()); | ||
| self::assertEquals(new \DateTimeImmutable('2025-11-15 10:03:15.000000'), $version->getCommitDate()); | ||
| self::assertSame('1.26.0', $version->getPkgVersion()); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Add validation and error handling in fromArray.
The
fromArraymethod directly accesses array keys without validating their existence and constructsDateTimeImmutablewithout error handling. This can lead to runtime errors if:commitDatestring is in an invalid formatConsider adding validation:
public static function fromArray(array $data): Version { + if (!isset($data['commitSha'], $data['commitDate'], $data['pkgVersion'])) { + throw new \InvalidArgumentException('Missing required keys in version data'); + } + + try { + $commitDate = new \DateTimeImmutable($data['commitDate']); + } catch (\Exception $e) { + throw new \InvalidArgumentException('Invalid commitDate format: ' . $e->getMessage(), 0, $e); + } + return new self( $data['commitSha'], - new \DateTimeImmutable($data['commitDate']), + $commitDate, $data['pkgVersion'], ); }🤖 Prompt for AI Agents
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.
Static analysis is for that. It's a soft contract
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.
@norkunas, understood! I see this aligns with the project's established approach where docblock type annotations serve as contracts for static analysis tools. The method signature already documents the expected array structure with
non-empty-stringtypes, which is sufficient for static analysis to catch issues.Thank you for the clarification!
🧠 Learnings used