Download the PHP package catpaw/unsafe without Composer
On this page you can find all versions of the php package catpaw/unsafe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download catpaw/unsafe
More information about catpaw/unsafe
Files in catpaw/unsafe
Informations about the package unsafe
Install with
Control flow is king
I am of the opinion that control flow is one of the most important things to deal with as a programmer, it affects my thinking and at times it actually guides my problem solving process.
Managing errors should not break the flow in which I control my program, I shouldn't have to jump up and down around my file to catch a new exception introduced by a new function I just invoked 20 lines above.
Try/Catch
I found myself relying way too much on code like this
or even
The last one might make sense in theory, but in practice those exceptions might each mean something different, a different cause for an error.
The reality is that very often I lump those exceptions in together because I forget to manage them or because for some reason at 4 AM I decide on the spot "yes, I should let my IDE dictate my error management".
Try/catch error handling has been (probably) the most popular way to manage errors in php, and I think it still is a valid way of dealing with errors in a global scope.
I can't argue there is something nice about having one centralized place to manage all errors, but I don't want to be forced to approach error management all the time in that manner.
If you're anything like me you might prefer managing your error inline, directly at the source, so that you deal with it when it pops up and then you don't have to think about it anymore.
Unsafe
I have a solution.
Do not throw exceptions in your code, instead return your errors as Unsafe
Use the ok() and error() functions to create Unsafe
ok()
Return ok($value) whenever there are no errors in your program.
This function will create a new Unsafe
error()
Return error($error) whenever you encounter an error in your program and want to propagate it upstream.
This function will create a new Unsafe
Example
The following example tries to read the contents of a file while managing errors.
First I'm declaring all entities involved, classes and functions.
then
- open a file
- read its contents
- close the file
This code will print the contents of file.txt
if all operations succeed.
Each time ->try($error)
is invoked the Unsafe object tries to unwrap its value.\
If the Unsafe object contains an error, the value returned by ->try($error)
resolves to null
and the variable $error
is assigned the contained error by reference.
anyError()
You can use anyError() to deal away with the repetitive snippet
Here's the same example but written using anyError()
The anyError() function takes a generator function and it consumes it step by step.
When the generator function yield
s an Error or an Unsafe
Effectively, or yield $error
acts like
On the other hand, if the result of ->try()
is valid, the or <expression>
is not executed and the generator keeps running until it reaches the next yield error
statement, the next return
statement or until the generator is consumed.
Matching
Since errors are results, you can actually match()
them
or apply any sort of expression that you want inline.