Completed
Push — master ( edbd19...e97690 )
by Pavel
04:45
created

MappedEntityFormFactory::createFormForClass()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 26
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 5
nop 1
crap 12
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\Symfony;
4
5
use ScayTrase\Api\Cruds\Exception\EntityProcessingException;
6
use ScayTrase\Api\Cruds\Exception\MapperException;
7
use ScayTrase\Api\Cruds\PropertyMapperInterface;
8
use Symfony\Component\Form\FormFactoryInterface;
9
use Symfony\Component\Form\FormInterface;
10
11
final class MappedEntityFormFactory
12
{
13
    /** @var  FormFactoryInterface */
14
    private $factory;
15
    /** @var  PropertyMapperInterface */
16
    private $mapper;
17
18
    /**
19
     * MappedEntityFormFactory constructor.
20
     *
21
     * @param FormFactoryInterface    $factory
22
     * @param PropertyMapperInterface $mapper
23
     */
24
    public function __construct(FormFactoryInterface $factory, PropertyMapperInterface $mapper)
25
    {
26
        $this->factory = $factory;
27
        $this->mapper  = $mapper;
28
    }
29
30
    /**
31
     * @param $className
32
     *
33
     * @return FormInterface
34
     *
35
     * @throws EntityProcessingException
36
     */
37
    public function createFormForClass($className)
38
    {
39
        $form = $this->factory->create();
40
41
        try {
42
            foreach ($this->mapper->getApiProperties($className) as $apiProperty) {
43
                $form->add(
44
                    $apiProperty,
45
                    $this->factory->createForProperty(
0 ignored issues
show
Documentation introduced by
$this->factory->createFo...assName, $apiProperty)) is of type object<Symfony\Component\Form\FormInterface>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
                        $className,
47
                        $this->mapper->getObjectProperty($className, $apiProperty)
48
                    )
49
                );
50
            }
51
        } catch (\Exception $exception) {
52
            throw new EntityProcessingException(
53
                sprintf(
54
                    'Cannot create form for class %s: %s',
55
                    $className,
56
                    $exception->getMessage()
57
                ),
58
                $exception->getCode(),
59
                $exception
60
            );
61
        }
62
63
        return $form;
64
    }
65
}
66