for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Larapie\Actions\Concerns;
use Larapie\Actions\Attribute;
trait ResolveCasting
{
protected function resolveAttributeCasting(array $data)
return collect($this->rules())
rules()
If this is a false-positive, you can also ignore this issue in your code via the ignore-call annotation
ignore-call
return collect($this->/** @scrutinizer ignore-call */ rules())
->filter(function ($rule) {
return $rule instanceof Attribute;
})
->intersectByKeys($data)->map(function (Attribute $attribute, $key) use ($data) {
return $this->processCasting($attribute, $data[$key]);
})->toArray();
}
private function processCasting(Attribute $attribute, $value)
if ($attribute->isNullable() && $value === null) {
return $value;
if (($castFunction = $attribute->getCast()) !== null && is_callable($castFunction)) {
return $castFunction($value);
if ((($type = $attribute->getCast()) !== null && is_string($type)) ||
(is_string($type = $attribute->cast(null)) && in_array(strtolower($type), ['bool', 'boolean', 'string', 'double', 'float', 'int', 'integer', 'array', 'object']))) {
return $this->castFromString($type, $value);
if (! ($attribute->cast(null) instanceof Attribute)) {
$attribute->cast(null)
Larapie\Actions\Attribute
return $attribute->cast($value);
private function castFromString(string $type, $value)
switch (strtolower($type)) {
case 'boolean':
case 'bool':
return (bool) $value;
case 'string':
return (string) $value;
case 'double':
return (float) $value;
case 'integer':
case 'int':
return (int) $value;
case 'float':
case 'array':
return (array) $value;
case 'object':
return (object) $value;
default:
throw new \RuntimeException('cast type not supported');