{ "uuid": [ { "value": "edacb021-6f94-4f6f-a39b-4362609caea0" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:34+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "PHP types and assertions\n" } ], "created": [ { "value": "2023-08-20T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:34+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2023\/08\/20\/php-types-and-assertions", "langcode": "en" } ], "body": [ { "value": "\n

Following yesterday's email about input validation, guard clauses and assertion libraries, these can be used to compliment PHP's native types and checking.<\/p>\n\n

For example:<\/p>\n\n

function createJourney(string $from, string $to, int $duration): void {\n  var_dump($from, $to, $duration);\n}\n<\/code><\/pre>\n\n

In this code, each parameter has a type, but there's no validation on the values.<\/p>\n\n

If I run this:<\/p>\n\n

createJourney('', '', -10);\n<\/code><\/pre>\n\n

I would get this output:<\/p>\n\n

string(0) \"\"\nstring(0) \"\"\nint(-10)\n<\/code><\/pre>\n\n

This is probably not what you want.<\/p>\n\n

I expect $to<\/code> and $from<\/code> to be not empty and the duration to be greater than zero.<\/p>\n\n

Here's the thing<\/h2>\n\n

I can use an assertion library or throw my own Exceptions if the values pass the type checks but aren't what I need.<\/p>\n\n

For example:<\/p>\n\n

function createJourney(string $from, string $to, int $duration): void {\n  Assert::stringNotEmpty($from);\n  Assert::stringNotEmpty($to);\n  Assert::positiveInteger($duration);\n\n  var_dump($from, $to, $duration);\n}\n<\/code><\/pre>\n\n

Now, if an empty string or negative duration is passed - in my implementation or test code - an Exception will be thrown.<\/p>\n\n ", "format": "full_html", "processed": "\n

Following yesterday's email about input validation, guard clauses and assertion libraries, these can be used to compliment PHP's native types and checking.<\/p>\n\n

For example:<\/p>\n\n

function createJourney(string $from, string $to, int $duration): void {\n  var_dump($from, $to, $duration);\n}\n<\/code><\/pre>\n\n

In this code, each parameter has a type, but there's no validation on the values.<\/p>\n\n

If I run this:<\/p>\n\n

createJourney('', '', -10);\n<\/code><\/pre>\n\n

I would get this output:<\/p>\n\n

string(0) \"\"\nstring(0) \"\"\nint(-10)\n<\/code><\/pre>\n\n

This is probably not what you want.<\/p>\n\n

I expect $to<\/code> and $from<\/code> to be not empty and the duration to be greater than zero.<\/p>\n\n

Here's the thing<\/h2>\n\n

I can use an assertion library or throw my own Exceptions if the values pass the type checks but aren't what I need.<\/p>\n\n

For example:<\/p>\n\n

function createJourney(string $from, string $to, int $duration): void {\n  Assert::stringNotEmpty($from);\n  Assert::stringNotEmpty($to);\n  Assert::positiveInteger($duration);\n\n  var_dump($from, $to, $duration);\n}\n<\/code><\/pre>\n\n

Now, if an empty string or negative duration is passed - in my implementation or test code - an Exception will be thrown.<\/p>\n\n ", "summary": null } ], "feeds_item": [ { "imported": "1970-01-01T00:33:45+00:00", "guid": null, "hash": "7a9179502a50a3f6f3443cdebef1a615", "target_type": "feeds_feed", "target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76" } ] }