Completed
Pull Request — develop (#15)
by Narcotic
13:08
created

ConstraintTrait::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * trait for custom constraint classes
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Validator\Constraint;
7
8
use JsonSchema\Constraints\Factory as CustomFactory;
9
use JsonSchema\Uri\UriRetriever;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
trait ConstraintTrait
18
{
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $dispatcher;
24
25
    /**
26
     * @var CustomFactory
27
     */
28
    private $factory;
29
30
    /**
31
     * Format constructor.
32
     *
33
     * @param int                $checkMode    check mode
34
     * @param UriRetriever|null  $uriRetriever uri retriever
35
     * @param CustomFactory|null $factory      factory
36
     */
37
    public function __construct(
38
        $checkMode = self::CHECK_MODE_NORMAL,
39
        UriRetriever $uriRetriever = null,
40
        CustomFactory $factory = null
41
    ) {
42
        parent::__construct($checkMode, $uriRetriever, $factory);
43
        $this->factory = $factory;
44
    }
45
46
    /**
47
     * sets the event dispatcher
48
     *
49
     * @param EventDispatcherInterface $dispatcher dispatcher
50
     *
51
     * @return void
52
     */
53
    public function setEventDispatcher(EventDispatcherInterface $dispatcher)
54
    {
55
        $this->dispatcher = $dispatcher;
56
    }
57
58
    /**
59
     * checks the input
60
     *
61
     * @param mixed $element element
62
     * @param null  $schema  schema
63
     * @param null  $path    path
64
     * @param null  $i       iterator value
65
     *
66
     * @return void
67
     */
68
    public function check($element, $schema = null, $path = null, $i = null)
69
    {
70
        parent::check($element, $schema, $path, $i);
71
72
        $event = new $this->eventClass($this->factory, $element, $schema, $path);
0 ignored issues
show
Bug introduced by
The property eventClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
        $result = $this->dispatcher->dispatch($event::NAME, $event);
74
75
        $this->addErrors($result->getErrors());
0 ignored issues
show
Bug introduced by
It seems like addErrors() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
    }
77
}
78