Completed
Push — dev ( db0115...3ad46f )
by Przemysław eRIZ
03:01
created

AssertGenerator::getGeneratorFqcn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 26
    public function generateForProperty(FieldMetadata $field, FakeMock $fakemock, ?string $group = null)
16
    {
17 26
        if (!$field->configuration->useAsserts) {
18 10
            return null;
19
        }
20
21 16
        $allAsserts = $field->annotations->findAllBy(Constraint::class);
22 16
        $asserts = $this->filterByGroup($allAsserts, $group);
23
24 16
        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 6
            $assert = $asserts[0];
26
27
            // https://coderwall.com/p/cpxxxw/php-get-class-name-without-namespace - reflection is the fastest?
28 6
            $baseClass = new \ReflectionClass($assert);
29
30 6
            if ($generator = $this->getGenerator($baseClass->getShortName())) {
31 4
                return $generator->generateForProperty($field, $assert, $this->generator);
32
            }
33
        }
34
35 12
        return null;
36
    }
37
38 16
    protected function filterByGroup($asserts, ?string $group = null){
39 16
        if(is_null($group)){
40 16
            return $asserts;
41
        }
42
43
        $result = array_filter($asserts, function($a) use ($group){
44
            /**
45
             * @var $a Constraint
46
             */
47
            return in_array($group, $a->groups);
48
        });
49
50
        return array_values($result);
51
    }
52
53 8
    protected function getGeneratorFqcn($simpleClassName)
54
    {
55 8
        return sprintf('Er1z\\FakeMock\\Generator\\AssertGenerator\\%s', $simpleClassName);
56
    }
57
}
58