Completed
Push — master ( 91372c...57e7bf )
by dima
05:02
created

ValidatorTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A preSave() 0 7 2
1
<?php
2
namespace Frameworkless;
3
4
use Symfony\Component\Validator\Validator\RecursiveValidator;
5
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
6
use Symfony\Component\Validator\ConstraintValidatorFactory;
7
use Symfony\Component\Translation\IdentityTranslator;
8
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
9
use Symfony\Component\Validator\Context\ExecutionContextFactory;
10
use Frameworkless\Exceptions\ValidationException;
11
/**
12
 * Description of ValidatorTrait
13
 *
14
 * @author Dmitriy
15
 */
16
trait ValidatorTrait
17
{
18
	/**
19
	 * Symfony\Component\Validator\Validator\RecursiveValidator
20
	 * @var type 
21
	 */
22
	protected $validator;
23
			
24
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
	{
26
		$this->validator = new RecursiveValidator(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\V...aintValidatorFactory()) of type object<Symfony\Component...tor\RecursiveValidator> is incompatible with the declared type object<Frameworkless\type> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
            new ExecutionContextFactory(new IdentityTranslator()),
28
            new LazyLoadingMetadataFactory(new StaticMethodLoader()),
29
            new ConstraintValidatorFactory()
30
        );
31
	}
32
	
33
	public function preSave(\Propel\Runtime\Connection\ConnectionInterface $con = null)
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
	{
35
        if (!$result = $this->validate($this->validator)) {
0 ignored issues
show
Bug introduced by
It seems like validate() 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...
36
            throw new ValidationException($this->getValidationFailures(),"Validation error");
0 ignored issues
show
Bug introduced by
It seems like getValidationFailures() 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...
37
        }     		
38
		return $result;
39
	}	
40
}
41