Completed
Push — master ( 92a758...e31339 )
by
unknown
06:27
created

AbstractScalarParam::getConstraints()   C

Complexity

Conditions 12
Paths 30

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 25
cts 25
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 30
nop 0
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Controller\Annotations;
13
14
use FOS\RestBundle\Validator\Constraints\Regex;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\Constraints\NotBlank;
17
use Symfony\Component\Validator\Constraints\All;
18
use Symfony\Component\Validator\Constraints\NotNull;
19
20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author Ener-Getick <[email protected]>
24
 */
25
abstract class AbstractScalarParam extends AbstractParam
26
{
27
    /** @var mixed */
28
    public $requirements = null;
29
30
    /** @var bool */
31
    public $map = false;
32
33
    /** @var bool */
34 11
    public $allowBlank = true;
35
36 11
    /** {@inheritdoc} */
37
    public function getConstraints()
38 11
    {
39 4
        $constraints = parent::getConstraints();
40 11
41 5
        if ($this->requirements instanceof Constraint) {
42 5
            $constraints[] = $this->requirements;
43 5
        } elseif (is_scalar($this->requirements)) {
44 5
            $constraints[] = new Regex(array(
45 5
                'pattern' => '#^(?:'.$this->requirements.')$#xsu',
46 5
                'message' => sprintf(
47 5
                    'Parameter \'%s\' value, does not match requirements \'%s\'',
48 5
                    $this->getName(),
49 10
                    $this->requirements
50 1
                ),
51 1
            ));
52 1
        } elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
53 1
            $constraints[] = new Regex(array(
54 1
                'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
55
                'message' => $this->requirements['error_message'],
56 11
            ));
57 1
        } elseif (is_array($this->requirements)) {
58 1
            foreach ($this->requirements as $requirement) {
59
                if ($requirement instanceof Constraint) {
60
                    $constraints[] = $requirement;
61
                } else {
62 11
                    @trigger_error('Using an array not only containing `Constraint`s as requirements is deprecated since version 2.6.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by Guilhem Niot
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
                }
64 4
            }
65 4
        }
66 4
67
        if (false === $this->allowBlank) {
68 11
            $constraints[] = new NotBlank();
69
        }
70
71
        // If the user wants to map the value, apply all constraints to every
72
        // value of the map
73
        if ($this->map) {
74
            $constraints = array(
75
                new All(array('constraints' => $constraints)),
76
            );
77
            if (false === $this->nullable) {
78
                $constraints[] = new NotNull();
79
            }
80
        }
81
82
        return $constraints;
83
    }
84
}
85