Completed
Push — master ( 915dc2...1c41bf )
by onixsib
03:17
created

IPv4Validator::validateValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author: onixsib <[email protected]>
4
 * @date:   03.08.2016
5
 * @copyright onixsib
6
 * @link http://onixsib.ru/
7
 * @file IPv4Validator
8
 */
9
namespace onixsib\validators;
10
11
use Yii;
12
use yii\validators\Validator;
13
14
/**
15
 * Class IPv4Validator
16
 */
17
class IPv4Validator extends Validator {
18
	/**
19
	 * @inheritdoc
20
	 */
21
	public function init() {
22
		parent::init();
23
		if ($this->message === null) {
24
			$this->message = \Yii::t('yii', "{attribute} is invalid.");
25
		}
26
	}
27
28
	public function validateAttribute($model, $attribute) {
29
		$string = $model->{$attribute};
30
		if (!$this->validateValue($string)) {
31
			$this->addError($model, $attribute, \Yii::t('yii', 'This in not valid IPv4 address {sample}', ['sample' => '127.0.0.1']));
32
		}
33
	}
34
35
	public function validateValue($string) {
36
37
		// filter_var suxx
38
		return preg_match('/^((2[0-4]|1\d|[1-9])?\d|25[0-5])(\.(?1)){3}\z/', $string);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return preg_match('/^((2...?1)){3}\\z/', $string); (integer) is incompatible with the return type of the parent method yii\validators\Validator::validateValue of type array|null.

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
	}
40
}
41
42
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...