{"id":13616859,"url":"https://github.com/hamlet-framework/type","last_synced_at":"2026-01-11T16:50:47.166Z","repository":{"id":62514324,"uuid":"184194352","full_name":"hamlet-framework/type","owner":"hamlet-framework","description":"Hamlet Framework / Type","archived":false,"fork":false,"pushed_at":"2024-02-06T02:31:37.000Z","size":297,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T05:58:13.463Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hamlet-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-30T04:59:02.000Z","updated_at":"2022-01-05T01:50:20.000Z","dependencies_parsed_at":"2024-02-05T00:25:28.854Z","dependency_job_id":"64152ddb-874e-4654-8880-8061a8a3cc2f","html_url":"https://github.com/hamlet-framework/type","commit_stats":{"total_commits":151,"total_committers":2,"mean_commits":75.5,"dds":"0.16556291390728473","last_synced_commit":"45b0e0cc8b235e84cd7bd05f201e6a87d556ae56"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamlet-framework%2Ftype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamlet-framework%2Ftype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamlet-framework%2Ftype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamlet-framework%2Ftype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hamlet-framework","download_url":"https://codeload.github.com/hamlet-framework/type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248815581,"owners_count":21165951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:01:34.197Z","updated_at":"2025-04-14T03:31:43.460Z","avatar_url":"https://github.com/hamlet-framework.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"Hamlet Type \n===\n\n![CI Status](https://github.com/hamlet-framework/type/workflows/CI/badge.svg?branch=master\u0026event=push)\n[![Packagist](https://img.shields.io/packagist/v/hamlet-framework/type.svg)](https://packagist.org/packages/hamlet-framework/type)\n[![Packagist](https://img.shields.io/packagist/dt/hamlet-framework/type.svg)](https://packagist.org/packages/hamlet-framework/type)\n[![Coverage Status](https://coveralls.io/repos/github/hamlet-framework/type/badge.svg?branch=master)](https://coveralls.io/github/hamlet-framework/type?branch=master)\n![Psalm coverage](https://shepherd.dev/github/hamlet-framework/type/coverage.svg?)\n\nThere are few aspects of specifying type of expression in PHP:\n\n1. The most exact specification of the type (we assume it's in psalm syntax): `array\u003cint,DateTime|null\u003e`\n2. The sequence of run time assertions: `assert($records instanceof array\u003cint,DateTime|null\u003e)`\n3. The type hint for static analysers (which is _psalm_ at the moment): `(array\u003cint,DateTime|null\u003e) $records`\n4. The ability to derive types without string manipulation: `array\u003cint,DateTime|null\u003e || null`\n5. The ability to cast when it's safe, i.e. falsey should cast to false, etc.\n\nThis library provides the basic building blocks for type specifications. For example, the following expression:\n\n```php\n$type = _map(\n  _int(), \n  _union(\n    _class(DateTime::class), \n    _null()\n  )\n)\n```\n\ncreates an object of type `Type\u003carray\u003cint,DateTime|null\u003e\u003e`.\n\nAsserting at run time, that the type of `$records` is `array\u003cint,DateTime|null\u003e\u003e`:\n```php\n$type-\u003eassert($records);\n```\n\nCast `$records` to `array\u003cint,DateTime|null\u003e\u003e` and throw an exception when `$records` cannot be cast to `array\u003cint,DateTime|null\u003e\u003e`:\n```php\nreturn $type-\u003ecast($records);\n```\n\nCombine type with other types, for example, making it nullable `array\u003cint,DateTime|null\u003e\u003e|null`:\n```php\n_union($type, _null())\n```\n\n## Object like arrays\n\nObject like arrays require more leg work. For example the type `array{id:int,name:string,valid?:bool}` \ncorresponds to this construct:\n\n```php\n/** @var Type\u003carray{id:int,name:string,valid?:bool}\u003e */\n$type = _object_like([\n    'id'     =\u003e _int(),\n    'name'   =\u003e _string(),\n    'valid?' =\u003e _bool()\n]);\n``` \n\nQuite a mouthful. Also, note the required `@var` as psalm currently have no support for dependent types. \n\nIf you want to assert types matching your PHPDoc you can use the type parser (WiP):\n\n```php\n/** @var Type\u003carray{id:int}\u003e */\n$type = Type::of('array{id:int}');\n\nassert($type-\u003ematches($record));\n```\n\n## Background\n\n- Move completely to PHPStan parsed including docblock\n- Add union and intersection types \n- Support for enums\n- Support for non-empty-* types\n- Support for tuples as int `array{int,string}`\n- Support for iterable|self|static|class-string\n- Better support for callables\n- Add PHPStan to QA pipeline","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamlet-framework%2Ftype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhamlet-framework%2Ftype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamlet-framework%2Ftype/lists"}