Completed
Push — master ( 79d36b...3588f7 )
by Eric
66:08 queued 03:30
created

Base64FileExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Base64 File package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Base64FileBundle\Form\Extension;
13
14
use Ivory\Base64FileBundle\Form\DataTransformer\Base64FileTransformer;
15
use Symfony\Component\Form\AbstractTypeExtension;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\OptionsResolver\Options;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class Base64FileExtension extends AbstractTypeExtension
25
{
26
    /**
27
     * @var bool
28
     */
29
    private $base64;
30
31
    /**
32
     * @param bool $base64
33
     */
34 195
    public function __construct($base64)
35
    {
36 195
        $this->base64 = $base64;
37 195
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 150
    public function buildForm(FormBuilderInterface $builder, array $options)
43
    {
44 150
        if ($options['base64']) {
45 135
            $builder->addViewTransformer(new Base64FileTransformer());
46 126
        }
47 150
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 143
    public function setDefaultOptions(OptionsResolverInterface $resolver)
53
    {
54 143
        $this->configureOptions($resolver);
55 143
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 165
    public function configureOptions(OptionsResolver $resolver)
61
    {
62 165
        $resolver->setDefaults([
63 165
            'base64'     => $this->base64,
64 165
            'data_class' => function (Options $options, $value) {
65 165
                return !$options['base64'] ? $value : null;
66 165
            },
67 154
        ]);
68
69 165
        if (method_exists($resolver, 'setDefault')) {
70 154
            $resolver->setAllowedTypes('base64', 'bool');
71 143
        } else {
72 11
            $resolver->setAllowedTypes(array('base64' => 'bool'));
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array('base64' => 'bool') is of type array<string,string,{"base64":"string"}>, but the function expects a string.

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...
73
        }
74 165
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 165
    public function getExtendedType()
80
    {
81 165
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
82 33
            return 'Symfony\Component\Form\Extension\Core\Type\FileType';
83
        }
84
85 132
        return 'file';
86
    }
87
}
88