{"id":26778257,"url":"https://github.com/andreamancuso/invariant-php","last_synced_at":"2026-03-06T22:03:08.838Z","repository":{"id":283359130,"uuid":"950303748","full_name":"andreamancuso/invariant-php","owner":"andreamancuso","description":"Native PHP extension for Design by Contract. Enforces class invariants with minimal runtime overhead.","archived":false,"fork":false,"pushed_at":"2025-03-19T21:35:10.000Z","size":53,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-12-13T09:51:40.260Z","etag":null,"topics":["dbc","designbycontract","php","php-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreamancuso.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,"zenodo":null}},"created_at":"2025-03-18T00:30:32.000Z","updated_at":"2025-04-08T20:35:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"44e99230-aeba-4e9c-813e-e573cb42a2ef","html_url":"https://github.com/andreamancuso/invariant-php","commit_stats":null,"previous_names":["andreamancuso/invariant-php"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andreamancuso/invariant-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreamancuso%2Finvariant-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreamancuso%2Finvariant-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreamancuso%2Finvariant-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreamancuso%2Finvariant-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreamancuso","download_url":"https://codeload.github.com/andreamancuso/invariant-php/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreamancuso%2Finvariant-php/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30200756,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T19:07:06.838Z","status":"ssl_error","status_checked_at":"2026-03-06T18:57:34.882Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["dbc","designbycontract","php","php-extension"],"created_at":"2025-03-29T05:36:42.744Z","updated_at":"2026-03-06T22:03:08.821Z","avatar_url":"https://github.com/andreamancuso.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Invariant PHP\n\n**Invariant PHP** is a native PHP extension that brings [Design by Contract (DbC)](https://en.wikipedia.org/wiki/Design_by_contract) principles to PHP. It **automatically enforces class invariants** _and ensures all typed properties are initialized_ without relying on reflection.\n\n## License\n\nReleased under the [PHP License 3.01](https://www.php.net/license/3_01.txt).\n\n## Features\n\n- **Class Invariants (`__invariant()`)** – Ensures an object's state remains valid **before and after** each method call.\n- **Enforced Initialization** – Detects **uninitialized typed properties** and raises a **fatal error** before `__invariant()` runs.\n- **Native PHP Extension** – Written in C, avoiding overhead from reflection or AOP.\n- **Transparent Execution** – Just define `__invariant()` and you are good to go.\n\n## Why?\n\nPHP lacks built-in support for **Design by Contract**. Existing solutions (like PHPDeal) rely on **runtime reflection** or **AOP hacks**, which can be slow or brittle. **Invariant PHP** hooks directly into the **Zend Engine**.\n\n## Example Usage\n```php\n\u003c?php\n\nclass BankAccount {\n    // Typed properties must be initialized or you'll get a fatal error!\n    private int $balance;\n\n    public function __construct(int $balance) {\n        $this-\u003ebalance = $balance;\n    }\n\n    public function withdraw(int $amount) {\n        $this-\u003ebalance -= $amount;\n    }\n\n    public function __invariant(): void {\n        if ($this-\u003ebalance \u003c 0) {\n            throw new \\LogicException(\"Invariant failed: Balance cannot be negative.\");\n        }\n    }\n}\n\n$account = new BankAccount(100);\n$account-\u003ewithdraw(50); // ✅ Works fine\n$account-\u003ewithdraw(100); // ❌ Fatal error or LogicException if below zero\n```\n\n## Limitations\n\nRight now, Invariant PHP doesn’t explicitly detect or prevent invariant loops, so if an `__invariant()` method calls another method that triggers `__invariant()` again, it could lead to unintended recursion.\n\nI plan to address this by:\n\n- Tracking active invariant checks per object to detect re-entrant calls.\n- Throwing an error or skipping nested invariant checks when recursion is detected.\n- Ensuring performance remains unaffected in non-recursive cases.\n\n## Installation\n\nCurrently, you must **build from source**:\n```sh\nphpize\n./configure\nmake -j$(nproc)\nsudo make install\n```\nThen enable it in `php.ini`:\n```ini\nextension=invariant_php.so\n```\n\n## **How It Works**\n\n- **Hooks the Zend Engine** by overriding `zend_execute_ex`.\n- **Checks typed properties** for initialization before calling `__invariant()`.\n- **Throws a fatal error** if any typed property is uninitialized.\n- **Automatically calls `__invariant()`** before \u0026 after each method execution.\n\n## **Roadmap**\n\n- **Support `requires()` (preconditions)**.\n- **Support `ensures()` (postconditions)**.\n- **Optimization \u0026 caching** for better performance.\n- **PECL package for easy installation**.\n\n---\n\n**Want to contribute?** PRs and discussions are welcome! 🎯\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreamancuso%2Finvariant-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreamancuso%2Finvariant-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreamancuso%2Finvariant-php/lists"}