LessThanOrEqual::decorate()   B
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 16
nop 4
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Er1z\FakeMock\Decorator\AssertDecorator;
4
5
use Er1z\FakeMock\Accessor;
6
use Er1z\FakeMock\Metadata\FieldMetadata;
7
use phpDocumentor\Reflection\Types\Float_;
8
use Symfony\Component\Validator\Constraint;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Constraint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class LessThanOrEqual implements AssertDecoratorInterface
11
{
12 36
    public function decorate(&$value, FieldMetadata $field, Constraint $configuration, ?string $group = null): bool
13
    {
14 36
        if (!is_null($value) && !is_numeric($value)) {
15 6
            throw new \InvalidArgumentException('Non-numeric value supplied');
16
        }
17
18 30
        $trailer = 0;
19 30
        if ($this->useTrailer()) {
20 15
            $trailer = $field->type instanceof Float_ ? 0.00001 : 1;
21
        }
22
23
        /*
24
         * @var \Symfony\Component\Validator\Constraints\LessThan|\Symfony\Component\Validator\Constraints\LessThanOrEqual
25
         */
26 30
        if ($configuration->value && $value >= $configuration->value) {
27 12
            $value -= $value - $configuration->value + $trailer;
28 18
        } elseif ($configuration->propertyPath) {
29 18
            $compare = Accessor::getPropertyValue($field->object, $configuration->propertyPath);
30
31 18
            if (!is_numeric($compare)) {
32 6
                throw new \InvalidArgumentException('Non-numeric value supplied');
33
            }
34
35 12
            if ($value >= $compare) {
36 12
                $value -= $value - $compare + $trailer;
37
            }
38
        }
39
40 24
        return true;
41
    }
42
43 15
    protected function useTrailer()
44
    {
45 15
        return false;
46
    }
47
}
48