Completed
Push — master ( f8bd36...f2dc96 )
by Jean-Christophe
01:33
created

Validator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validate_() 0 9 2
A setParams() 0 5 2
A getMember() 0 3 1
A setMember() 0 3 1
A getParameters() 0 4 1
A mergeMessages() 0 7 2
A _getMessage() 3 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ubiquity\contents\validation\validators;
4
5
abstract class Validator implements ValidatorInterface{
6
	protected $modifiedMessage;
7
	protected $message;
8
	protected $member;
9
	protected $value;
10
	
11
	public function validate_($value,$member,$instance,$params,$severity='warning',$message=null){
1 ignored issue
show
Unused Code introduced by
The parameter $instance 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...
12
		$this->value=$value;
13
		$this->setParams($params);
14
		if(!$this->validate($value)){
15
			$this->modifiedMessage=$message;
16
			return new ConstraintViolation($this->_getMessage(), $value, $member, get_class($this),$severity);
17
		}
18
		return true;
19
	}
20
	
21
	protected function setParams(array $params){
22
		foreach ($params as $member=>$value){
23
			$this->$member=$value;
24
		}
25
	}
26
	/**
27
	 * @return mixed
28
	 */
29
	public function getMember() {
30
		return $this->member;
31
	}
32
33
	/**
34
	 * @param mixed $member
35
	 */
36
	public function setMember($member) {
37
		$this->member = $member;
38
	}
39
	
40
	/**
41
	 * {@inheritDoc}
42
	 * @see \Ubiquity\contents\validation\validators\ValidatorInterface::getParameters()
43
	 */
44
	public function getParameters(): array {
45
		return [];
46
		
47
	}
48
	
49
	protected function mergeMessages(){
50
		if(!isset($this->modifiedMessage)){
51
			return $this->message;
52
		}else{
53
			return $this->modifiedMessage;
54
		}
55
	}
56
	
57
	protected function _getMessage(){
58
		$parameters=$this->getParameters();
59
		$message=$this->mergeMessages();
60 View Code Duplication
		foreach ($parameters as $param){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
			$message=str_replace("{".$param."}", $this->$param, $message);
62
		}
63
		return $message;
64
	}
65
66
}
67
68