- Illogical type casting (
PHP
's native implementation is way too "smart") - Pointless casts like
array => float
are allowed - Boilerplate code to work with arrays (check if
isset()
, throw an exception, cast the type, etc.)
Consider an example:
$userId = $queryParams['userId'] ?? null;
if ($userId === null) {
throw ...
}
$userId = (int)$userId;
$userId = (new Mess($queryParams))['userId']->getAsInt();
You can mess with API responses/configs/whatever:
$mess = new Mess($response);
$book = new Book(
$mess['title']->getString(),
$mess['isBestseller']->getBool(),
$mess['stats']['rating']->getInt(),
$mess['tags']->getListOfString()
);
getListOfString()
getListOfInt()
getArrayOfStringToString()
getArrayOfStringToBool()
- etc.
$ composer require zakirullin/mess
$queryParams = new Mess(['isDeleted' => 'true']);
$queryParams['isDeleted']->getBool(); // UnexpectedTypeException
$queryParams['isDeleted']->getAsBool(); // true
$value = new Mess('25');
$value->getInt(); // UnexpectedTypeException
$value->getAsInt(); // 25
$value->getString(); // '25'
$value = new Mess('25a');
$value->getInt(); // UnexpectedTypeException
$value->getAsInt(); // UncastableValueException
$config = new Mess(['param' => '1']);
$config['a']['b']->getInt(); // MissingKeyException: "MissingKeyException: a.b"
$config['a']['b']->findInt(); // null
$config['param']->getInt(); // UnexpectedTypeException
$config['param']->getAsInt(); // 1
$config['param']->findInt(); // UnexpectedTypeException
$config['param']->findAsInt(); // 1
As you might notice, type casting is performed while using (find|get)As*
methods.
Having trouble grasping get*()
/find*()
? Check out brilliant Ocramius's slides.
'\d+' => int // OK
'buzz12' => int // UncastableValueException
bool => int // UncastableValueException
array => int // UncastableValueException
object => int // UncastableValueException
resource => int // UncastableValueException
Fairly simple, isn't it? Let us fail fast!
Let's imagine a library that is configured this way:
$config = [
'retries' => 5, // int
'delay' => 20, // int
]
// Initialization
$retries = $config['retries'] ?? null;
if ($retries === null) {
throw new MissingConfigKeyException(...);
}
...
$retries = (int)$retries;
$delay = (int)$delay;
Client-side code:
$config => [
'retries' => [5, 10, 30], // (int)array => 1
'delay' => true, // (int)bool => 1
]
No matter if that's a misuse, or a result of major update: The system will continue to work.
And that's the worst thing about it. It will continue to work, though, not in a way it was supposed to work.
PHP
is trying to do its best to let it work at least somehow.
- Deserialized data
- Request
body
/query
API
response- Config
- etc.