Completed
Push — master ( 5227cf...4597e7 )
by Tristan
08:33
created

FormatConstraint::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\JsonSchema\Constraints;
4
5
use JsonSchema\Constraints\Factory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Nicofuma\SwaggerBundle\J...ema\Constraints\Factory.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use JsonSchema\Constraints\FormatConstraint as FormatConstraintBase;
7
use JsonSchema\Uri\UriRetriever;
8
use Nicofuma\SwaggerBundle\Exception\FormatConstraintException;
9
use Nicofuma\SwaggerBundle\JsonSchema\Constraints\Format\FormatValidatorInterface;
10
11
class FormatConstraint extends FormatConstraintBase
12
{
13
    /** @var FormatValidatorInterface[] */
14
    private $formatMap = [];
15
16
    public function __construct($checkMode, UriRetriever $uriRetriever, Factory $factory)
17 4
    {
18
        parent::__construct($checkMode, $uriRetriever, $factory);
19 4
20 1
        if ($factory instanceof \Nicofuma\SwaggerBundle\JsonSchema\Constraints\Factory) {
21
            $this->formatMap = $factory->getFormatValidators();
22
        }
23 3
    }
24
25 1
    /**
26 1
     * {@inheritdoc}
27 1
     */
28 1
    public function check($element, $schema = null, $path = null, $i = null)
29 1
    {
30
        if (!isset($schema->format)) {
31
            return;
32 1
        }
33
34
        if (array_key_exists($schema->format, $this->formatMap)) {
35 2
            try {
36 2
                $this->formatMap[$schema->format]->validate($element);
37
            } catch (FormatConstraintException $e) {
38
                foreach ($e->getErrors() as $error) {
39
                    $this->addError($path, $error, 'format');
40
                }
41
            }
42
43
            return;
44 1
        }
45
46 1
        parent::check($element, $schema, $path, $i);
47 1
    }
48
49
    /**
50
     * Add a format validator.
51
     *
52
     * @param string                   $format
53
     * @param FormatValidatorInterface $formatValidator
54
     */
55
    public function addFormatValidator($format, FormatValidatorInterface $formatValidator)
56
    {
57
        $this->formatMap[$format] = $formatValidator;
58
    }
59
}
60