AssertGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 44
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateForProperty() 0 21 4
A filterByGroup() 0 14 2
A getGeneratorFqcn() 0 3 1
1
<?php
2
3
namespace Er1z\FakeMock\Generator;
4
5
use Er1z\FakeMock\FakeMock;
6
use Er1z\FakeMock\Metadata\FieldMetadata;
7
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...
8
9
/**
10
 * @todo: add interface with FakerableInterface in order to inject main Faker instance or sth
11
 * Class AssertGenerator
12
 */
13
class AssertGenerator extends AttachableGeneratorAbstract
14
{
15 43
    public function generateForProperty(FieldMetadata $field, FakeMock $fakemock, ?string $group = null)
16
    {
17 43
        if (!$field->configuration->useAsserts) {
18 13
            return null;
19
        }
20
21 30
        $allAsserts = $field->annotations->findAllBy(Constraint::class);
22 30
        $asserts = $this->filterByGroup($allAsserts, $group);
23
24 30
        if ($asserts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $asserts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
25 9
            $assert = $asserts[0];
26
27
            // https://coderwall.com/p/cpxxxw/php-get-class-name-without-namespace - reflection is the fastest?
28 9
            $baseClass = new \ReflectionClass($assert);
29
30 9
            if ($generator = $this->getGenerator($baseClass->getShortName())) {
31 6
                return $generator->generateForProperty($field, $assert, $this->fakerRegistry->getGeneratorForField($field));
32
            }
33
        }
34
35 24
        return null;
36
    }
37
38 30
    protected function filterByGroup($asserts, ?string $group = null)
39
    {
40 30
        if (is_null($group)) {
41 30
            return $asserts;
42
        }
43
44
        $result = array_filter($asserts, function ($a) use ($group) {
45
            /*
46
             * @var $a Constraint
47
             */
48
            return in_array($group, $a->groups);
49
        });
50
51
        return array_values($result);
52
    }
53
54 12
    protected function getGeneratorFqcn($simpleClassName)
55
    {
56 12
        return sprintf('Er1z\\FakeMock\\Generator\\AssertGenerator\\%s', $simpleClassName);
57
    }
58
}
59