{"id":13620330,"url":"https://github.com/php-strictus/strictus","last_synced_at":"2025-04-06T07:13:35.372Z","repository":{"id":65708373,"uuid":"597686812","full_name":"php-strictus/strictus","owner":"php-strictus","description":"Strict Typing on inline variables for PHP","archived":false,"fork":false,"pushed_at":"2023-06-28T12:28:06.000Z","size":318,"stargazers_count":159,"open_issues_count":0,"forks_count":9,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-05-20T19:11:11.463Z","etag":null,"topics":["php","php8","strict-types","types"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-strictus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["WendellAdriel"],"custom":["https://www.paypal.me/wendelladriel"]}},"created_at":"2023-02-05T10:27:59.000Z","updated_at":"2024-04-30T07:00:26.000Z","dependencies_parsed_at":"2024-01-14T08:06:47.282Z","dependency_job_id":null,"html_url":"https://github.com/php-strictus/strictus","commit_stats":{"total_commits":18,"total_committers":3,"mean_commits":6.0,"dds":0.5,"last_synced_commit":"92d190c03456343a1a3027ca7ff92080deebd7c5"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-strictus%2Fstrictus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-strictus%2Fstrictus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-strictus%2Fstrictus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-strictus%2Fstrictus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-strictus","download_url":"https://codeload.github.com/php-strictus/strictus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445671,"owners_count":20939958,"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":["php","php8","strict-types","types"],"created_at":"2024-08-01T21:00:54.578Z","updated_at":"2025-04-06T07:13:35.351Z","avatar_url":"https://github.com/php-strictus.png","language":"PHP","funding_links":["https://github.com/sponsors/WendellAdriel","https://www.paypal.me/wendelladriel"],"categories":["PHP"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/php-strictus/strictus/raw/main/art/logo.png\" alt=\"Strictus\" width=\"400\"/\u003e\n    \u003ch1\u003eStrictus\u003c/h1\u003e\n    \u003ch4\u003eStrict Typing for local variables in PHP\u003c/h4\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003ca href=\"https://packagist.org/packages/strictus/strictus\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/strictus/strictus.svg?style=flat-square\" alt=\"Packagist\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/strictus/strictus\"\u003e\u003cimg src=\"https://img.shields.io/packagist/php-v/strictus/strictus.svg?style=flat-square\" alt=\"PHP from Packagist\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/php-strictus/strictus/actions\"\u003e\u003cimg alt=\"GitHub Workflow Status (main)\" src=\"https://img.shields.io/github/actions/workflow/status/php-strictus/strictus/tests.yml?branch=main\u0026label=Tests\"\u003e \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003ca href=\"#introduction\"\u003eIntroduction\u003c/a\u003e |\n    \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e |\n    \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e |\n    \u003ca href=\"#credits\"\u003eCredits\u003c/a\u003e |\n    \u003ca href=\"#contributing\"\u003eContributing\u003c/a\u003e\n\u003c/div\u003e\n\n## Introduction\n\nStrictus brings **strict typing** for local variables into PHP.\n\nWith Strictus, you can control the types of local variables using different patterns.\n\n---\n\n`💣` **The problem:**\n\nPHP has no support for strongly typed Local Variables.\n\nHere is an illustrative example of a basic mistake:\n\n```php\n\u003c?php \n//Rule: Active discount of 10% or 25% for orders from $50\n\n$total = 82.50;\n$discount = 0.10; //float\n\nif ($total \u003e= 50) {\n    $discount = '25%'; //replacing a float value with string value 🤦🏻‍♂️\n}\n\n$total = $total - ($total * $discount); //💥 Error: A float cannot be multiplied by a string\n```\n\nIn the code above, nothing prevents overriding the `float` value of `$discount` a `string` value, causing a bug.\n\n\u003cbr/\u003e\n\n`👍` **The solution:**  \n\nLet's rewrite the previous example using Strictus and strongly typed variables:\n\n```php\n\u003c?php \n//Rule: Active discount of 10% or 25% for orders from $50\n\nuse Strictus\\Strictus;\n\n$total = Strictus::float(82.50); \n$discount = Strictus::float(0.10);\n\nif ($total() \u003e= 50) {\n    $discount(0.25); //updates the $discount value\n}\n\n$total($total() - ($total() * $discount()));\n\necho $total(); //61.875\n```\n\nIn the code above, the variable `$discount` is an instance of `StrictusFloat::class` and  it only accepts `float` values.\n\nAn Exception `StrictusTypeException` is thrown when we try to assign anything that is not of type `float`, like a `string` for example.\n\nSee this example:\n\n```php\n\u003c?php \nuse Strictus\\Strictus;\n\n$discount = Strictus::float(0.10);\n$discount('25%'); //A StrictusTypeException stops the code execution\n```\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require strictus/strictus\n```\n\nRequires: PHP 8.1+\n\n## Usage\n\nThere are a few different patterns you can use to work `Strictus`.\n\n### Creating Your Variables\n\nTo create a variable, simply call `Strictus::*method*()`, replacing the `*method*` with the type you want to enforce.\n\nFor example:\n\n```php\n\u003c?php\nuse Strictus\\Strictus;\n\n//creates a string\n$name = Strictus::string('Wendell');\n\n //creates a nullable string\n$comment = Strictus::nullableString(null);\n\n //creates an int\n$score = Strictus::int(100);\n\n//creates a boolean\n$isActive = Strictus::boolean(true);\n\n//creates an array\n$authors = Strictus::array(['Wendell', 'Christopher']);\n\n//creates an object\n$person = Strictus::object((object) ['name' =\u003e 'Wendell', 'country' =\u003e 'BR']);\n\n//instantiates a class\n$calculator = Strictus::instance(CalculatorClass::class, new CalculatorClass());\n\n//instantiates an enum\n$role = Strictus::enum(Role::class, Role::CONTRIBUTOR);\n\nclass CalculatorClass\n{\n    //...\n}\n\nenum Role\n{\n    case CONTRIBUTOR;\n}\n```\n\n💡 Check out all the available [variable methods](#variable-methods).\n\n### Getting Variable Value\n\nTo retrieve the variable value, just call it like a function:\n\n```php\necho $name(); //Wendell\n\necho $score() - 10; //90\n\nif ($isActive()) {\n    //do your logic here\n}\n\necho implode($authors(), ';'); //Wendell;Christopher\n```\n\nAlternatively, you can use the `$variable` like a Value Object:\n\n```php\n$name = Strictus::string('Christopher'); //creates a string\n\necho $name-\u003evalue; //Christopher\n```\n\n### Update Variable Value\n\nTo update the variable value, call it like a function passing the new value as the argument:\n\n```php\n$score = Strictus::int(100);\n\n$score($score() - 20); //updates $score\n\necho $score(); //80\n```\n\nAlternatively, you can use the `$variable` like a Value Object:\n\n```php\n$score = Strictus::int(100);\n\n$score-\u003evalue = 0;\n\necho $score(); //0\n```\n\n### Variable methods\n\nYou can use the following methods to create single type variables:\n\n| Type       | Nullable | Method                                            |\n|------------|----------|---------------------------------------------------|\n| String     | No       | Strictus::string($value)                          |\n| String     | Yes      | Strictus::string($value, true)                    |\n| String     | Yes      | Strictus::nullableString($value)                  |\n| Integer    | No       | Strictus::int($value)                             |\n| Integer    | Yes      | Strictus::int($value, true)                       |\n| Integer    | Yes      | Strictus::nullableInt($value)                     |\n| Float      | No       | Strictus::float($value)                           |\n| Float      | Yes      | Strictus::float($value, true)                     |\n| Float      | Yes      | Strictus::nullableFloat($value, true)             |\n| Boolean    | No       | Strictus::boolean($value)                         |\n| Boolean    | Yes      | Strictus::boolean($value, true)                   |\n| Boolean    | Yes      | Strictus::nullableBoolean($value)                 |\n| Array      | No       | Strictus::array($value)                           |\n| Array      | Yes      | Strictus::array($value, true)                     |\n| Array      | Yes      | Strictus::nullableArray($value)                   |\n| Object     | No       | Strictus::object($value)                          |\n| Object     | Yes      | Strictus::object($value, true)                    |\n| Object     | Yes      | Strictus::nullableObject($value)                  |\n| Class Type | No       | Strictus::instance($instanceType, $value)         |\n| Class Type | Yes      | Strictus::instance($instanceType, $value, true)   |\n| Class Type | Yes      | Strictus::nullableInstance($instanceType, $value) |\n| Enum Type  | No       | Strictus::enum($enumType, $value)                 |\n| Enum Type  | Yes      | Strictus::enum($enumType, $value, true)           |\n| Enum Type  | Yes      | Strictus::nullableEnum($enumType, $value)         |\n\n### Union Types\n\n`Strictus` also supports union types:\n\n```php\nuse Strictus\\Enums\\Type;\n\n$unionTypesVariable = Strictus::union([Type::INT, Type::STRING], 'foo');\n\necho $unionTypesVariable-\u003evalue; //foo\n\necho $unionTypesVariable(); //foo\n\n// Update variable\n\n$unionTypesVariable-\u003evalue = 100;\n\necho $unionTypesVariable-\u003evalue; //100\n\n// Thrown an exception if the value is wrong union types\n\n$unionTypesVariable-\u003evalue = false; //StrictusTypeException\n```\n\n### Immutable Variables\n\nIf you want to create immutable variables, you can use the `-\u003eimmutable()` method. If you try to assign a new value\nto an Immutable Variable, an `Strictus\\Exceptions\\ImmutableStrictusException` exception will be thrown:\n\n```php\n$immutableScore = Strictus::int(100)-\u003eimmutable();\n\n$immutableScore(50); //ImmutableStrictusException\n$immutableScore-\u003evalue = 50; //ImmutableStrictusException\n```\n\n### Error Handling\n\nIf you try to assign a value that doesn't match the type of the created variable, an\n`Strictus\\Exceptions\\StrictusTypeException` exception will be thrown:\n\n```php\n$score = Strictus::int(100);\n\n$score('one hundred'); //StrictusTypeException\n$score-\u003evalue = false; //StrictusTypeException\n```\n\nIf you try to assign a new value to an Immutable Variable, an\n`Strictus\\Exceptions\\ImmutableStrictusException` exception will be thrown:\n\n```php\n$immutableScore = Strictus::int(100)-\u003eimmutable();\n\n$immutableScore(50); //ImmutableStrictusException\n$immutableScore-\u003evalue = 50; //ImmutableStrictusException\n```\n\n## Motivation\n\nFollowing a discussion on Twitter between **[Christopher Miller](https://twitter.com/ccmiller2018)** and\n**[Wendell Adriel](https://twitter.com/wendell_adriel)** around the lack of strongly typed local variables\nfor PHP we quickly decided a package was the right approach whilst we could get an RFC into the core.\n\n## Credits\n\n- [Christopher Miller](https://github.com/chrisjumptwentyfour)\n- [Wendell Adriel](https://github.com/WendellAdriel)\n- [All Contributors](../../contributors)\n\n## Contributing\n\nWe welcome contributions!\n\nPlease visit the **[Contributing Guide](CONTRIBUTING.md)** to learn more about contributing to Strictus.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-strictus%2Fstrictus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-strictus%2Fstrictus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-strictus%2Fstrictus/lists"}