Completed
Push — master ( 90a4d3...4d725e )
by Jean-Christophe
01:38
created

ValidatorMultiple::_getMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 3
Ratio 27.27 %

Importance

Changes 0
Metric Value
dl 3
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Ubiquity\contents\validation\validators\multiples;
4
5
use Ubiquity\contents\validation\validators\Validator;
6
use Ubiquity\utils\base\UString;
7
use Ubiquity\exceptions\ValidatorException;
8
9
abstract class ValidatorMultiple extends Validator{
10
	
11
	protected $violation;
12
	
13
	public function __construct(){
14
		$this->message=[
15
				"notNull"=>"This value should not be null"
16
		];
17
	}
18
19
	public function validate($value) {
20
		if($this->notNull===true && null==$value){
0 ignored issues
show
Bug introduced by
The property notNull 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...
21
			$this->violation="notNull";
22
			return false;
23
		}
24
		if (!UString::isValid($value)) {
25
			throw new ValidatorException('This value can not be converted to string');
26
		}
27
		return true;
28
	}
29
	
30
	/**
31
	 * @return array|string
32
	 */
33
	protected function mergeMessages(){
34
		if(!isset($this->modifiedMessage)){
35
			return $this->message;
36
		}else{
37
			if(is_array($this->modifiedMessage) && is_array($this->message)){
38
				return array_merge($this->message,$this->modifiedMessage);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge($this...this->modifiedMessage); (array) is incompatible with the return type of the parent method Ubiquity\contents\valida...alidator::mergeMessages of type string|array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
39
			}else{
40
				return $this->modifiedMessage;
41
			}
42
		}
43
	}
44
	
45
	protected function _getMessage(){
46
		$parameters=$this->getParameters();
47
		$message=$this->mergeMessages();
48
		if(isset($this->violation) && is_array($message)){
49
			$message=$this->_getMessageViolation($message);
50
		}
51 View Code Duplication
		foreach ($parameters as $param){
52
			$message=str_replace("{".$param."}", $this->$param, $message);
53
		}
54
		return $message;
55
	}
56
	
57
	protected function _getMessageViolation($messages){
58
		if(isset($messages[$this->violation])){
59
			return $messages[$this->violation];
60
		}
61
		return reset($messages);
62
	}
63
64
65
}
66
67