SmallerThan   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A applicableToTypes() 0 3 1
A isValid() 0 11 3
A __construct() 0 1 1
1
<?php
2
declare(strict_types=1);
3
4
namespace PrinsFrank\PhpStrictModels\Rule;
5
6
use Attribute;
7
use PrinsFrank\PhpStrictModels\Enum\Type;
8
9
#[Attribute]
10
class SmallerThan implements Rule
11
{
12 1
    public function __construct(private float|int $smallerThan){}
13
14 1
    public function applicableToTypes(): array
15
    {
16 1
        return [Type::float, Type::int, Type::array, Type::string];
17
    }
18
19
    /**
20
     * @param int|float|string|mixed[] $value
21
     */
22 1
    public function isValid(mixed $value): bool
23
    {
24 1
        if (is_array($value)) {
25 1
            return count($value) < $this->smallerThan;
26
        }
27
28 1
        if (is_string($value)) {
29 1
            return mb_strlen($value) < $this->smallerThan;
30
        }
31
32 1
        return $value < $this->smallerThan;
33
    }
34
35 1
    public function getMessage(): string
36
    {
37 1
        return 'Should be smaller than ' . $this->smallerThan;
38
    }
39
}
40