Completed
Push — 2.x ( a0426f...f16329 )
by Christian
01:41 queued 01:38
created

AbstractScalarParam::getConstraints()   C

Complexity

Conditions 13
Paths 45

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 13.0062

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 29
cts 30
cp 0.9667
rs 6.6166
c 0
b 0
f 0
cc 13
nc 45
nop 0
crap 13.0062

How to fix   Long Method    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
    public $allowBlank = true;
35
36
    /** {@inheritdoc} */
37 29
    public function getConstraints()
38
    {
39 29
        $constraints = parent::getConstraints();
40
41 29
        if ($this->requirements instanceof Constraint) {
42 8
            $constraints[] = $this->requirements;
43 24
        } elseif (is_scalar($this->requirements)) {
44 7
            $constraints[] = new Regex(array(
45 7
                'pattern' => '#^(?:'.$this->requirements.')$#xsu',
46 7
                'message' => sprintf(
47 7
                    'Parameter \'%s\' value, does not match requirements \'%s\'',
48 7
                    $this->getName(),
49 7
                    $this->requirements
50
                ),
51
            ));
52 18
        } 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 1
                'message' => $this->requirements['error_message'],
56
            ));
57 17
        } elseif (is_array($this->requirements)) {
58 1
            foreach ($this->requirements as $requirement) {
59 1
                if ($requirement instanceof Constraint) {
60 1
                    $constraints[] = $requirement;
61
                } else {
62
                    @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
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
            }
65
        }
66
67 29
        if (false === $this->allowBlank) {
68 2
            $notBlank = new NotBlank();
69 2
            if (property_exists(NotBlank::class, 'allowNull')) {
70 2
                $notBlank->allowNull = $this->nullable;
71
            }
72 2
            $constraints[] = $notBlank;
73
        }
74
75
        // If the user wants to map the value, apply all constraints to every
76
        // value of the map
77 29
        if ($this->map) {
78
            $constraints = array(
79 5
                new All(array('constraints' => $constraints)),
80
            );
81 5
            if (false === $this->nullable) {
82 4
                $constraints[] = new NotNull();
83
            }
84
        }
85
86 29
        return $constraints;
87
    }
88
}
89