SizeTrait::getValueSize()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 10.3696

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 12
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 19
ccs 2
cts 3
cp 0.6667
crap 10.3696
rs 8.4444
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Traits;
13
14
use Countable;
15
use Rakit\Validation\Rules\Traits\SizeTrait as TraitsSizeTrait;
16
17
trait SizeTrait
18
{
19
    use TraitsSizeTrait;
20
    use FileTrait;
21
22
    /**
23
     * Get size (int) value from given $value
24
     *
25
     * @param mixed $value
26
     *
27
     * @return false|float
28
     */
29
    protected function getValueSize($value)
30
    {
31
        if (null !== $attribute = $this->getAttribute()) {
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        if (null !== $attribute = $this->/** @scrutinizer ignore-call */ getAttribute()) {
Loading history...
32
            if (is_numeric($value) && ($attribute->hasRule('integer') || $attribute->hasRule('numeric') || $attribute->hasRule('decimal'))) {
33 2
                $value = (float) $value;
34
            }
35
        }
36
37
        $size = match (true) {
38
            is_int($value) || is_float($value) => $value,
39
            is_string($value)                  => mb_strlen($value, 'UTF-8'),
40
            $this->isUploadedFileValue($value) => $value['size'],
41
            is_array($value)                   => count($value),
42
            $this->isValidFileInstance($value) => $value->getSize() / 1024,
43
            $value instanceof Countable        => $value->count(),
44
            default                            => false
45
        };
46
47 4
        return $size === false ? false : (float) $size;
48
    }
49
}
50