FileDTODataTransformerBuilder::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle\DependencyInjection\Compiler\Application\DataTransformer;
14
15
use BenGorFile\FileBundle\DependencyInjection\Compiler\Application\ApplicationBuilder;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * File DTO data transformer builder.
22
 *
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
class FileDTODataTransformerBuilder implements ApplicationBuilder
26
{
27
    /**
28
     * Configuration array.
29
     *
30
     * @var array
31
     */
32
    protected $configuration;
33
34
    /**
35
     * The container builder.
36
     *
37
     * @var ContainerBuilder
38
     */
39
    protected $container;
40
41
    /**
42
     * The FQCN or the service id of file data transformer.
43
     *
44
     * @var string
45
     */
46
    protected $dataTransformer;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param ContainerBuilder $container       The container builder
52
     * @param string           $dataTransformer The FQCN or the service id of file data transformer
53
     * @param array            $configuration   The configuration tree
54
     */
55
    public function __construct(ContainerBuilder $container, $dataTransformer, array $configuration = [])
56
    {
57
        $this->container = $container;
58
        $this->dataTransformer = $dataTransformer;
59
        $this->configuration = $configuration;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function build($file)
66
    {
67
        $dataTransformer = class_exists($this->dataTransformer)
68
            ? new Definition($this->dataTransformer)
69
            : new Reference($this->dataTransformer);
70
71
        $this->container->setDefinition(
72
            'bengor.file.application.data_transformer.' . $file . '_dto',
73
            $dataTransformer
0 ignored issues
show
Bug introduced by
It seems like $dataTransformer defined by class_exists($this->data...$this->dataTransformer) on line 67 can also be of type object<Symfony\Component...ncyInjection\Reference>; however, Symfony\Component\Depend...uilder::setDefinition() does only seem to accept object<Symfony\Component...cyInjection\Definition>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
        );
75
76
        $this->container->setAlias(
77
            'bengor_file.' . $file . '.dto_data_transformer',
78
            'bengor.file.application.data_transformer.' . $file . '_dto'
79
        );
80
81
        return $this->container;
82
    }
83
}
84