{"id":16417823,"url":"https://github.com/yceruto/option-type","last_synced_at":"2025-07-18T20:36:38.827Z","repository":{"id":234225036,"uuid":"788451377","full_name":"yceruto/option-type","owner":"yceruto","description":"The Option type for PHP","archived":false,"fork":false,"pushed_at":"2024-11-27T13:18:42.000Z","size":69,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-06T00:33:07.750Z","etag":null,"topics":["null-safety","option-type","optional-type","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yceruto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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,"zenodo":null},"funding":{"github":["yceruto"]}},"created_at":"2024-04-18T12:49:59.000Z","updated_at":"2025-06-19T11:48:38.000Z","dependencies_parsed_at":"2024-04-18T15:17:43.766Z","dependency_job_id":"b1e4ca7d-971b-4c5f-92c7-aab45d6db134","html_url":"https://github.com/yceruto/option-type","commit_stats":null,"previous_names":["yceruto/option-type"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/yceruto/option-type","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Foption-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Foption-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Foption-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Foption-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yceruto","download_url":"https://codeload.github.com/yceruto/option-type/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yceruto%2Foption-type/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265829169,"owners_count":23835090,"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":["null-safety","option-type","optional-type","php"],"created_at":"2024-10-11T07:12:22.728Z","updated_at":"2025-07-18T20:36:38.800Z","avatar_url":"https://github.com/yceruto.png","language":"PHP","funding_links":["https://github.com/sponsors/yceruto"],"categories":[],"sub_categories":[],"readme":"# PHP Option type\n\n[![Latest Stable Version](https://poser.pugx.org/yceruto/option-type/v?v=1)](https://packagist.org/packages/yceruto/option-type)\n[![Unstable](http://poser.pugx.org/yceruto/option-type/v/unstable)](https://packagist.org/packages/yceruto/option-type)\n[![License](https://poser.pugx.org/yceruto/option-type/license)](https://packagist.org/packages/yceruto/option-type)\n[![PHP Version Require](https://poser.pugx.org/yceruto/option-type/require/php)](https://packagist.org/packages/yceruto/option-type)\n\nThe `Option` type represents a value that might or might not be there. It's all about\nnull safety in PHP!\n\n\u003e [!NOTE]\n\u003e Inspired by [Rust's Option type](https://doc.rust-lang.org/std/option/) and other \n\u003e languages like Scala, Swift, F#, etc.\n\n## Installation\n\n```bash\ncomposer require yceruto/option-type\n```\n\n## Handling the presence or absence of a value with `null`\n\nIn PHP, denoting the absence of a value is done with `null`, e.g. when a `divide`\nfunction returns `null` if the divisor is `0`.\n\n```php\nfunction divide(int $dividend, int $divisor): ?int\n{\n    if (0 === $divisor) {\n        return null;\n    }\n\n    return intdiv($dividend, $divisor);\n}\n\nfunction success(int $result): string {\n    return sprintf('Result: %d', $result);\n}\n\n$result = divide(10, 2);\n\necho success($result);\n```\n\nCan you spot the issue in this code? Apparently, everything is fine until you try to\ndivide by zero. The function will return `null`, and the `success()` function will throw\na `TypeError` because it expects an `int` value, not `null`.\n\nThe issue with this approach is that it's too easy to overlook checking if the value is \n`null`, leading to runtime errors, and this is where the `Option` type comes in handy: it \nalways forces you to deal with the `null` case.\n\n## Handling the presence or absence of a value with `Option`\n\nOptions often work with pattern matching to check if there’s a value and act accordingly, \nalways making sure to handle the `null` case.\n\n```php\nuse Std\\Type\\Option;\nuse function Std\\Type\\Option\\none;\nuse function Std\\Type\\Option\\some;\n\n/**\n * @return Option\u003cint\u003e\n */\nfunction divide(int $dividend, int $divisor): Option\n{\n    if (0 === $divisor) {\n        return none();\n    }\n\n    return some(intdiv($dividend, $divisor));\n}\n\nfunction success(int $result): string {\n    return sprintf('Result: %d', $result);\n}\n\n// The return value of the function is an Option\n$result = divide(10, 2);\n\n// Pattern match to retrieve the value\necho $result-\u003ematch(\n    // The division was valid\n    some: fn (int $v) =\u003e success($v),\n    // The division was invalid\n    none: fn () =\u003e 'Division by zero!',\n);\n```\n\n\u003e [!TIP]\n\u003eYou can use the functions `some()` and `none()` as quick ways to create an `Option` \n\u003einstance. `some()` is just like `new Some()`, meaning it includes a value, while \n\u003e`none()` is the same as `new None()`, indicating it is missing a value.\n\n## Documentation\n\n * [API Reference](docs/api_reference.md)\n * [Examples](docs/examples.md)\n\n## License\n\nThis software is published under the [MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyceruto%2Foption-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyceruto%2Foption-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyceruto%2Foption-type/lists"}