AbstractScalarParam   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 64
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getConstraints() 0 51 14
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 $index => $requirement) {
59 1
                if ($requirement instanceof Constraint) {
60 1
                    $constraints[] = $requirement;
61
                } else {
62
                    throw new \TypeError(sprintf('Expected the requirements to be an array of %s instances but got %s at position %d.', Constraint::class, is_object($requirement) ? get_class($requirement) : gettype($requirement), $index));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Expected the re...($requirement), $index).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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